Skip to content

Fix Math.hypot polyfill to call with zero/one params #2117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions externs/es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,12 @@ Math.sign = function(value) {};
Math.cbrt = function(value) {};

/**
* @param {number} value1
* @param {...number} var_args
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to delete the value1 argument from the parameter list, too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brad4d Nice catch! I've fixed.

* @return {number}
* @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
Expand Down
16 changes: 10 additions & 6 deletions src/com/google/javascript/jscomp/js/es6/math/hypot.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,19 @@ $jscomp.polyfill('Math.hypot', function(orig) {
*
* <p>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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down