Fix rounding small (but not too small) numbers to zero on calculation
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by itchyny · committed by Nico Williams · parent 27a4d57 · Browse files · 2 files·+30 added−31 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Leonid S. Usov <leonid.s.usov@gmail.com>
Diff stats
2 files+30 added−31 removed
- tests:+8 added−0 removed
- other:+22 added−31 removed
Provenance
- Commit
680baef- Tree
e560115- Author
itchyny <itchyny@cybozu.co.jp>- Committer
Nico Williams <nico@cryptonector.com>- Parent
27a4d57
2 files changed+30 added−31 removed
modified:src/jv.c+22 added-31 removed
213 unmodified lines214 #define JVP_FLAGS_NUMBER_NATIVE JVP_MAKE_FLAGS(JV_KIND_NUMBER, JVP_MAKE_PFLAGS(JVP_NUMBER_NATIVE, 0))215 #define JVP_FLAGS_NUMBER_LITERAL JVP_MAKE_FLAGS(JV_KIND_NUMBER, JVP_MAKE_PFLAGS(JVP_NUMBER_DECIMAL, 1))216 217 // the decimal precision of binary double218-#define BIN64_DEC_PRECISION (17)219-#define DEC_NUMBER_STRING_GUARD (14)218+#define DEC_NUBMER_DOUBLE_PRECISION (16)219+#define DEC_NUMBER_STRING_GUARD (14)220+#define DEC_NUBMER_DOUBLE_EXTRA_UNITS ((DEC_NUBMER_DOUBLE_PRECISION - DECNUMDIGITS + DECDPUN - 1)/DECDPUN)221 222 #include "jv_thread.h"223 #ifdef WIN32224 #ifndef __MINGW32__305 unmodified lines530 if (ctx) {531 return ctx;532 }533 533- decContext _ctx = {534- 0,535- DEC_MAX_EMAX,536- DEC_MIN_EMAX,537- DEC_ROUND_HALF_UP,538- 0, /*no errors*/539- 0, /*status*/540- 0, /*no clamping*/541- };542- if (key == &dec_ctx_key) {543- _ctx.digits = DEC_MAX_DIGITS;544- } else if (key == &dec_ctx_dbl_key) {545- _ctx.digits = BIN64_DEC_PRECISION;546- }547-534 ctx = malloc(sizeof(decContext));535 if (ctx) {550- *ctx = _ctx;536+ if (key == &dec_ctx_key)537+ {538+ decContextDefault(ctx, DEC_INIT_BASE);539+ ctx->digits = DEC_MAX_DIGITS;540+ ctx->traps = 0; /*no errors*/541+ }542+ else if (key == &dec_ctx_dbl_key)543+ {544+ decContextDefault(ctx, DEC_INIT_DECIMAL64);545+ // just to make sure we got this right546+ assert(ctx->digits <= DEC_NUBMER_DOUBLE_PRECISION);547+ }548 if (pthread_setspecific(*key, ctx) != 0) {549 fprintf(stderr, "error: cannot store thread specific data");550 abort();551 }9 unmodified lines561 } jvp_literal_number;562 563 typedef struct {564 decNumber number;568- decNumberUnit units[1];569-} decNumberSingle;570-571-typedef struct {572- decNumber number;573- decNumberUnit units[BIN64_DEC_PRECISION];565+ decNumberUnit units[DEC_NUBMER_DOUBLE_EXTRA_UNITS];566 } decNumberDoublePrecision;567 568 569 static inline int jvp_number_is_literal(jv n) {48 unmodified lines618 assert(JVP_HAS_FLAGS(j, JVP_FLAGS_NUMBER_LITERAL));619 620 decNumber *p_dec_number = jvp_dec_number_ptr(j);621 decNumberDoublePrecision dec_double;630- char literal[BIN64_DEC_PRECISION + DEC_NUMBER_STRING_GUARD + 1];622+ char literal[DEC_NUBMER_DOUBLE_PRECISION + DEC_NUMBER_STRING_GUARD + 1];623 624 // reduce the number to the shortest possible form633- // while also making sure than no more than BIN64_DEC_PRECISION634- // digits are used (dec_context_to_double)625+ // that fits into the 64 bit floating point representation626 decNumberReduce(&dec_double.number, p_dec_number, DEC_CONTEXT_TO_DOUBLE());627 628 decNumberToString(&dec_double.number, literal);629 124 unmodified lines754 assert(JVP_HAS_KIND(b, JV_KIND_NUMBER));755 756 #ifdef USE_DECNUM757 if (JVP_HAS_FLAGS(a, JVP_FLAGS_NUMBER_LITERAL) && JVP_HAS_FLAGS(b, JVP_FLAGS_NUMBER_LITERAL)) {767- decNumberSingle res;768- decNumberCompare(&res.number,758+ decNumber res;759+ decNumberCompare(&res,760 jvp_dec_number_ptr(a),761 jvp_dec_number_ptr(b),762 DEC_CONTEXT()763 );773- if (decNumberIsZero(&res.number)) {764+ if (decNumberIsZero(&res)) {765 return 0;775- } else if (decNumberIsNegative(&res.number)) {766+ } else if (decNumberIsNegative(&res)) {767 return -1;768 } else {769 return 1;770 }1221 unmodified linesmodified:tests/jq.test+8 added
546 unmodified lines547 [16 / 4 / 2, 16 / 4 * 2, 16 - 4 - 2, 16 - 4 + 2]548 null549 [2, 8, 10, 14]550 551+1e-19 + 1e-20 - 5e-21552+null553+1.05e-19554+555+1 / 1e-17556+null557+1e+17558+559 25 % 7560 null561 4562 1442 unmodified lines