es.math.hypot.js 888 B

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