Further refine hypot implementation

Thanks go to Fabian Giesen on Twitter for code review and advice.
This commit is contained in:
Justine Tunney 2021-02-21 12:53:01 -08:00
parent 4a5698b5c9
commit c6c9b5dfde
4 changed files with 66 additions and 18 deletions

View file

@ -22,10 +22,13 @@
* Returns euclidean distance.
*/
long double hypotl(long double a, long double b) {
long double r;
long double r, t;
if (isinf(a) || isinf(b)) return INFINITY;
if (isunordered(a, b)) return NAN;
if (!a) return 0;
a = fabsl(a);
b = fabsl(b);
if (a < b) t = b, b = a, a = t;
if (!a) return b;
r = b / a;
return fabsl(a) * sqrtl(1 + r * r);
return a * sqrtl(1 + r * r);
}