-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Conversation
lgtm |
Internal review was more involved than I expected. Going to back-burner this for now. |
As I mentioned in the internal review, I don't think we want to change the type signature but it should be compatible with the actual implementation and I suggest we use V8's. Which looks like: function MathHypot(x, y) { // Function length is 2.
var length = arguments.length;
var args = new Array(length);
var max = 0;
for (var i = 0; i < length; i++) {
var n = arguments[i];
if (typeof n !== "number") n = Number(n);
if (n === Infinity || n === -Infinity) return Infinity;
n = Math.abs(n);
if (n > max) max = n;
args[i] = n;
}
// Kahan summation to avoid rounding errors.
// Normalize the numbers to the largest one to avoid overflow.
if (max === 0) max = 1;
var sum = 0;
var compensation = 0;
for (var i = 0; i < length; i++) {
var n = args[i] / max;
var summand = n * n - compensation;
var preliminary = sum + summand;
compensation = (preliminary - sum) - summand;
sum = preliminary;
}
return Math.sqrt(sum) * max;
}; |
I changed my mind, this function isn't used enough for it to valuable to have a tighter signature. |
@MatrixFrog are you proceeding with this? |
I do not know if I should fix this PR ... |
Probably not. |
@MatrixFrog @brad4d @concavelenz any updates? |
Created internal Google issue b/121320889 @concavelenz could you determine whether we should accept or reject this PR? |
@@ -147,7 +147,6 @@ Math.sign = function(value) {}; | |||
Math.cbrt = function(value) {}; | |||
|
|||
/** | |||
* @param {number} value1 | |||
* @param {...number} var_args |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
@concavelenz has confirmed to me that he is OK with accepting this PR. |
I've imported this PR and sent it for internal testing and review. |
I submitted the internal version of this change a few minutes ago. |
@brad4d thanks! |
A compat-table test fails due to this issue.