Debian Codegraph @

third-party/boost-math/include/boost/math/ccmath/hypot.hpp

raw
1 // (C) Copyright John Maddock 2005-2021.
2 // (C) Copyright Matt Borland 2021.
3 // Use, modification and distribution are subject to the
4 // Boost Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7 #ifndef BOOST_MATH_CCMATH_HYPOT_HPP
8 #define BOOST_MATH_CCMATH_HYPOT_HPP
9
10 #include <boost/math/ccmath/detail/config.hpp>
11
12 #ifdef BOOST_MATH_NO_CCMATH
13 #error "The header <boost/math/hypot.hpp> can only be used in C++17 and later."
14 #endif
15
16 #include <array>
17 #include <boost/math/tools/config.hpp>
18 #include <boost/math/tools/promotion.hpp>
19 #include <boost/math/ccmath/sqrt.hpp>
20 #include <boost/math/ccmath/abs.hpp>
21 #include <boost/math/ccmath/isinf.hpp>
22 #include <boost/math/ccmath/isnan.hpp>
23 #include <boost/math/ccmath/detail/swap.hpp>
24
25 namespace boost::math::ccmath {
26
27 namespace detail {
28
29 template <typename T>
30 constexpr T hypot_impl(T x, T y) noexcept
31 {
32 x = boost::math::ccmath::abs(x);
33 y = boost::math::ccmath::abs(y);
34
35 if (y > x)
36 {
37 boost::math::ccmath::detail::swap(x, y);
38 }
39
40 if(x * std::numeric_limits<T>::epsilon() >= y)
41 {
42 return x;
43 }
44
45 T rat = y / x;
46 return x * boost::math::ccmath::sqrt(1 + rat * rat);
47 }
48
49 } // Namespace detail
50
51 template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>
52 constexpr Real hypot(Real x, Real y) noexcept
53 {
54 if(BOOST_MATH_IS_CONSTANT_EVALUATED(x))
55 {
56 if (boost::math::ccmath::abs(x) == static_cast<Real>(0))
57 {
58 return boost::math::ccmath::abs(y);
59 }
60 else if (boost::math::ccmath::abs(y) == static_cast<Real>(0))
61 {
62 return boost::math::ccmath::abs(x);
63 }
64 // Return +inf even if the other argument is NaN
65 else if (boost::math::ccmath::isinf(x) || boost::math::ccmath::isinf(y))
66 {
67 return std::numeric_limits<Real>::infinity();
68 }
69 else if (boost::math::ccmath::isnan(x))
70 {
71 return x;
72 }
73 else if (boost::math::ccmath::isnan(y))
74 {
75 return y;
76 }
77
78 return boost::math::ccmath::detail::hypot_impl(x, y);
79 }
80 else
81 {
82 using std::hypot;
83 return hypot(x, y);
84 }
85 }
86
87 template <typename T1, typename T2>
88 constexpr auto hypot(T1 x, T2 y) noexcept
89 {
90 if(BOOST_MATH_IS_CONSTANT_EVALUATED(x))
91 {
92 using promoted_type = boost::math::tools::promote_args_t<T1, T2>;
93 return boost::math::ccmath::hypot(static_cast<promoted_type>(x), static_cast<promoted_type>(y));
94 }
95 else
96 {
97 using std::hypot;
98 return hypot(x, y);
99 }
100 }
101
102 constexpr float hypotf(float x, float y) noexcept
103 {
104 return boost::math::ccmath::hypot(x, y);
105 }
106
107 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
108 constexpr long double hypotl(long double x, long double y) noexcept
109 {
110 return boost::math::ccmath::hypot(x, y);
111 }
112 #endif
113
114 } // Namespaces
115
116 #endif // BOOST_MATH_CCMATH_HYPOT_HPP