fix: std.round preserves identity for |x| >= 2^52#1004
Open
He-Pin wants to merge 1 commit into
Open
Conversation
Motivation: std.round returned the wrong integer for large odd inputs in [2^52, 2^53) (e.g. std.round(9007199254740991) produced 9007199254740992). The prior `floor(x + 0.5)` / `ceil(x - 0.5)` implementation invokes IEEE 754 round-to-even when the ULP is >= 1.0, so adding 0.5 rounds to the nearest *even* integer rather than the correct half-away-from-zero. go-jsonnet (math.Round) and jrsonnet both return the identity for these inputs; sjsonnet was the outlier. Modification: Short-circuit in MathModule.scala std.round when |x| >= 2^52 or x is NaN — at that magnitude every double is already an exact integer (ULP >= 1.0), so returning x unchanged is both correct and avoids the precision trap. The half-away-from-zero rule is preserved for all smaller magnitudes, and NaN/Infinity propagate naturally. Added five regression assertions (2^53 - 1, 2^53 - 2, 2^52 + 1, -(2^53 - 1), 1e20) to the existing std.round/isEven/isOdd/isInteger/ isDecimal test fixture to lock in the corrected behavior. Result: std.round now matches go-jsonnet and jrsonnet on every integer in [2^52, 2^53). The existing std.round(0.5)==1 / std.round(-0.5)==-1 half-away-from-zero semantics are preserved, and all 23 file tests plus 86 evaluator tests pass. Cross-implementation comparison (std.round input → output): | Input | sjsonnet (before) | sjsonnet (after) | go-jsonnet 0.22.0 | jrsonnet 0.5.0-pre99 | C++ jsonnet 0.22.0 | |---------------------|-------------------|------------------|-------------------|----------------------|--------------------| | 9007199254740991 | 9007199254740992 | 9007199254740991 | 9007199254740991 | 9007199254740991 | 9007199254740992 * | | 4503599627370497 | 4503599627370498 | 4503599627370497 | 4503599627370497 | 4503599627370497 | 4503599627370498 * | | -9007199254740991 | -9007199254740992 | -9007199254740991| -9007199254740991 | -9007199254740991 | -9007199254740990* | | 0.5 | 1 | 1 | 1 | 1 | 1 | | -0.5 | -1 | -1 | -1 | -1 | 0 * | | 2.5 | 3 | 3 | 3 | 3 | 2 * | * C++ jsonnet v0.22.0 uses round-half-to-even (round(-0.5)==0, round(2.5)==2) and exhibits the same large-integer precision loss as pre-fix sjsonnet; go-jsonnet and jrsonnet use half-away-from-zero.
This was referenced Jun 20, 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.roundreturned the wrong integer for large odd inputs in[2^52, 2^53). For example,std.round(9007199254740991)(2^53 - 1)produced
9007199254740992. The priorfloor(x + 0.5)/ceil(x - 0.5)implementation invokes IEEE 754 round-to-even when theULP is >= 1.0, so adding 0.5 rounds to the nearest even integer
rather than the correct half-away-from-zero value. go-jsonnet
(
math.Round) and jrsonnet both return the identity for these inputs;sjsonnet was the outlier.
Modification
MathModule.scalastd.roundwhen|x| >= 2^52or
xis NaN — at that magnitude every double is already an exactinteger (ULP >= 1.0), so returning
xunchanged is both correct andavoids the precision trap.
magnitudes; NaN / Infinity propagate naturally.
1e20) to the existing std.round/isEven/isOdd/isInteger/isDecimal
fixture.
Result
std.roundnow matches go-jsonnet and jrsonnet on every integer in[2^52, 2^53). The existingstd.round(0.5)==1/std.round(-0.5)==-1half-away-from-zero semantics are preserved.All 23 file tests plus 86 evaluator tests pass.
References
(roundTiesToEven)
math.Round: https://github.com/google/go-jsonnet/blob/master/builtin.go(
std.round"ties away from zero")