Skip to content
Myr

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

2 files+30 added−31 removed

modified:src/jv.c+22 added-31 removed
213 unmodified lines
214#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))216217// 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)221222#include "jv_thread.h"223#ifdef WIN32224#ifndef __MINGW32__
305 unmodified lines
530 if (ctx) {531 return ctx;532 }533533 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 }547534 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 lines
561} jvp_literal_number;562563typedef struct {564 decNumber number;568 decNumberUnit units[1];569} decNumberSingle;570571typedef struct {572 decNumber number;573 decNumberUnit units[BIN64_DEC_PRECISION];565 decNumberUnit units[DEC_NUBMER_DOUBLE_EXTRA_UNITS];566} decNumberDoublePrecision;567568569static inline int jvp_number_is_literal(jv n) {
48 unmodified lines
618 assert(JVP_HAS_FLAGS(j, JVP_FLAGS_NUMBER_LITERAL));619620 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];623624 // 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());627628 decNumberToString(&dec_double.number, literal);629
124 unmodified lines
754 assert(JVP_HAS_KIND(b, JV_KIND_NUMBER));755756#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 lines
modified:tests/jq.test+8 added
546 unmodified lines
547[16 / 4 / 2, 16 / 4 * 2, 16 - 4 - 2, 16 - 4 + 2]548null549[2, 8, 10, 14]5505511e-19 + 1e-20 - 5e-21552null5531.05e-195545551 / 1e-17556null5571e+1755855925 % 7560null5614562
1442 unmodified lines