Skip to content

fix(vm): keep the sign of -0.0 float results - #87

Merged
wolfy-j merged 2 commits into
mainfrom
fix/len-negative-zero
Nov 14, 2026
Merged

wolfy-j merged 2 commits into
mainfrom
fix/len-negative-zero

Conversation

@wolfy-j

@wolfy-j wolfy-j commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

Defect

A float result of -0.0 loses its sign:

print(1/#setmetatable({}, {__len = function() return -0.0 end}))  -- inf, Lua 5.4: -inf
print(1/(-(0.0)))                                                -- inf, Lua 5.4: -inf
local a, b = 0.0, -0.0  -- in one function: b reads back as 0.0

There are two causes:

  • lnumberToValue reuses a cached boxed number for any float that equals a small cached value. -0.0 == 0, so every float result of -0.0 came back as +0.0. This affects __len results, unary minus, *, /, //, %, ^ and underflow.
  • ConstIndex shares constant slots by Lua equality, so 0.0 and -0.0 in one function share a slot.

Preemption fuzzing on #60 found it: a preempted __len call returns -0 and the synchronous path returns 0.

Fix

  • fix(vm): the cache is used only when the float is bit-identical to the cached value (math.Float64bits). All 18 call sites in vm.go, registry.go and numeric_arithmetic.go go through this one function.
  • fix(compiler): sameConstant matches float constants bit for bit.

Tests

numeric_negative_zero_test.go:

  • TestFloatZeroKeepsSign checks the Lua 5.4 sign through 1/v, and math.type, for #/__len, unary minus, double unary minus, * (int and float), /, underflow, //, %, ^ and both constant-pool cases. 12 cases fail on main, and all pass after both commits.
  • TestNegativeZeroTableKey passes before and after: a -0.0 key is stored as integer key 0, as in Lua 5.4.

Already correct, and covered by the test: __add/__unm metamethod results, table values, constructors, a lone -0.0 constant, varargs, tonumber('-0.0'), string.format('%g'), and -0.0 + 0 (Lua gives +0).

Not in this PR

These are related Lua 5.4 differences outside the boxing path:

  • for i = -0.0, 0 starts at +0.0, because the Lua 5.1-style setup computes init - step + step. feat(vm): tick-budget preemption of coroutines #60 changes the same numeric-for code.
  • tostring(-0.0) gives "0" (Lua 5.4 gives "-0.0"), because LNumber.String formats integral floats as integers.
  • math.type(tonumber('-0')) is float (Lua 5.4: integer), and math.floor returns a float.

Verification

lnumberToValue shared a preallocated value for any float equal to a small
non-negative integer. -0.0 compares equal to 0, so every float result boxed
through it (OP_LEN metamethod results, unary minus, arithmetic, floor
division, modulo, power) turned -0.0 into 0.0, and 1/(-z) gave +inf. The
cache now applies only to floats bit-identical to a cached value.
ConstIndex reused a constant that compared equal under Lua equality, so a
function using both 0.0 and -0.0 shared one constant slot and returned
whichever appeared first for both. Float constants are now matched bit for
bit.
@wolfy-j
wolfy-j merged commit c6dc3a5 into main Nov 14, 2026
12 checks passed
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