[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[libdfp-patches] [PATCH] Fix printing of mantissa near 1.0 in 'f' format



Hi,

This is a fix Andreas did. It's been tested and confirmed to work. I'm
submitting it to the list for the sake of formality.

Testcase:

int
main (int argc, char * argv[])
{
        _Decimal128 dfp1, dfp2;
        double fp1, fp2;
        char str1[14] = "0.4444444444";
        char str2[14] = "0.5555555555";
        char str3[14] = "0.9999999999";

        register_printf_dfp();

        dfp1 = strtod128(str1, NULL);
        dfp2 = strtod128(str2, NULL);

        fp1 = atof(str1);
        fp2 = atof(str2);

        printf("expected result   (ch  - %%s)     : %s\n", str3);
        printf("calculated result (fp  - %%.15f)  : %.15f\n", fp1 +
fp2);
        printf("calculated result (fp  - %%f)     : %f\n", fp1 + fp2);
        printf("calculated result (fp  - %%.15g)  : %.15g\n", fp1 +
fp2);
        printf("calculated result (fp  - %%g)     : %g\n", fp1 + fp2);
        printf("calculated result (dfp - %%.15DDf): %.15DDf\n", dfp1 +
dfp2);
        printf("calculated result (dfp - %%DDf)   : %DDf\n", dfp1 +
dfp2);

        return 0;
}


Wrong behavior: calculated result (dfp - %DDf)   : 0.10000
Correct behavior: calculated result (dfp - %DDf)   : 1.00000

Luis
--

2010-10-04  Andreas Schwab  <schwab@xxxxxxxxxx>

	* printf_dfp.c (__printf_dfp): Fix up nd when rounding overflows
	beyond first digit.
---
 printf_dfp.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/printf_dfp.c b/printf_dfp.c
index 05700af..90af8db 100644
--- a/printf_dfp.c
+++ b/printf_dfp.c
@@ -697,8 +697,8 @@ __printf_dfp (FILE *fp,
 	  }
 
 	while (digits[--index] == '9') digits[index] = '0';
-	  digits[index]++;
-	if (index < n) { n--; }
+	digits[index]++;
+	if (index < n) { n--; nd++; }
       } while (0);
     } /* Done rounding.  */