Skip to content

Commit af9551e

Browse files
committed
Convert decnum to binary64 (double) instead of decimal64
This is what the JSON spec suggests and will also be less confusing compared to other jq implementations and langauges. Related to #2939
1 parent 88f01a7 commit af9551e

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/jv.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ enum {
213213
#define JVP_FLAGS_NUMBER_LITERAL JVP_MAKE_FLAGS(JV_KIND_NUMBER, JVP_MAKE_PFLAGS(JVP_NUMBER_DECIMAL, 1))
214214

215215
// the decimal precision of binary double
216-
#define DEC_NUBMER_DOUBLE_PRECISION (16)
216+
#define DEC_NUBMER_DOUBLE_PRECISION (17)
217217
#define DEC_NUMBER_STRING_GUARD (14)
218218
#define DEC_NUBMER_DOUBLE_EXTRA_UNITS ((DEC_NUBMER_DOUBLE_PRECISION - DECNUMDIGITS + DECDPUN - 1)/DECDPUN)
219219

@@ -535,9 +535,13 @@ static decContext* tsd_dec_ctx_get(pthread_key_t *key) {
535535
}
536536
else if (key == &dec_ctx_dbl_key)
537537
{
538-
decContextDefault(ctx, DEC_INIT_DECIMAL64);
539-
// just to make sure we got this right
540-
assert(ctx->digits <= DEC_NUBMER_DOUBLE_PRECISION);
538+
// constants suitable for double (binary64)
539+
ctx->digits = DEC_NUBMER_DOUBLE_PRECISION;
540+
ctx->emax = 1023;
541+
ctx->emin = -1022;
542+
ctx->round = DEC_ROUND_HALF_EVEN;
543+
ctx->traps = 0;
544+
ctx->clamp = 1;
541545
}
542546
if (pthread_setspecific(*key, ctx) != 0) {
543547
fprintf(stderr, "error: cannot store thread specific data");

tests/jq.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1867,6 +1867,15 @@ false
18671867
1
18681868
1
18691869

1870+
# decnum to double conversion
1871+
.[] as $n | $n+0 | [., tostring, . == $n]
1872+
[-9007199254740993, -9007199254740992, 9007199254740992, 9007199254740993, 13911860366432393]
1873+
[-9007199254740992,"-9007199254740992",true]
1874+
[-9007199254740992,"-9007199254740992",true]
1875+
[9007199254740992,"9007199254740992",true]
1876+
[9007199254740992,"9007199254740992",true]
1877+
[13911860366432392,"13911860366432392",true]
1878+
18701879
# abs, fabs, length
18711880
abs
18721881
"abc"

0 commit comments

Comments
 (0)