Repository navigation
Pool a Float literal by its bits, not by its value - #433
Merged
Merged
Conversation
Two floats can be equal without being the same number, and one can be
unequal to itself. The constant pool asked the wrong question of both:
- -0.0 == 0.0 is true and the two hash alike, so the second literal in
a file was handed the first one's slot. `p 0.0` then `p(-0.0)`
printed 0.0 twice; reversing the two lines printed -0.0 twice.
Whichever the file happened to mention first won, and the sign is
load-bearing — 1.0 / -0.0 is -Infinity.
- NaN is the mirror image: equal to nothing, itself included, so it
matched no entry and took a fresh slot on every mention.
Keying Floats on their bits pools both correctly and leaves every other
constant on the value-keyed map. The whitebox test pins the slots and the
end-to-end one pins what the two lines print; both fail without the
change.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
Two floats can be equal without being the same number, and one can be unequal to itself. The compiler's constant pool is a
map[object.Value]int, so it asked the wrong question of both.A file's second zero was handed the first one's slot.
-0.0 == 0.0is true and the two hash alike, so whichever the file mentioned first won both lines. The sign is not cosmetic:1.0 / -0.0is-Infinity.NaN is the mirror image — equal to nothing, itself included — so it matched no entry and took a fresh constant slot on every mention.
Keying
object.Floatonmath.Float64bitspools both correctly. Every other constant stays on the value-keyed map, which is right for them.Proof
Two tests, both of which fail without the change:
internal/compiler, whitebox onaddConst— pins that0.0and-0.0take different slots holding the right sign each, that the same float asked twice keeps one slot, that two NaNs share a slot, and that a non-Float still pools by value.internal/vm, end-to-end on what the lines actually print:Full
./internal/vm/and./internal/compiler/green.