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