fix(double): correct round for signed zero, the half boundary, and large integers - #3939
Merged
Merged
Conversation
…rge integers Closes moonbitlang#3912 Signed-off-by: 林晨 (Leo Cheng) <leo-cheng@vip.qq.com>
| (self + 0.5).floor() | ||
| // `(self + 0.5).floor()` alone drops the sign of zero, crosses the half | ||
| // boundary for `nextDown(0.5)`, and perturbs integers in `[2^52, 2^53)`. | ||
| if self != self || self.abs() >= 4503599627370496.0 { |
Contributor
|
others lgtm. Thank you for your working ^_^ |
…the sign-losing branch); add the +0.0 bit test in the native and wasm paths Signed-off-by: 林晨 (Leo Cheng) <chengkelfan@qq.com>
…d test (moon fmt) Signed-off-by: 林晨 (Leo Cheng) <chengkelfan@qq.com>
Contributor
Author
|
Addressed — thanks for catching that. |
Kaida-Amethyst
approved these changes
Aug 4, 2026
Contributor
|
Good Job |
Kaida-Amethyst
enabled auto-merge (squash)
August 4, 2026 08:18
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.
Double::roundwas(self + 0.5).floor()(in both the generic and wasm files), which breaks in three ways on native/wasm-gc:round(-0.0)and values in(-0.5, 0)lose the sign and return+0.0.round(nextDown(0.5))rounds up to1.0becausenextDown(0.5) + 0.5rounds to1.0before the floor.[2^52, 2^53)get perturbed becausex + 0.5is no longer exact there.Reworked both to match the JS target (
Math.round): pass NaN / infinities /|x| >= 2^52through unchanged, map(0, 0.5)to+0and[-0.5, 0]to-0, and otherwisefloor(x + 0.5). The existing test compared with==, which hides the sign of zero, so I added bit-level checks for the three cases.Closes #3912