Skip to content

Commit dbbca3d

Browse files
author
Fabrice Bellard
committed
dtoa fix for minus zero
1 parent 37cde16 commit dbbca3d

File tree

2 files changed

+6
-11
lines changed

2 files changed

+6
-11
lines changed

dtoa.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,9 @@ int js_dtoa(char *buf, double d, int radix, int n_digits, int flags,
11471147
P = n_digits + 1;
11481148
else
11491149
P = n_digits;
1150+
/* "-0" is displayed as "0" if JS_DTOA_MINUS_ZERO is not present */
1151+
if (sgn && (flags & JS_DTOA_MINUS_ZERO))
1152+
*q++ = '-';
11501153
goto output;
11511154
}
11521155
/* denormal number: convert to a normal number */
@@ -1156,6 +1159,8 @@ int js_dtoa(char *buf, double d, int radix, int n_digits, int flags,
11561159
} else {
11571160
m |= (uint64_t)1 << 52;
11581161
}
1162+
if (sgn)
1163+
*q++ = '-';
11591164
/* remove the bias */
11601165
e -= 1022;
11611166
/* d = 2^(e-53)*m */
@@ -1167,8 +1172,6 @@ int js_dtoa(char *buf, double d, int radix, int n_digits, int flags,
11671172
(flags & JS_DTOA_EXP_MASK) != JS_DTOA_EXP_ENABLED) {
11681173
m >>= 53 - e;
11691174
/* 'm' is never zero */
1170-
if (sgn)
1171-
*q++ = '-';
11721175
q += u64toa_radix(q, m, radix);
11731176
goto done;
11741177
}
@@ -1244,10 +1247,6 @@ int js_dtoa(char *buf, double d, int radix, int n_digits, int flags,
12441247
/* frac is rounded using RNDNA */
12451248
mul_pow_round(tmp1, m, e - 53, radix1, radix_shift, n_digits, JS_RNDNA);
12461249

1247-
/* "-0" is displayed as "0" */
1248-
if (sgn && !(tmp1->tab[0] == 0 && tmp1->len == 1)) {
1249-
*q++ = '-';
1250-
}
12511250
/* we add one extra digit on the left and remove it if needed
12521251
to avoid testing if the result is < radix^P */
12531252
len = output_digits(q, tmp1, radix, max_int(E + 1, 1) + n_digits,
@@ -1277,11 +1276,6 @@ int js_dtoa(char *buf, double d, int radix, int n_digits, int flags,
12771276
}
12781277
}
12791278
output:
1280-
/* "-0" is displayed as "0" if JS_DTOA_MINUS_ZERO is not present */
1281-
if (sgn && ((flags & JS_DTOA_MINUS_ZERO) ||
1282-
!(tmp1->tab[0] == 0 && tmp1->len == 1))) {
1283-
*q++ = '-';
1284-
}
12851279
if (fmt == JS_DTOA_FORMAT_FIXED)
12861280
E_max = n_digits;
12871281
else

tests/test_builtin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ function test_number()
393393
assert((-1.125).toFixed(2), "-1.13");
394394
assert((0.5).toFixed(0), "1");
395395
assert((-0.5).toFixed(0), "-1");
396+
assert((-1e-10).toFixed(0), "-0");
396397

397398
assert((1.3).toString(7), "1.2046204620462046205");
398399
assert((1.3).toString(35), "1.ahhhhhhhhhm");

0 commit comments

Comments
 (0)