BAN: Implement more numerically stable hypot

This commit is contained in:
2026-07-09 09:54:57 +03:00
parent f6b91578ae
commit 2c340c5532

View File

@@ -2,6 +2,7 @@
#include <BAN/Limits.h> #include <BAN/Limits.h>
#include <BAN/Numbers.h> #include <BAN/Numbers.h>
#include <BAN/Swap.h>
#include <BAN/Traits.h> #include <BAN/Traits.h>
#include <float.h> #include <float.h>
@@ -470,7 +471,17 @@ namespace BAN::Math
template<floating_point T> template<floating_point T>
inline constexpr T hypot(T x, T y) inline constexpr T hypot(T x, T y)
{ {
return sqrt<T>(x * x + y * y); x = abs(x);
y = abs(y);
if (x < y)
swap(x, y);
if (y == (T)0.0)
return x;
const T r = y / x;
return x * sqrt<T>((T)1.0 + r * r);
} }
#ifdef BAN_MATH_POP_OPTIONS #ifdef BAN_MATH_POP_OPTIONS