es.math.hypot.js 985 B

1234567891011121314151617181920212223242526272829303132333435
  1. var $ = require('../internals/export');
  2. // eslint-disable-next-line es-x/no-math-hypot -- required for testing
  3. var $hypot = Math.hypot;
  4. var abs = Math.abs;
  5. var sqrt = Math.sqrt;
  6. // Chrome 77 bug
  7. // https://bugs.chromium.org/p/v8/issues/detail?id=9546
  8. var BUGGY = !!$hypot && $hypot(Infinity, NaN) !== Infinity;
  9. // `Math.hypot` method
  10. // https://tc39.es/ecma262/#sec-math.hypot
  11. $({ target: 'Math', stat: true, forced: BUGGY }, {
  12. // eslint-disable-next-line no-unused-vars -- required for `.length`
  13. hypot: function hypot(value1, value2) {
  14. var sum = 0;
  15. var i = 0;
  16. var aLen = arguments.length;
  17. var larg = 0;
  18. var arg, div;
  19. while (i < aLen) {
  20. arg = abs(arguments[i++]);
  21. if (larg < arg) {
  22. div = larg / arg;
  23. sum = sum * div * div + 1;
  24. larg = arg;
  25. } else if (arg > 0) {
  26. div = arg / larg;
  27. sum += div * div;
  28. } else sum += arg;
  29. }
  30. return larg === Infinity ? Infinity : larg * sqrt(sum);
  31. }
  32. });