Skip to content

Commit 4c8936e

Browse files
teppeistjgq
authored andcommitted
Fix Math.hypot polyfill to call with zero/one params
Closes #2117 ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=231277270
1 parent f0ae19c commit 4c8936e

File tree

4 files changed

+20
-20
lines changed

4 files changed

+20
-20
lines changed

externs/es6.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,12 @@ Math.sign = function(value) {};
144144
Math.cbrt = function(value) {};
145145

146146
/**
147-
* @param {number} value1
148147
* @param {...number} var_args
149148
* @return {number}
150149
* @nosideeffects
151150
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/hypot
152151
*/
153-
Math.hypot = function(value1, var_args) {};
152+
Math.hypot = function(var_args) {};
154153

155154
/**
156155
* @param {number} value1

src/com/google/javascript/jscomp/js/es6/math/hypot.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,32 @@ $jscomp.polyfill('Math.hypot', function(orig) {
2424
*
2525
* <p>Polyfills the static function Math.hypot().
2626
*
27-
* @param {number} x Any number, or value that can be coerced to a number.
28-
* @param {number} y Any number, or value that can be coerced to a number.
29-
* @param {...*} var_args More numbers.
27+
* @param {...number} var_args Any number, or value that can be coerced to a
28+
* number.
3029
* @return {number} The square root of the sum of the squares.
3130
*/
32-
var polyfill = function(x, y, var_args) {
33-
// Make the type checker happy.
34-
x = Number(x);
35-
y = Number(y);
36-
var i, z, sum;
31+
var polyfill = function(var_args) {
32+
if (arguments.length < 2) {
33+
return arguments.length ? Math.abs(arguments[0]) : 0;
34+
}
35+
36+
var i, z, sum, max;
3737
// Note: we need to normalize the numbers in case of over/underflow.
38-
var max = Math.max(Math.abs(x), Math.abs(y));
39-
for (i = 2; i < arguments.length; i++) {
38+
for (max = 0, i = 0; i < arguments.length; i++) {
4039
max = Math.max(max, Math.abs(arguments[i]));
4140
}
41+
// TODO(sdh): Document where these constants come from.
4242
if (max > 1e100 || max < 1e-100) {
4343
if (!max) return max; // Handle 0 and NaN before trying to divide.
44-
x = x / max;
45-
y = y / max;
46-
sum = x * x + y * y;
47-
for (i = 2; i < arguments.length; i++) {
44+
sum = 0;
45+
for (i = 0; i < arguments.length; i++) {
4846
z = Number(arguments[i]) / max;
4947
sum += z * z;
5048
}
5149
return Math.sqrt(sum) * max;
5250
} else {
53-
sum = x * x + y * y;
54-
for (i = 2; i < arguments.length; i++) {
51+
sum = 0;
52+
for (i = 0; i < arguments.length; i++) {
5553
z = Number(arguments[i]);
5654
sum += z * z;
5755
}

src/com/google/javascript/jscomp/resources.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

test/com/google/javascript/jscomp/runtime_tests/polyfill_tests/math_hypot_test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ const testSuite = goog.require('goog.testing.testSuite');
2121

2222
testSuite({
2323
testHypot() {
24-
assertRoughlyEquals(5, Math.hypot(3, 4), 1e-10);
24+
assertEquals(0, Math.hypot());
25+
assertEquals(3, Math.hypot(3));
26+
assertEquals(3, Math.hypot(-3));
27+
2528
assertRoughlyEquals(5, Math.hypot(-3, 4), 1e-10);
2629
assertRoughlyEquals(13, Math.hypot(5, 12), 1e-10);
2730
assertRoughlyEquals(13, Math.hypot(5, -12), 1e-10);

0 commit comments

Comments
 (0)