Skip to content

Commit 7e3d771

Browse files
authored
Merge pull request #6129 from pmarsh-scottlogic/main
fix min() and max() crashing on large arrays
2 parents 716a187 + 7d5200d commit 7e3d771

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/math/calculation.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -499,10 +499,18 @@ p5.prototype.map = function(n, start1, stop1, start2, stop2, withinBounds) {
499499
* @return {Number}
500500
*/
501501
p5.prototype.max = function(...args) {
502+
const findMax = arr => {
503+
let max = -Infinity;
504+
for (let x of arr) {
505+
max = x > max ? x : max;
506+
}
507+
return max;
508+
};
509+
502510
if (args[0] instanceof Array) {
503-
return Math.max.apply(null, args[0]);
511+
return findMax(args[0]);
504512
} else {
505-
return Math.max.apply(null, args);
513+
return findMax(args);
506514
}
507515
};
508516

@@ -547,10 +555,18 @@ p5.prototype.max = function(...args) {
547555
* @return {Number}
548556
*/
549557
p5.prototype.min = function(...args) {
558+
const findMin = arr => {
559+
let min = Infinity;
560+
for (let x of arr) {
561+
min = x < min ? x : min;
562+
}
563+
return min;
564+
};
565+
550566
if (args[0] instanceof Array) {
551-
return Math.min.apply(null, args[0]);
567+
return findMin(args[0]);
552568
} else {
553-
return Math.min.apply(null, args);
569+
return findMin(args);
554570
}
555571
};
556572

0 commit comments

Comments
 (0)