From 169c23a9bb3e501ae33b944fe6a11357cb95513e Mon Sep 17 00:00:00 2001 From: teppeis Date: Mon, 31 Oct 2016 03:07:15 +0900 Subject: [PATCH 1/2] Fix Math.hypot to call with zero/one params --- externs/es6.js | 1 - .../javascript/jscomp/js/es6/math/hypot.js | 16 ++++++++++------ .../polyfill_tests/math_hypot_test.js | 5 ++++- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/externs/es6.js b/externs/es6.js index c87145cda60..b7d19d0f45b 100644 --- a/externs/es6.js +++ b/externs/es6.js @@ -147,7 +147,6 @@ Math.sign = function(value) {}; Math.cbrt = function(value) {}; /** - * @param {number} value1 * @param {...number} var_args * @return {number} * @nosideeffects diff --git a/src/com/google/javascript/jscomp/js/es6/math/hypot.js b/src/com/google/javascript/jscomp/js/es6/math/hypot.js index 36068024cef..407c3bad7c6 100644 --- a/src/com/google/javascript/jscomp/js/es6/math/hypot.js +++ b/src/com/google/javascript/jscomp/js/es6/math/hypot.js @@ -24,15 +24,19 @@ $jscomp.polyfill('Math.hypot', function(orig) { * *

Polyfills the static function Math.hypot(). * - * @param {number} x Any number, or value that can be coerced to a number. - * @param {number} y Any number, or value that can be coerced to a number. - * @param {...*} var_args More numbers. + * @param {...number} var_args Any number, or value that can be coerced to a number. * @return {number} The square root of the sum of the squares. */ - var polyfill = function(x, y, var_args) { + var polyfill = function(var_args) { + if (arguments.length === 0) { + return 0; + } else if (arguments.length === 1) { + return Math.abs(Number(arguments[0])); + } + // Make the type checker happy. - x = Number(x); - y = Number(y); + var x = Number(arguments[0]); + var y = Number(arguments[1]); var i, z, sum; // Note: we need to normalize the numbers in case of over/underflow. var max = Math.max(Math.abs(x), Math.abs(y)); diff --git a/test/com/google/javascript/jscomp/runtime_tests/polyfill_tests/math_hypot_test.js b/test/com/google/javascript/jscomp/runtime_tests/polyfill_tests/math_hypot_test.js index b76f0b11ffb..5950f7cea91 100644 --- a/test/com/google/javascript/jscomp/runtime_tests/polyfill_tests/math_hypot_test.js +++ b/test/com/google/javascript/jscomp/runtime_tests/polyfill_tests/math_hypot_test.js @@ -21,7 +21,10 @@ const testSuite = goog.require('goog.testing.testSuite'); testSuite({ testHypot() { - assertRoughlyEquals(5, Math.hypot(3, 4), 1e-10); + assertEquals(0, Math.hypot()); + assertEquals(3, Math.hypot(3)); + assertEquals(3, Math.hypot(-3)); + assertRoughlyEquals(5, Math.hypot(-3, 4), 1e-10); assertRoughlyEquals(13, Math.hypot(5, 12), 1e-10); assertRoughlyEquals(13, Math.hypot(5, -12), 1e-10); From 5832fbaccd0486c880d3675866d7e075de4b3fb3 Mon Sep 17 00:00:00 2001 From: teppeis Date: Thu, 10 Jan 2019 08:44:40 +0900 Subject: [PATCH 2/2] Delete value2 from params of Math.hypot --- externs/es6.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/externs/es6.js b/externs/es6.js index b7d19d0f45b..b8064e96e55 100644 --- a/externs/es6.js +++ b/externs/es6.js @@ -152,7 +152,7 @@ Math.cbrt = function(value) {}; * @nosideeffects * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/hypot */ -Math.hypot = function(value1, var_args) {}; +Math.hypot = function(var_args) {}; /** * @param {number} value1