diff options
Diffstat (limited to 'misc')
-rw-r--r-- | misc/efgcvt_r.c | 29 | ||||
-rw-r--r-- | misc/tst-efgcvt.c | 12 |
2 files changed, 38 insertions, 3 deletions
diff --git a/misc/efgcvt_r.c b/misc/efgcvt_r.c index f6bd988..9e586e1 100644 --- a/misc/efgcvt_r.c +++ b/misc/efgcvt_r.c @@ -49,6 +49,7 @@ APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit, decpt, sign, buf, len) size_t len; { int n, i; + int left; if (buf == NULL) { @@ -56,6 +57,7 @@ APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit, decpt, sign, buf, len) return -1; } + left = 0; if (isfinite (value)) { *sign = signbit (value) != 0; @@ -65,10 +67,20 @@ APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit, decpt, sign, buf, len) if (ndigit < 0) { /* Rounding to the left of the decimal point. */ - for (i = ndigit; i < 0; i++) - value *= 0.1; + while (ndigit < 0) + { + FLOAT_TYPE new_value = value * 0.1; - ndigit = 0; + if (new_value < 1.0) + { + ndigit = 0; + break; + } + + value = new_value; + ++left; + ++ndigit; + } } } else @@ -110,6 +122,17 @@ APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit, decpt, sign, buf, len) buf[n - (i - MAX (*decpt, 0))] = '\0'; } + if (left) + { + *decpt += left; + if (--len > n) + { + while (left-- > 0 && n < len) + buf[n++] = '0'; + buf[n] = '\0'; + } + } + return 0; } diff --git a/misc/tst-efgcvt.c b/misc/tst-efgcvt.c index 65c588b..fc6bbf2 100644 --- a/misc/tst-efgcvt.c +++ b/misc/tst-efgcvt.c @@ -49,6 +49,12 @@ static testcase ecvt_tests[] = { 5.5, 1, 1, "6" }, { 1.0, -1, 1, "" }, { 0.01, 2, -1, "10" }, + { 100.0, -2, 3, "" }, + { 100.0, -5, 3, "" }, + { 100.0, -4, 3, "" }, + { 100.01, -4, 3, "" }, + { 123.01, -4, 3, "" }, + { 126.71, -4, 3, "" }, /* -1.0 is end marker. */ { -1.0, 0, 0, "" } }; @@ -65,6 +71,12 @@ static testcase fcvt_tests[] = { 5.5, 1, 1, "55" }, { 5.5, 0, 1, "6" }, { 0.01, 2, -1, "1" }, + { 100.0, -2, 3, "100" }, + { 100.0, -5, 3, "100" }, + { 100.0, -4, 3, "100" }, + { 100.01, -4, 3, "100" }, + { 123.01, -4, 3, "100" }, + { 126.71, -4, 3, "100" }, /* -1.0 is end marker. */ { -1.0, 0, 0, "" } }; |