Browse Source

Nan and Infinity can be negative

Decimal point was missing for some exponential output
Only divide sign by 10 once per loop
Add a missing NUL terminator for one case
If there is no whole number portion, print a leading zero (e.g. 0.1 not .1)
OPENBSD_3_9
millert 19 years ago
parent
commit
8773a5e4ee
1 changed files with 12 additions and 6 deletions
  1. +12
    -6
      src/lib/libc/stdlib/gcvt.c

+ 12
- 6
src/lib/libc/stdlib/gcvt.c View File

@ -1,4 +1,4 @@
/* $OpenBSD: gcvt.c,v 1.6 2005/08/08 08:05:36 espie Exp $ */
/* $OpenBSD: gcvt.c,v 1.7 2006/01/10 02:08:28 millert Exp $ */
/* /*
* Copyright (c) 2002, 2003 Todd C. Miller <Todd.Miller@courtesan.com> * Copyright (c) 2002, 2003 Todd C. Miller <Todd.Miller@courtesan.com>
@ -40,7 +40,7 @@ gcvt(double value, int ndigit, char *buf)
digits = __dtoa(value, 2, ndigit, &decpt, &sign, NULL); digits = __dtoa(value, 2, ndigit, &decpt, &sign, NULL);
if (decpt == 9999) { if (decpt == 9999) {
/* Infinity or NaN, assume buffer is at least ndigit long. */ /* Infinity or NaN, assume buffer is at least ndigit long. */
strlcpy(buf, digits, ndigit + 1);
snprintf(buf, ndigit + 1, "%s%s", sign ? "-" : "", digits);
return (buf); return (buf);
} }
@ -49,13 +49,16 @@ gcvt(double value, int ndigit, char *buf)
*dst++ = '-'; *dst++ = '-';
if (decpt < 0 || decpt > ndigit) { if (decpt < 0 || decpt > ndigit) {
/* exponential format */
/* exponential format (e.g. 1.2345e+13) */
if (--decpt < 0) { if (--decpt < 0) {
sign = 1; sign = 1;
decpt = -decpt; decpt = -decpt;
} else } else
sign = 0; sign = 0;
for (src = digits; *src != '\0'; )
src = digits;
*dst++ = *src++;
*dst++ = '.'; /* XXX - locale-specific */
while (*src != '\0')
*dst++ = *src++; *dst++ = *src++;
*dst++ = 'e'; *dst++ = 'e';
if (sign) if (sign)
@ -69,7 +72,8 @@ gcvt(double value, int ndigit, char *buf)
} else { } else {
/* XXX - optimize */ /* XXX - optimize */
for (sign = decpt, i = 0; (sign /= 10) != 0; i++) for (sign = decpt, i = 0; (sign /= 10) != 0; i++)
sign /= 10;
continue;
dst[i + 1] = '\0';
while (decpt != 0) { while (decpt != 0) {
dst[i--] = '0' + decpt % 10; dst[i--] = '0' + decpt % 10;
decpt /= 10; decpt /= 10;
@ -84,7 +88,9 @@ gcvt(double value, int ndigit, char *buf)
*dst++ = '0'; *dst++ = '0';
} }
if (*src != '\0') { if (*src != '\0') {
*dst++ = '.'; /* XXX - locale-specific (LC_NUMERIC) */
if (src == digits)
*dst++ = '0'; /* zero before decimal point */
*dst++ = '.'; /* XXX - locale-specific */
for (i = decpt; digits[i] != '\0'; i++) { for (i = decpt; digits[i] != '\0'; i++) {
*dst++ = digits[i]; *dst++ = digits[i];
} }


Loading…
Cancel
Save