From afa23a53cde43cbd7d6e5d91ae759e342d518de6 Mon Sep 17 00:00:00 2001 From: fhgffy <102001626+fhgffy@users.noreply.github.com> Date: Tue, 20 Oct 2026 21:16:56 +0800 Subject: [PATCH] Keep the sign of zero when negating a number literal (#3662) Since jq 1.8.0, negating a number literal computed 0 - x, so -0 and 0 | -. printed 0 while (0+0) | -. printed -0. Flip the sign bit with decNumberCopyNegate() instead, so -0 prints -0 again as it did in jq 1.7, and -(-0) is still 0. --- src/jv.c | 2 +- tests/jq.test | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/jv.c b/src/jv.c index 76d2bf82..930fb6fb 100644 --- a/src/jv.c +++ b/src/jv.c @@ -758,7 +758,7 @@ jv jv_number_negate(jv n) { if (JVP_HAS_FLAGS(n, JVP_FLAGS_NUMBER_LITERAL)) { jvp_literal_number* m = jvp_literal_number_alloc(jvp_dec_number_ptr(n)->digits); - decNumberMinus(&m->num_decimal, jvp_dec_number_ptr(n), DEC_CONTEXT()); + decNumberCopyNegate(&m->num_decimal, jvp_dec_number_ptr(n)); jv r = {JVP_FLAGS_NUMBER_LITERAL, 0, 0, 0, {&m->refcnt}}; return r; } diff --git a/tests/jq.test b/tests/jq.test index d45ecd24..557432d9 100644 --- a/tests/jq.test +++ b/tests/jq.test @@ -2443,6 +2443,11 @@ true null true +# Unary negation keeps the sign of zero +[-0, -(-0), (.[] | -.)] | map(tojson) +[0, -0] +["-0","0","-0","0"] + . |= try . catch . 1 1