Skip to content

Math.max starts from -Infinity and Math.min from +Infinity - #31

Merged
ASDAlexander77 merged 1 commit into
mainfrom
fix-math-max-min-seeds
Oct 11, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
fix-math-max-min-seeds

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Problem

Math.max started its running maximum at Number.MIN_VALUE, which is the smallest positive double, not the most negative one. So any all-negative input returned 2.2e-308:

print(Math.max(-3, -5));   // 2.22507e-308, expected -3

Math.min started at Number.MAX_VALUE, so Math.min() and Math.min(Infinity) returned 1.79e308 instead of Infinity.

Reported in TypeScriptCompiler docs/bugs/native-ts-graphics-findings.md (issue 5, a camera pitch clamp broke).

Fix

Start from -Infinity / +Infinity, and return NaN if any argument is NaN, as JavaScript does.

Why tests/math.ts didn't catch it

tests/math.ts:107 checks isEq(Math.max(-1, -7), -1). But isEq(x: any, y: any) uses !==, and the compiler answered false for an any number compared with an any integer. Compiler PR ASDAlexander77/TypeScriptCompiler#535 fixes that; with it, tests/math.ts reports this bug. The new tests/math_max_min.ts uses typed comparisons, so it fails on the old library with either compiler.

Testing

tests.ps1 release compile + JIT (gc), with a compiler built from current main plus #535: 163/163 pass on both. The new test fails against the current library (assertion failed: Math.max(-3, -5)).

🤖 Generated with Claude Code

Math.max started from Number.MIN_VALUE, the smallest positive number, so
the maximum of negative numbers was 2.2e-308 (Math.max(-3, -5)).
Math.min started from Number.MAX_VALUE, so Math.min() and
Math.min(Infinity) were 1.79e308. Both now start from the infinities, as
in JavaScript, and a NaN argument makes the result NaN.

tests/math.ts already checks Math.max(-1, -7), but through an `any`
helper whose `!==` could not see the difference (compiler PR #535). The
new tests/math_max_min.ts checks with typed comparisons.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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