es6.math.hypot.js 664 B

12345678910111213141516171819202122232425
  1. // 20.2.2.17 Math.hypot([value1[, value2[, … ]]])
  2. var $export = require('./_export');
  3. var abs = Math.abs;
  4. $export($export.S, 'Math', {
  5. hypot: function hypot(value1, value2) { // eslint-disable-line no-unused-vars
  6. var sum = 0;
  7. var i = 0;
  8. var aLen = arguments.length;
  9. var larg = 0;
  10. var arg, div;
  11. while (i < aLen) {
  12. arg = abs(arguments[i++]);
  13. if (larg < arg) {
  14. div = larg / arg;
  15. sum = sum * div * div + 1;
  16. larg = arg;
  17. } else if (arg > 0) {
  18. div = arg / larg;
  19. sum += div * div;
  20. } else sum += arg;
  21. }
  22. return larg === Infinity ? Infinity : larg * Math.sqrt(sum);
  23. }
  24. });