Skip to content

Pool a Float literal by its bits, not by its value - #433

Merged
tannevaled merged 1 commit into
mainfrom
fix/float-literal-pooling
Aug 23, 2026
Merged

tannevaled merged 1 commit into
mainfrom
fix/float-literal-pooling

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

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.

p 0.0
p(-0.0)     # printed 0.0   — wrong
p(-0.0)
p 0.0       # printed -0.0  — wrong

-0.0 == 0.0 is true and the two hash alike, so whichever the file mentioned first won both lines. The sign is not cosmetic: 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 constant slot on every mention.

Keying object.Float on math.Float64bits pools 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 on addConst — pins that 0.0 and -0.0 take 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.
    --- FAIL: TestAddConstPoolsFloatsByBits
        0.0 and -0.0 share slot 0; they are equal but not the same number
    
  • internal/vm, end-to-end on what the lines actually print:
    src="p 0.0\np(-0.0)"   got="0.0\n0.0\n"    want="0.0\n-0.0\n"
    src="p(-0.0)\np 0.0"   got="-0.0\n-0.0\n"  want="-0.0\n0.0\n"
    src="p [0.0, -0.0]"     got="[0.0, 0.0]\n"  want="[0.0, -0.0]\n"
    

Full ./internal/vm/ and ./internal/compiler/ green.

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>
@tannevaled
tannevaled merged commit 9ae5928 into main Aug 23, 2026
7 checks passed
@tannevaled
tannevaled deleted the fix/float-literal-pooling branch August 24, 2026 08:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant