From: Ulrich Drepper Date: Wed, 4 Mar 1998 10:23:33 +0000 (+0000) Subject: Really update from 2.1. X-Git-Tag: cvs/before-sparc-2_0_x-branch~136 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1c937c5386c342cea3eef339af077570dbefe918;p=thirdparty%2Fglibc.git Really update from 2.1. --- diff --git a/misc/efgcvt_r.c b/misc/efgcvt_r.c index e152e271f57..f8acc55242e 100644 --- a/misc/efgcvt_r.c +++ b/misc/efgcvt_r.c @@ -23,6 +23,7 @@ #include #include #include +#include #ifndef FLOAT_TYPE #define FLOAT_TYPE double @@ -70,12 +71,21 @@ APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit, decpt, sign, buf, len) if (ndigit < 0) { - /* Rounding to the left of the decimal point. Solaris 2.5.1. */ - for (i = ndigit; i < 0; i++) - value *= 0.1; + /* Rounding to the left of the decimal point. */ + while (ndigit < 0) + { + FLOAT_TYPE new_value = value * 0.1; + + if (new_value < 1.0) + { + ndigit = 0; + break; + } - left = ndigit; - ndigit = 0; + value = new_value; + ++left; + ++ndigit; + } } } else @@ -100,19 +110,31 @@ APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit, decpt, sign, buf, len) do ++i; while (i < n && !isdigit (buf[i])); - memmove (&buf[*decpt], &buf[i], n - i); - n -= i - *decpt; - buf[n] = '\0'; + + if (*decpt == 1 && buf[0] == '0' && value != 0.0) + { + /* We must not have leading zeroes. Strip them all out and + adjust *DECPT if necessary. */ + --*decpt; + while (i < n && buf[i] == '0') + { + --*decpt; + ++i; + } + } + + memmove (&buf[MAX (*decpt, 0)], &buf[i], n - i); + buf[n - (i - MAX (*decpt, 0))] = '\0'; } if (left) { - *decpt -= left; + *decpt += left; if (--len > n) { - while (left++ < 0 && len > n) + while (left-- > 0 && n < len) buf[n++] = '0'; - buf [n] = '\0'; + buf[n] = '\0'; } } @@ -157,7 +179,7 @@ APPEND (FUNC_PREFIX, ecvt_r) (value, ndigit, decpt, sign, buf, len) do { d *= 10.0; - exponent--; + --exponent; } while (d < 1.0); } @@ -166,7 +188,7 @@ APPEND (FUNC_PREFIX, ecvt_r) (value, ndigit, decpt, sign, buf, len) do { d *= 0.1; - exponent++; + ++exponent; } while (d >= 10.0); } @@ -176,17 +198,24 @@ APPEND (FUNC_PREFIX, ecvt_r) (value, ndigit, decpt, sign, buf, len) value = d; } } + else if (value == 0.0) + /* SUSv2 leaves it unspecified whether *DECPT is 0 or 1 for 0.0. + This could be changed to -1 if we want to return 0. */ + exponent = 0; - if (APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit - 1, decpt, sign, buf, len)) - return -1; - - if (ndigit <= 0) + if (ndigit <= 0 && len > 0) { - if (len > 0) - buf[0] = '\0'; - *decpt = ndigit; - return 0; + buf[0] = '\0'; + *decpt = 1; + if (!ISINF (value) && !ISNAN (value)) + *sign = value < 0.0; + else + *sign = 0; } + else + if (APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit - 1, decpt, sign, + buf, len)) + return -1; *decpt += exponent; return 0;