Skip to content

Math.max(-0, 0) is 0 and Math.min(0, -0) is -0, in either order - #104

Merged
ASDAlexander77 merged 1 commit into
mainfrom
math-max-min-signed-zero
Nov 17, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
math-max-min-signed-zero

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Bug

> and < don't tell -0 from +0, so Math.max/Math.min of the two zeros depended on argument order:

Call JavaScript Before
Math.max(-0, 0) 0 -0
Math.max(0, -0) 0 0
Math.min(0, -0) -0 0
Math.min(-0, 0) -0 -0

Fix

When the candidate and the current result are both zero, the sign decides: +0 is the larger. NaN handling and Math.max()/Math.min() with no arguments are unchanged.

Tests

  • tests/math_max_min.ts: every order of the two zeros, max(-0, -0), min(0, 0), and zeros against other numbers. The zeros are made at run time, because -0 written as a literal is the integer 0.
  • This test needs the compiler fix in Rest arguments that are all folded constants are a real array (regression from #895) TypeScriptCompiler#899 (merged). Without it, Math.max(nz, nz) with const nz = -0.0 * scale got a one-element array of garbage.
  • Local Windows run with the compiler at #899, in a clean clone: tests.ps1 release compile + jit for gc/rc/none/own all pass, and the declarations match. The debug pass was not run.

🤖 Generated with Claude Code

`>` and `<` do not tell -0 from +0, so the answer depended on the argument
order: Math.max(-0, 0) gave -0 and Math.min(0, -0) gave 0. With the two zeros
compared, +0 is the larger, as in JavaScript.

The test makes its zeros at run time (`-0` is the integer 0). It needs the
compiler fix for folded constants passed as rest arguments.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 8622780 into main Nov 17, 2026
6 of 10 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