hypot
hypot(x,y) = sqrt(x * x + y * y)
Method :
If (assume round-to-nearest) z = x*x + y*y
has error less than sqrt(2)/2 ulp, than
sqrt(z) has error less than 1 ulp (exercise).
So, compute sqrt(x*x + y*y) with some care as
follows to get the error below 1 ulp:
Assume x > y > 0;
(if possible, set rounding to round-to-nearest)
1. if x > 2y use
x1*x1 + (y*y + (x2*(x + x1))) for x*x + y*y
where x1 = x with lower 32 bits cleared, x2 = x - x1; else
2. if x <= 2y use
t1*y1 + ((x-y) * (x-y) + (t1*y2 + t2*y))
where t1 = 2x with lower 32 bits cleared, t2 = 2x - t1,
y1= y with lower 32 bits chopped, y2 = y - y1.
NOTE: scaling may be necessary if some argument is too
large or too tiny
You might need to use setScale method before invoking this.
Content copied to clipboard
Special cases:
hypot(x,y) is INF if x or y is +INF or -INF; else
hypot(x,y) is NAN if x or y is NAN.
Content copied to clipboard
Accuracy:
hypot(x,y) returns sqrt(x^2 + y^2) with error less
than 1 ulp (unit in the last place)
Content copied to clipboard