MCPcopy Create free account
hub / github.com/Code-Bullet/Jump-King / hypot

Function hypot

libraries/p5.js:26744–26781  ·  view source on GitHub ↗
(x, y, z)

Source from the content-addressed store, hash-verified

26742// This won't under- or overflow in intermediate steps
26743// https://en.wikipedia.org/wiki/Hypot
26744function hypot(x, y, z) {
26745 // Use the native implementation if it's available
26746 if (typeof Math.hypot === 'function') {
26747 return Math.hypot.apply(null, arguments);
26748 }
26749
26750 // Otherwise use the V8 implementation
26751 // https://github.com/v8/v8/blob/8cd3cf297287e581a49e487067f5cbd991b27123/src/js/math.js#L217
26752 var length = arguments.length;
26753 var args = [];
26754 var max = 0;
26755 for (var i = 0; i < length; i++) {
26756 var n = arguments[i];
26757 n = +n;
26758 if (n === Infinity || n === -Infinity) {
26759 return Infinity;
26760 }
26761 n = Math.abs(n);
26762 if (n > max) {
26763 max = n;
26764 }
26765 args[i] = n;
26766 }
26767
26768 if (max === 0) {
26769 max = 1;
26770 }
26771 var sum = 0;
26772 var compensation = 0;
26773 for (var j = 0; j < length; j++) {
26774 var m = args[j] / max;
26775 var summand = m * m - compensation;
26776 var preliminary = sum + summand;
26777 compensation = (preliminary - sum) - summand;
26778 sum = preliminary;
26779 }
26780 return Math.sqrt(sum) * max;
26781}
26782
26783module.exports = p5;
26784

Callers 1

p5.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected