File indexing completed on 2025-01-30 09:45:15
0001
0002
0003
0004
0005
0006
0007 #ifndef BOOST_MATH_CCMATH_HYPOT_HPP
0008 #define BOOST_MATH_CCMATH_HYPOT_HPP
0009
0010 #include <boost/math/ccmath/detail/config.hpp>
0011
0012 #ifdef BOOST_MATH_NO_CCMATH
0013 #error "The header <boost/math/hypot.hpp> can only be used in C++17 and later."
0014 #endif
0015
0016 #include <array>
0017 #include <boost/math/tools/config.hpp>
0018 #include <boost/math/tools/promotion.hpp>
0019 #include <boost/math/ccmath/sqrt.hpp>
0020 #include <boost/math/ccmath/abs.hpp>
0021 #include <boost/math/ccmath/isinf.hpp>
0022 #include <boost/math/ccmath/isnan.hpp>
0023 #include <boost/math/ccmath/detail/swap.hpp>
0024
0025 namespace boost::math::ccmath {
0026
0027 namespace detail {
0028
0029 template <typename T>
0030 constexpr T hypot_impl(T x, T y) noexcept
0031 {
0032 x = boost::math::ccmath::abs(x);
0033 y = boost::math::ccmath::abs(y);
0034
0035 if (y > x)
0036 {
0037 boost::math::ccmath::detail::swap(x, y);
0038 }
0039
0040 if(x * std::numeric_limits<T>::epsilon() >= y)
0041 {
0042 return x;
0043 }
0044
0045 T rat = y / x;
0046 return x * boost::math::ccmath::sqrt(1 + rat * rat);
0047 }
0048
0049 }
0050
0051 template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>
0052 constexpr Real hypot(Real x, Real y) noexcept
0053 {
0054 if(BOOST_MATH_IS_CONSTANT_EVALUATED(x))
0055 {
0056 if (boost::math::ccmath::abs(x) == static_cast<Real>(0))
0057 {
0058 return boost::math::ccmath::abs(y);
0059 }
0060 else if (boost::math::ccmath::abs(y) == static_cast<Real>(0))
0061 {
0062 return boost::math::ccmath::abs(x);
0063 }
0064
0065 else if (boost::math::ccmath::isinf(x) || boost::math::ccmath::isinf(y))
0066 {
0067 return std::numeric_limits<Real>::infinity();
0068 }
0069 else if (boost::math::ccmath::isnan(x))
0070 {
0071 return x;
0072 }
0073 else if (boost::math::ccmath::isnan(y))
0074 {
0075 return y;
0076 }
0077
0078 return boost::math::ccmath::detail::hypot_impl(x, y);
0079 }
0080 else
0081 {
0082 using std::hypot;
0083 return hypot(x, y);
0084 }
0085 }
0086
0087 template <typename T1, typename T2>
0088 constexpr auto hypot(T1 x, T2 y) noexcept
0089 {
0090 if(BOOST_MATH_IS_CONSTANT_EVALUATED(x))
0091 {
0092 using promoted_type = boost::math::tools::promote_args_2_t<T1, T2>;
0093 return boost::math::ccmath::hypot(static_cast<promoted_type>(x), static_cast<promoted_type>(y));
0094 }
0095 else
0096 {
0097 using std::hypot;
0098 return hypot(x, y);
0099 }
0100 }
0101
0102 constexpr float hypotf(float x, float y) noexcept
0103 {
0104 return boost::math::ccmath::hypot(x, y);
0105 }
0106
0107 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
0108 constexpr long double hypotl(long double x, long double y) noexcept
0109 {
0110 return boost::math::ccmath::hypot(x, y);
0111 }
0112 #endif
0113
0114 }
0115
0116 #endif