Chris@16: // (C) Copyright John Maddock 2005-2006. Chris@16: // Use, modification and distribution are subject to the Chris@16: // Boost Software License, Version 1.0. (See accompanying file Chris@16: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: #ifndef BOOST_MATH_HYPOT_INCLUDED Chris@16: #define BOOST_MATH_HYPOT_INCLUDED Chris@16: Chris@16: #ifdef _MSC_VER Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include // for swap Chris@16: Chris@16: #ifdef BOOST_NO_STDC_NAMESPACE Chris@16: namespace std{ using ::sqrt; using ::fabs; } Chris@16: #endif Chris@16: Chris@16: namespace boost{ namespace math{ namespace detail{ Chris@16: Chris@16: template Chris@16: T hypot_imp(T x, T y, const Policy& pol) Chris@16: { Chris@16: // Chris@16: // Normalize x and y, so that both are positive and x >= y: Chris@16: // Chris@16: using std::fabs; using std::sqrt; // ADL of std names Chris@16: Chris@16: x = fabs(x); Chris@16: y = fabs(y); Chris@16: Chris@16: #ifdef BOOST_MSVC Chris@16: #pragma warning(push) Chris@16: #pragma warning(disable: 4127) Chris@16: #endif Chris@16: // special case, see C99 Annex F: Chris@16: if(std::numeric_limits::has_infinity Chris@16: && ((x == std::numeric_limits::infinity()) Chris@16: || (y == std::numeric_limits::infinity()))) Chris@16: return policies::raise_overflow_error("boost::math::hypot<%1%>(%1%,%1%)", 0, pol); Chris@16: #ifdef BOOST_MSVC Chris@16: #pragma warning(pop) Chris@16: #endif Chris@16: Chris@16: if(y > x) Chris@16: (std::swap)(x, y); Chris@16: Chris@16: if(x * tools::epsilon() >= y) Chris@16: return x; Chris@16: Chris@16: T rat = y / x; Chris@16: return x * sqrt(1 + rat*rat); Chris@16: } // template T hypot(T x, T y) Chris@16: Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename tools::promote_args::type Chris@16: hypot(T1 x, T2 y) Chris@16: { Chris@16: typedef typename tools::promote_args::type result_type; Chris@16: return detail::hypot_imp( Chris@16: static_cast(x), static_cast(y), policies::policy<>()); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename tools::promote_args::type Chris@16: hypot(T1 x, T2 y, const Policy& pol) Chris@16: { Chris@16: typedef typename tools::promote_args::type result_type; Chris@16: return detail::hypot_imp( Chris@16: static_cast(x), static_cast(y), pol); Chris@16: } Chris@16: Chris@16: } // namespace math Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_MATH_HYPOT_INCLUDED Chris@16: Chris@16: Chris@16: