fix: preserve negative zero sign in std.mantissa - #1092
Merged
stephenamar-db merged 1 commit intoJul 28, 2026
Conversation
Motivation: `std.mantissa(0 * -1)` returned positive 0.0 instead of -0.0. The zero guard used literal `0.0`, discarding the IEEE 754 sign bit. std.mantissa implements the mantissa component of frexp (C99 §7.12.6.4); IEEE 754 requires sign preservation. go-jsonnet (math.Frexp), jrsonnet (returns s directly), and Python (math.frexp) all return -0.0 for negative zero. Modification: Change `if (x == 0) 0.0` to `if (x == 0) x`. Since -0.0 == 0.0 is true in IEEE 754, the guard still catches both zeros but returns the input unchanged, preserving the sign bit. std.exponent needs no change (returns integer 0L, no sign bit to preserve). Result: `std.mantissa(0 * -1)` returns -0.0, matching all reference implementations. Positive zero and non-zero behavior unchanged.
He-Pin
force-pushed
the
fix/mantissa-negative-zero
branch
from
July 27, 2026 19:04
ac97744 to
e0c5ec3
Compare
stephenamar-db
approved these changes
Jul 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
std.mantissa(0 * -1)returned positive0.0instead of-0.0. The zero guard used literal0.0, discarding the IEEE 754 sign bit.Mathematical basis:
std.mantissaimplements the mantissa component offrexp(C99 §7.12.6.4). While C says "if value is zero, both parts are zero", IEEE 754 requires sign preservation. All reference implementations (go-jsonnet via Go'smath.Frexp, jrsonnet via Rust's bit-preserving(s, 0)return, Python viamath.frexp) return(-0.0, 0)for negative zero input.Modification
Change
if (x == 0) 0.0toif (x == 0) x. Since-0.0 == 0.0istruein IEEE 754, the guard still catches both zeros but now returns the input unchanged, preserving the sign bit.std.exponentdoes not need the same fix — it returns an integer (0L), so there is no sign bit to preserve.Result
std.mantissa(0 * -1)now returns-0.0, matching all reference implementations. Positive zero behavior is unchanged.Behavior comparison
math.frexp)std.mantissa(0 * -1)-0-0(-0.0, 0)0-0std.mantissa(0)00(0.0, 0)00(unchanged)std.mantissa(1.5)0.750.75(0.75, 1)0.750.75(unchanged)std.mantissa(-1.5)-0.75-0.75(-0.75, 1)-0.75-0.75(unchanged)Test plan
mantissa_negative_zero.jsonnetregression test with golden filestd.toStringto observe sign (assertEqualcannot distinguish -0.0 from 0.0 due to IEEE equality)std.toString(std.mantissa(0 * -1))→"-0"(was"0")std.toString(std.mantissa(0))→"0"(unchanged)