Repository navigation
Math.max starts from -Infinity and Math.min from +Infinity - #31
Merged
Merged
Conversation
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>
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.
Problem
Math.maxstarted its running maximum atNumber.MIN_VALUE, which is the smallest positive double, not the most negative one. So any all-negative input returned 2.2e-308:Math.minstarted atNumber.MAX_VALUE, soMath.min()andMath.min(Infinity)returned 1.79e308 instead ofInfinity.Reported in TypeScriptCompiler
docs/bugs/native-ts-graphics-findings.md(issue 5, a camera pitch clamp broke).Fix
Start from
-Infinity/+Infinity, and returnNaNif any argument isNaN, as JavaScript does.Why tests/math.ts didn't catch it
tests/math.ts:107checksisEq(Math.max(-1, -7), -1). ButisEq(x: any, y: any)uses!==, and the compiler answered false for ananynumber compared with ananyinteger. Compiler PR ASDAlexander77/TypeScriptCompiler#535 fixes that; with it,tests/math.tsreports this bug. The newtests/math_max_min.tsuses typed comparisons, so it fails on the old library with either compiler.Testing
tests.ps1release 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