2
0
mirror of https://github.com/boostorg/math.git synced 2026-01-19 04:22:09 +00:00

Add 3-arg hypot impl

This commit is contained in:
Matt Borland
2025-09-08 13:08:22 +02:00
parent ab265d474f
commit 5ba3c38d1e

View File

@@ -16,6 +16,7 @@
#include <boost/math/tools/type_traits.hpp>
#include <boost/math/policies/error_handling.hpp>
#include <boost/math/special_functions/math_fwd.hpp>
#include <boost/math/tools/utility.hpp>
namespace boost{ namespace math{ namespace detail{
@@ -53,6 +54,42 @@ BOOST_MATH_GPU_ENABLED T hypot_imp(T x, T y, const Policy& pol)
return x * sqrt(1 + rat*rat);
} // template <class T> T hypot(T x, T y)
template <class T, class Policy>
BOOST_MATH_GPU_ENABLED T hypot_impl(T x, T y, T z, const Policy& pol)
{
BOOST_MATH_STD_USING
x = fabs(x);
y = fabs(y);
z = fabs(z);
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4127)
#endif
// special case, see C99 Annex F:
BOOST_MATH_IF_CONSTEXPR (boost::math::numeric_limits<T>::has_infinity)
{
if(((x == boost::math::numeric_limits<T>::infinity())
|| (y == boost::math::numeric_limits<T>::infinity())
|| (z == boost::math::numeric_limits<T>::infinity())))
return policies::raise_overflow_error<T>("boost::math::hypot<%1%>(%1%,%1%,%1%)", nullptr, pol);
}
#ifdef _MSC_VER
#pragma warning(pop)
#endif
const T a {(max)((max)(x, y), z)};
const T x_div_a {x / a};
const T y_div_a {y / a};
const T z_div_a {z / a};
return a * sqrt(x_div_a * x_div_a
+ y_div_a * y_div_a
+ z_div_a * z_div_a);
}
}
template <class T1, class T2>
@@ -73,6 +110,28 @@ BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T1, T2>::type
static_cast<result_type>(x), static_cast<result_type>(y), pol);
}
template <class T1, class T2, class T3, boost::math::enable_if_t<!policies::is_policy<T3>::value, bool> = true>
BOOST_MATH_GPU_ENABLED tools::promote_args_t<T1, T2, T3>
hypot(T1 x, T2 y, T3 z)
{
using result_type = tools::promote_args_t<T1, T2, T3>;
return detail::hypot_imp(static_cast<result_type>(x),
static_cast<result_type>(y),
static_cast<result_type>(z),
policies::policy<>());
}
template <class T1, class T2, class T3, class Policy>
BOOST_MATH_GPU_ENABLED tools::promote_args_t<T1, T2, T3>
hypot(T1 x, T2 y, T3 z, const Policy& pol)
{
using result_type = tools::promote_args_t<T1, T2, T3>;
return detail::hypot_imp(static_cast<result_type>(x),
static_cast<result_type>(y),
static_cast<result_type>(z),
pol);
}
} // namespace math
} // namespace boost