Skip to content

Commit

Permalink
Convert decnum to binary64 (double) instead of decimal64
Browse files Browse the repository at this point in the history
This is what the JSON spec suggests and will also be less confusing compared to other jq implementations and langauges.

Related to #2939
  • Loading branch information
wader committed Nov 23, 2023
1 parent 88f01a7 commit af9551e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/jv.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ enum {
#define JVP_FLAGS_NUMBER_LITERAL JVP_MAKE_FLAGS(JV_KIND_NUMBER, JVP_MAKE_PFLAGS(JVP_NUMBER_DECIMAL, 1))

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

Expand Down Expand Up @@ -535,9 +535,13 @@ static decContext* tsd_dec_ctx_get(pthread_key_t *key) {
}
else if (key == &dec_ctx_dbl_key)
{
decContextDefault(ctx, DEC_INIT_DECIMAL64);
// just to make sure we got this right
assert(ctx->digits <= DEC_NUBMER_DOUBLE_PRECISION);
// constants suitable for double (binary64)
ctx->digits = DEC_NUBMER_DOUBLE_PRECISION;
ctx->emax = 1023;
ctx->emin = -1022;
ctx->round = DEC_ROUND_HALF_EVEN;
ctx->traps = 0;
ctx->clamp = 1;
}
if (pthread_setspecific(*key, ctx) != 0) {
fprintf(stderr, "error: cannot store thread specific data");
Expand Down
9 changes: 9 additions & 0 deletions tests/jq.test
Original file line number Diff line number Diff line change
Expand Up @@ -1867,6 +1867,15 @@ false
1
1

# decnum to double conversion
.[] as $n | $n+0 | [., tostring, . == $n]
[-9007199254740993, -9007199254740992, 9007199254740992, 9007199254740993, 13911860366432393]
[-9007199254740992,"-9007199254740992",true]
[-9007199254740992,"-9007199254740992",true]
[9007199254740992,"9007199254740992",true]
[9007199254740992,"9007199254740992",true]
[13911860366432392,"13911860366432392",true]

# abs, fabs, length
abs
"abc"
Expand Down

0 comments on commit af9551e

Please sign in to comment.