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.
This commit is contained in:
fhgffy authored and GitHub committed 2026-10-20 22:16:56 +09:00
1 parent 852c4d6096
commit afa23a53cd
2 files changed
+6 -1

No files matched your search

+1 -1
View File
@@ -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;
}
+5
View File
@@ -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