From 0777ba33fc206f05967851b2231e6c4091e07a76 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Viet <123613986+NgoQuocViet2001@users.noreply.github.com> Date: Sat, 5 Dec 2026 19:37:44 +0700 Subject: [PATCH] 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`. --- src/jv.c | 30 ++++++++++-------------------- tests/jq.test | 8 ++++++++ 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/jv.c b/src/jv.c index 4cddc5a6..de7fe998 100644 --- a/src/jv.c +++ b/src/jv.c @@ -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) { diff --git a/tests/jq.test b/tests/jq.test index a55e98d3..89e4a04b 100644 --- a/tests/jq.test +++ b/tests/jq.test @@ -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"