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)
6 #ifndef BOOST_MATH_HYPOT_INCLUDED
7 #define BOOST_MATH_HYPOT_INCLUDED
13 #include <boost/math/tools/config.hpp>
14 #include <boost/math/tools/precision.hpp>
15 #include <boost/math/policies/error_handling.hpp>
16 #include <boost/math/special_functions/math_fwd.hpp>
17 #include <boost/config/no_tr1/cmath.hpp>
18 #include <algorithm> // for swap
20 #ifdef BOOST_NO_STDC_NAMESPACE
21 namespace std{ using ::sqrt; using ::fabs; }
24 namespace boost{ namespace math{ namespace detail{
26 template <class T, class Policy>
27 T hypot_imp(T x, T y, const Policy& pol)
30 // Normalize x and y, so that both are positive and x >= y:
32 using std::fabs; using std::sqrt; // ADL of std names
39 #pragma warning(disable: 4127)
41 // special case, see C99 Annex F:
42 if(std::numeric_limits<T>::has_infinity
43 && ((x == std::numeric_limits<T>::infinity())
44 || (y == std::numeric_limits<T>::infinity())))
45 return policies::raise_overflow_error<T>("boost::math::hypot<%1%>(%1%,%1%)", 0, pol);
53 if(x * tools::epsilon<T>() >= y)
57 return x * sqrt(1 + rat*rat);
58 } // template <class T> T hypot(T x, T y)
62 template <class T1, class T2>
63 inline typename tools::promote_args<T1, T2>::type
66 typedef typename tools::promote_args<T1, T2>::type result_type;
67 return detail::hypot_imp(
68 static_cast<result_type>(x), static_cast<result_type>(y), policies::policy<>());
71 template <class T1, class T2, class Policy>
72 inline typename tools::promote_args<T1, T2>::type
73 hypot(T1 x, T2 y, const Policy& pol)
75 typedef typename tools::promote_args<T1, T2>::type result_type;
76 return detail::hypot_imp(
77 static_cast<result_type>(x), static_cast<result_type>(y), pol);
83 #endif // BOOST_MATH_HYPOT_INCLUDED