Fix double rounding of number literals with more than 17 digits (#3695)

Converting a number literal to a double first rounded it to 17
significant digits, so a literal near the midpoint of two doubles
could be rounded twice in the wrong direction.

Convert all the digits of the literal at once, so that it rounds to
the nearest double: `100000000000000041 * 1` now gives
`100000000000000050` instead of `100000000000000030`.
This commit is contained in:
Ngo Quoc Viet authored and GitHub committed 2026-12-05 21:37:44 +09:00
1 parent 2b0e51cfb1
commit 0777ba33fc
2 files changed
+18 -20

No files matched your search

+10 -20
View File
@@ -208,7 +208,6 @@ enum {
// the decimal precision of binary double
#define DEC_NUMBER_DOUBLE_PRECISION (17)
#define DEC_NUMBER_STRING_GUARD (14)
#define DEC_NUMBER_DOUBLE_EXTRA_UNITS ((DEC_NUMBER_DOUBLE_PRECISION - DECNUMDIGITS + DECDPUN - 1)/DECDPUN)
#include "jv_thread.h"
#ifdef WIN32
@@ -535,11 +534,6 @@ typedef struct {
decNumber num_decimal; // must be the last field in the structure for memory management
} jvp_literal_number;
typedef struct {
decNumber number;
decNumberUnit units[DEC_NUMBER_DOUBLE_EXTRA_UNITS];
} decNumberDoublePrecision;
static inline int jvp_number_is_literal(jv n) {
assert(JVP_HAS_KIND(n, JV_KIND_NUMBER));
@@ -601,24 +595,20 @@ static jv jvp_literal_number_new(const char * literal) {
static double jvp_literal_number_to_double(jv j) {
assert(JVP_HAS_FLAGS(j, JVP_FLAGS_NUMBER_LITERAL));
decContext dblCtx;
// init as decimal64 but change digits to allow conversion to binary64 (double)
decContextDefault(&dblCtx, DEC_INIT_DECIMAL64);
dblCtx.digits = DEC_NUMBER_DOUBLE_PRECISION;
decNumber *p_dec_number = jvp_dec_number_ptr(j);
decNumberDoublePrecision dec_double;
char literal[DEC_NUMBER_DOUBLE_PRECISION + DEC_NUMBER_STRING_GUARD + 1];
char buf[DEC_NUMBER_DOUBLE_PRECISION + DEC_NUMBER_STRING_GUARD + 1];
size_t len = (size_t)p_dec_number->digits + DEC_NUMBER_STRING_GUARD + 1;
char *literal = len <= sizeof(buf) ? buf : jv_mem_alloc(len);
// reduce the number to the shortest possible form
// that fits into the 64 bit floating point representation
decNumberReduce(&dec_double.number, p_dec_number, &dblCtx);
decNumberToString(&dec_double.number, literal);
// Pass every digit to strtod: rounding to 17 significant digits first
// can land on a halfway point between two doubles and round the wrong way.
decNumberToString(p_dec_number, literal);
char *end;
return jvp_strtod(tsd_dtoa_context_get(), literal, &end);
double d = jvp_strtod(tsd_dtoa_context_get(), literal, &end);
if (literal != buf)
jv_mem_free(literal);
return d;
}
static const char* jvp_literal_number_literal(jv n) {
+8
View File
@@ -2545,6 +2545,14 @@ true
[9007199254740992,"9007199254740992",true]
[13911860366432392,"13911860366432392",true]
# Literals with more than 17 significant digits round to the nearest double
.[] * 1 | tostring
[100000000000000041, -12345678901234565.4, 9007199254740993.000000000000001, 1.00000000000000011102230246251565404236316680908203125001]
"100000000000000050"
"-12345678901234566"
"9007199254740994"
"1.0000000000000002"
# abs, fabs, length
abs
"abc"