# Math.hypot(1)

Allen Wirfs-Brock allen at wirfs-brock.com
Thu Aug 1 09:46:57 PDT 2013

```On Jul 31, 2013, at 9:25 AM, Jason Orendorff wrote:

> I think the spec says that
>
>    Math.hypot() ===> NaN
>    Math.hypot(1) ===> NaN
>    Math.hypot(1, 2, 2, 4) ===> 3
>    Math.hypot(3, 4, undefined) ===> NaN

No, default value assignment is trigger for arguments that have the actual value undefined.  So the last one is the same as Math.hypot(3,4,0)

>
> Is that correct?
>
> I'm basing this on paragraph 4 of clause 15, plus the first sentence
> of 15.8.2, plus the fact that Math.hypot() appears to "require" 2
> arguments.
>
> The last case illustrates that the default argument syntax used in the heading:
>
>    15.8.2.29 Math.hypot( value1 , value2, value3 = 0 )
>
> is not quite how the function behaves.

But it's supposed to behave that way.
>
> How about we change this function to take any number of arguments
> (like Math.max), convert them all using ToNumber, and compute the
> norm?
>
>    Math.hypot() ===> 0
>    Math.hypot(1) ===> 1
>    Math.hypot(1, 2, 2, 4) ===> 5
>
> Allowing 3 arguments but ignoring the 4th seems like a bit of a head
> fake, to me.

This was discussed at the March 2012 TC39 meetings and the consensus was what is currently in the spec.  Here is what the minutes say:

Discussion of hypot, hypot2.
hypot is the square root of the sum of squares and takes either two or
three arguments.
hypot2 is the sum of squares and takes either two or three arguments.
Waldemar: How is hypot2 better than just doing x*x + y*y?
Luke: It's just ergonomics.
General reluctance about the hypot2 name because it looks like the 2
means two arguments (as in atan2).  Some debate about other function
names (hypotSq? sumOfSquares?).
MarkM: How is hypot better than sqrt(x*x + y*y)?
It's potentially more efficient and more accurate.  It is widespread
in numeric libraries.
Consensus:  hypot will support just two or three arguments.  hypot2 dropped.   <<<<=====
Waldemar, MarkM:  Why not one or zero arguments?  It would be 0 for
zero arguments and abs for one argument.
Allen, DaveH:  If you pass one argument to hypot, you'll get NaN.
Luke:  2 or 3 is the 99% use case.
Waldemar:  2 or 3 arguments is the 99% use case for max.
Waldemar:  If it's not variadic and takes only 2 or 3 arguments,
you'll get silent mistakes.  If you pass in four arguments, you'll get
the hypot of the first three, and the last one will be silently