83 lines · plain
1// (C) Copyright John Maddock 2005-2006.2// Use, modification and distribution are subject to the3// Boost Software License, Version 1.0. (See accompanying file4// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)5 6#ifndef BOOST_MATH_HYPOT_INCLUDED7#define BOOST_MATH_HYPOT_INCLUDED8 9#ifdef _MSC_VER10#pragma once11#endif12 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 20namespace boost{ namespace math{ namespace detail{21 22template <class T, class Policy>23BOOST_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_USING29 30 x = fabs(x);31 y = fabs(y);32 33#ifdef _MSC_VER34#pragma warning(push)35#pragma warning(disable: 4127)36#endif37 // special case, see C99 Annex F:38 if(boost::math::numeric_limits<T>::has_infinity39 && ((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_VER43#pragma warning(pop)44#endif45 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 58template <class T1, class T2>59BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T1, T2>::type60 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 67template <class T1, class T2, class Policy>68BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T1, T2>::type69 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 math77} // namespace boost78 79#endif // BOOST_MATH_HYPOT_INCLUDED80 81 82 83