From bc3c7e6b47f02a2029cac2a264cfccadff241cdc Mon Sep 17 00:00:00 2001 From: ayushman1210 Date: Sun, 16 Nov 2025 13:34:07 +0530 Subject: [PATCH] fix made max min with infinity --- src/core/friendly_errors/validate_params.js | 16 +++++++------- test/unit/math/calculation.js | 24 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/core/friendly_errors/validate_params.js b/src/core/friendly_errors/validate_params.js index bf98dca98b..00e6e5fce5 100644 --- a/src/core/friendly_errors/validate_params.js +++ b/src/core/friendly_errors/validate_params.js @@ -342,15 +342,15 @@ if (typeof IS_MINIFIED !== 'undefined') { * @returns {Boolean} a boolean indicating whether input type is Number */ const isNumber = param => { - if (isNaN(parseFloat(param))) return false; - switch (typeof param) { - case 'number': - return true; - case 'string': - return !isNaN(param); - default: - return false; + // Treat numeric Infinity values as numbers + if (param === Infinity || param === -Infinity) return true; + // Real numbers (exclude NaN) + if (typeof param === 'number') return !isNaN(param); + // Accept numeric strings (including "Infinity"/"-Infinity") + if (typeof param === 'string') { + return param.trim() !== '' && !isNaN(Number(param)); } + return false; }; /** diff --git a/test/unit/math/calculation.js b/test/unit/math/calculation.js index 72d6cfe93e..3726121b4e 100644 --- a/test/unit/math/calculation.js +++ b/test/unit/math/calculation.js @@ -293,6 +293,18 @@ suite('Calculation', function() { result = myp5.max([10, 10]); assert.equal(result, 10); }); + test('should handle Infinity', function() { + result = myp5.max(3, Infinity); + assert.equal(result, Infinity); + }); + test('should handle -Infinity', function() { + result = myp5.max(3, -Infinity); + assert.equal(result, 3); + }); + test('should handle Infinity in array', function() { + result = myp5.max([3, Infinity, 5]); + assert.equal(result, Infinity); + }); }); suite('p5.prototype.min', function() { @@ -325,6 +337,18 @@ suite('Calculation', function() { result = myp5.min([10, 10]); assert.equal(result, 10); }); + test('should handle Infinity', function() { + result = myp5.min(Infinity, 3); + assert.equal(result, 3); + }); + test('should handle -Infinity', function() { + result = myp5.min(3, -Infinity); + assert.equal(result, -Infinity); + }); + test('should handle -Infinity in array', function() { + result = myp5.min([3, -Infinity, 5]); + assert.equal(result, -Infinity); + }); }); suite('p5.prototype.norm', function() {