Skip to content

Commit

Permalink
Merge pull request #6537 from diyaayay/fix/6536
Browse files Browse the repository at this point in the history
removes hypot, changes function calls
  • Loading branch information
davepagurek authored Nov 9, 2023
2 parents 17304ce + 83ecc77 commit e9266b1
Showing 1 changed file with 3 additions and 45 deletions.
48 changes: 3 additions & 45 deletions src/math/calculation.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ p5.prototype.dist = function(...args) {
p5._validateParameters('dist', args);
if (args.length === 4) {
//2D
return hypot(args[2] - args[0], args[3] - args[1]);
return Math.hypot(args[2] - args[0], args[3] - args[1]);
} else if (args.length === 6) {
//3D
return hypot(args[3] - args[0], args[4] - args[1], args[5] - args[2]);
return Math.hypot(args[3] - args[0], args[4] - args[1], args[5] - args[2]);
}
};

Expand Down Expand Up @@ -341,7 +341,7 @@ p5.prototype.log = Math.log;
*/
p5.prototype.mag = function(x, y) {
p5._validateParameters('mag', arguments);
return hypot(x, y);
return Math.hypot(x, y);
};

/**
Expand Down Expand Up @@ -727,48 +727,6 @@ p5.prototype.sq = n => n * n;
*/
p5.prototype.sqrt = Math.sqrt;

// Calculate the length of the hypotenuse of a right triangle
// This won't under- or overflow in intermediate steps
// https://en.wikipedia.org/wiki/Hypot
function hypot(x, y, z) {
// Use the native implementation if it's available
if (typeof Math.hypot === 'function') {
return Math.hypot.apply(null, arguments);
}

// Otherwise use the V8 implementation
// https://github.com/v8/v8/blob/8cd3cf297287e581a49e487067f5cbd991b27123/src/js/math.js#L217
const length = arguments.length;
const args = [];
let max = 0;
for (let i = 0; i < length; i++) {
let n = arguments[i];
n = +n;
if (n === Infinity || n === -Infinity) {
return Infinity;
}
n = Math.abs(n);
if (n > max) {
max = n;
}
args[i] = n;
}

if (max === 0) {
max = 1;
}
let sum = 0;
let compensation = 0;
for (let j = 0; j < length; j++) {
const m = args[j] / max;
const summand = m * m - compensation;
const preliminary = sum + summand;
compensation = preliminary - sum - summand;
sum = preliminary;
}
return Math.sqrt(sum) * max;
}

/**
* Calculates the fractional part of a number. For example,
* `fract(12.34)` returns 0.34.
Expand Down

0 comments on commit e9266b1

Please sign in to comment.