MathLib.hypot()

MathLib.hypot() calculates the square root of the sum of the squared arguments. MathLib.hypotab=a2+b2 The hypot implementation can handle any number of arguments. MathLib.hypota1an=i=1nai2

Examples

The length of the hypothenuse in a right triangle, where the catheti have length 3 and 4, can be calculated with: var length = MathLib.hypot(3, 4); // The value of length is now 5

Specification

MathLib.hypot accepts any number of arguments. The type of all arguments has to be "number".
  1. MathLib.hypot(x, y, ...) = +∞ if any argument is infinite
  2. MathLib.hypot(x, y, ...) = NaN if any argument is NaN, and none infinite
  3. MathLib.hypot(x, y, ...) = +0 if all arguments are ±0
  4. Otherwise MathLib.hypot(x, y, ...) = the square root of the sum of the squared arguments

Implementation

The implementation is based on the following identity x2+y2=x1+yx2 (with x>y). This implementation has the advantage that it has no problems with underflows and overflows like a naive implementation would have. If three or more arguments are passed, the computation is made with the following identity: hypot(a,b,c)=hypot(a,hypot(b,c))

Tests

Categories: n-ary Function