From 66f1ac6bca1a36f856547be91f010faad02da420 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sun, 11 Oct 2026 12:55:50 +0100 Subject: [PATCH] Math.max starts from -Infinity and Math.min from +Infinity 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 --- src/lib.ts | 20 ++++++++++++++++---- tests/math_max_min.ts | 22 ++++++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 tests/math_max_min.ts diff --git a/src/lib.ts b/src/lib.ts index af7e9eb..9a87efd 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -1550,8 +1550,15 @@ export static class Math { * @param values Numeric expressions to be evaluated. */ public max(...values: number[]): number { - let m = Number.MIN_VALUE; - for (const v of values) if (v > m) m = v; + // -Infinity, not Number.MIN_VALUE: that is the smallest positive number, so the maximum of + // negative numbers came out as it. With no arguments the result is -Infinity, and any NaN + // makes it NaN. + let m = Number.NEGATIVE_INFINITY; + for (const v of values) { + if (isNaN(v)) return Number.NaN; + if (v > m) m = v; + } + return m; } @@ -1560,8 +1567,13 @@ export static class Math { * @param values Numeric expressions to be evaluated. */ public min(...values: number[]): number { - let m = Number.MAX_VALUE; - for (const v of values) if (v < m) m = v; + // +Infinity, not Number.MAX_VALUE, so that Math.min() and Math.min(Infinity) are Infinity + let m = Number.POSITIVE_INFINITY; + for (const v of values) { + if (isNaN(v)) return Number.NaN; + if (v < m) m = v; + } + return m; } diff --git a/tests/math_max_min.ts b/tests/math_max_min.ts new file mode 100644 index 0000000..10680e6 --- /dev/null +++ b/tests/math_max_min.ts @@ -0,0 +1,22 @@ +// Math.max started from Number.MIN_VALUE, the smallest positive number, so the maximum of negative +// numbers was 2.2e-308. Checked with typed comparisons rather than an `any` helper, so the test +// does not depend on how `any` values compare. + +function main() { + const m = Math.max(-3, -5); + assert(m == -3, "Math.max(-3, -5)"); + assert(Math.max(-1, -7) == -1, "Math.max(-1, -7)"); + assert(Math.max(-2.5) == -2.5, "Math.max(-2.5)"); + assert(Math.max(1, 7) == 7, "Math.max(1, 7)"); + assert(Math.max() == Number.NEGATIVE_INFINITY, "Math.max()"); + assert(Math.max(Number.NEGATIVE_INFINITY, -1e308) == -1e308, "Math.max(-Infinity, -1e308)"); + assert(isNaN(Math.max(1, Number.NaN, 3)), "Math.max(1, NaN, 3)"); + + assert(Math.min(3, 5) == 3, "Math.min(3, 5)"); + assert(Math.min(-1, -7) == -7, "Math.min(-1, -7)"); + assert(Math.min() == Number.POSITIVE_INFINITY, "Math.min()"); + assert(Math.min(Number.POSITIVE_INFINITY) == Number.POSITIVE_INFINITY, "Math.min(Infinity)"); + assert(isNaN(Math.min(Number.NaN, 1)), "Math.min(NaN, 1)"); + + print("ALL DONE"); +}