Skip to content

fix(mathlib): define math.huge as positive infinity - #90

Merged
wolfy-j merged 1 commit into
mainfrom
fix/math-huge-inf
Nov 15, 2026
Merged

wolfy-j merged 1 commit into
mainfrom
fix/math-huge-inf

Conversation

@wolfy-j

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

Copy link
Copy Markdown
Contributor

Defect

math.huge is math.MaxFloat64 instead of +inf:

print(math.huge == 1/0)          -- false, Lua 5.4: true
print(math.huge * 2 == math.huge) -- false, Lua 5.4: true
print(1e308 * 10 > math.huge)    -- true, Lua 5.4: false

Code that uses math.huge as an unbounded limit (x < math.huge, math.min seeds, -math.huge sentinels) misbehaves for values that overflow to infinity.

Cause

OpenMath sets huge to LNumber(math.MaxFloat64). Lua defines it as HUGE_VAL.

Fix

math.huge is LNumber(math.Inf(1)). Nothing else depends on the old value: the stdlib type is number, and no constant folding or serialization path special-cases it.

Tests

mathlib_huge_test.go checks that math.huge is +Inf and a float, its equality with 1/0 and -1/0, arithmetic saturation, ordering against DBL_MAX, NaN from math.huge - math.huge, math.max/math.min, and numeric for loops with math.huge and -math.huge limits. It fails on main.

Verification

go vet ./..., golangci-lint v2.8.0 with the CI flags, go test ./... (full fixture corpus), the coverage collector run, and the race "other" shard (go test -race on all non-fixture tests and the non-regression fixture categories) pass. The regression-fixture race shards are left to CI.

tostring(math.huge) now prints +Inf, the same as tostring(1/0) already does; the float spelling in LNumber.String is a separate, known difference.

The runtime picks this up through a go-lua dependency bump after merge.

math.huge was math.MaxFloat64, so math.huge ~= 1/0, math.huge * 2 ~=
math.huge and values above DBL_MAX compared greater than math.huge. Lua
defines math.huge as HUGE_VAL, which is +inf.
@wolfy-j
wolfy-j requested a review from skhaz November 15, 2026 17:45
@wolfy-j
wolfy-j requested a review from skhaz November 15, 2026 18:48
@wolfy-j
wolfy-j merged commit 6e7bd6f into main Nov 15, 2026
12 checks passed
@wolfy-j
wolfy-j deleted the fix/math-huge-inf branch November 15, 2026 19:50
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.

2 participants