Debian Codegraph @

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

raw
1 // (C) Copyright John Maddock 2005-2006.
2 // Use, modification and distribution are subject to the
3 // Boost Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6 #ifndef BOOST_MATH_HYPOT_INCLUDED
7 #define BOOST_MATH_HYPOT_INCLUDED
8
9 #ifdef _MSC_VER
10 #pragma once
11 #endif
12
13 #include <boost/math/tools/config.hpp>
14 #include <boost/math/tools/precision.hpp>
15 #include <boost/math/tools/numeric_limits.hpp>
16 #include <boost/math/tools/type_traits.hpp>
17 #include <boost/math/policies/error_handling.hpp>
18 #include <boost/math/special_functions/math_fwd.hpp>
19
20 namespace boost{ namespace math{ namespace detail{
21
22 template <class T, class Policy>
23 BOOST_MATH_GPU_ENABLED T hypot_imp(T x, T y, const Policy& pol)
24 {
25 //
26 // Normalize x and y, so that both are positive and x >= y:
27 //
28 BOOST_MATH_STD_USING
29
30 x = fabs(x);
31 y = fabs(y);
32
33 #ifdef _MSC_VER
34 #pragma warning(push)
35 #pragma warning(disable: 4127)
36 #endif
37 // special case, see C99 Annex F:
38 if(boost::math::numeric_limits<T>::has_infinity
39 && ((x == boost::math::numeric_limits<T>::infinity())
40 || (y == boost::math::numeric_limits<T>::infinity())))
41 return policies::raise_overflow_error<T>("boost::math::hypot<%1%>(%1%,%1%)", nullptr, pol);
42 #ifdef _MSC_VER
43 #pragma warning(pop)
44 #endif
45
46 if(y > x)
47 BOOST_MATH_GPU_SAFE_SWAP(x, y);
48
49 if(x * tools::epsilon<T>() >= y)
50 return x;
51
52 T rat = y / x;
53 return x * sqrt(1 + rat*rat);
54 } // template <class T> T hypot(T x, T y)
55
56 }
57
58 template <class T1, class T2>
59 BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T1, T2>::type
60 hypot(T1 x, T2 y)
61 {
62 typedef typename tools::promote_args<T1, T2>::type result_type;
63 return detail::hypot_imp(
64 static_cast<result_type>(x), static_cast<result_type>(y), policies::policy<>());
65 }
66
67 template <class T1, class T2, class Policy>
68 BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T1, T2>::type
69 hypot(T1 x, T2 y, const Policy& pol)
70 {
71 typedef typename tools::promote_args<T1, T2>::type result_type;
72 return detail::hypot_imp(
73 static_cast<result_type>(x), static_cast<result_type>(y), pol);
74 }
75
76 } // namespace math
77 } // namespace boost
78
79 #endif // BOOST_MATH_HYPOT_INCLUDED
80
81
82