Repository navigation
fix(std)!: round an f64 to the nearest whole number - #261
Open
siracozmen01 wants to merge 3 commits into
Open
siracozmen01 wants to merge 3 commits into
siracozmen01 wants to merge 3 commits into
Conversation
`round` of an `f64` was `floor(value + 0.5)`, and the addition is rounded before `floor` sees it: 0.49999999999999994 answered 1, and every odd whole number past 2^52 answered the even one above it. It now takes the whole number below and adds one when what is left above it is at least a half. A deterministic program observes this, so the profile moves from kest-det 4 to kest-det 5. Closes #258 BREAKING CHANGE: math.round of an f64 answers 0 for 0.49999999999999994 and an odd whole number past 2^52 answers itself, where they answered one more; round(-0.0) is -0.0 where it was 0.0. The profile is kest-det 5.
round(-0.0) is -0.0 and round(0.0) is 0.0; the old sum took nought under nought to nought.
This branch has not been deployed
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.
Problem
math.roundof anf64wasfloor(value + 0.5), and the addition is rounded beforefloorsees it:0.49999999999999994, thef64just under a half, answered 1;round(-0.0)answered0.0, losing the sign.deterministicprograms see these answers, on every machine alike.Change
lib/std/math.kest:round(f64)takesbelow = floor(value)and adds one whenvalue - below >= 0.5. Outside (-1/2, 0) that subtraction is exact. Inside it,belowis -1 and the subtraction rounds to at least a half only for values that are -1/2 or above, which is the documented rule. A half still goes up (round(2.5)is 3,round(-2.5)is -2). A whole number, an infinity and NaN answer themselves.round(f32)is unchanged: anf32plus a half inf64is exact.KEST_PROFILE_VERSIONmoves fromkest-det 4tokest-det 5, because a deterministic program observes the change. It is named everywhere the profile is:include/kest.h, the glossary,determinism.md,editions.md,getting-started.md,examples/slice/kest.projectandtools/fuzz.c.docs/user/determinism.mdhas a new "Rounding" paragraph that states the rule.test/regress/roundnear.kestcovers:f32(0.49999997, 8388609 and halves);round(-0.0)is -0.0 andround(0.0)is 0.0.BREAKING CHANGE:
math.roundof anf64answers 0 for 0.49999999999999994, and an odd whole number past 2^52 answers itself, where both answered one more.round(-0.0)is -0.0 where it was 0.0. The determinism profile iskest-det 5.Verification
roundnear.kestanswers 1 on main and 0 here. With the oldfloor(value + 0.5)put back into the built library, it answers 1 again.f64values, chosen around halves, whole numbers, 2^52 and 2^53 and nought: the new rule disagreed with round-half-up 0 times and the old one 6,691 times.ctest --preset dev -j4passes 21 of 21 (gate, regressions under every switch and compiled to C, conformance), as dotools/check-costs.shandtools/check-c.shover every example, library and bench file.test/conformance.traceis unchanged: no example's output moves.Closes #258