Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:40:15

0001 //  (C) Copyright John Maddock 2005-2006.
0002 //  Use, modification and distribution are subject to the
0003 //  Boost Software License, Version 1.0. (See accompanying file
0004 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0005 
0006 #ifndef BOOST_MATH_HYPOT_INCLUDED
0007 #define BOOST_MATH_HYPOT_INCLUDED
0008 
0009 #ifdef _MSC_VER
0010 #pragma once
0011 #endif
0012 
0013 #include <boost/math/tools/config.hpp>
0014 #include <boost/math/tools/precision.hpp>
0015 #include <boost/math/policies/error_handling.hpp>
0016 #include <boost/math/special_functions/math_fwd.hpp>
0017 #include <algorithm> // for swap
0018 #include <cmath>
0019 
0020 namespace boost{ namespace math{ namespace detail{
0021 
0022 template <class T, class Policy>
0023 T hypot_imp(T x, T y, const Policy& pol)
0024 {
0025    //
0026    // Normalize x and y, so that both are positive and x >= y:
0027    //
0028    using std::fabs; using std::sqrt; // ADL of std names
0029 
0030    x = fabs(x);
0031    y = fabs(y);
0032 
0033 #ifdef _MSC_VER
0034 #pragma warning(push)
0035 #pragma warning(disable: 4127)
0036 #endif
0037    // special case, see C99 Annex F:
0038    if(std::numeric_limits<T>::has_infinity
0039       && ((x == std::numeric_limits<T>::infinity())
0040       || (y == std::numeric_limits<T>::infinity())))
0041       return policies::raise_overflow_error<T>("boost::math::hypot<%1%>(%1%,%1%)", nullptr, pol);
0042 #ifdef _MSC_VER
0043 #pragma warning(pop)
0044 #endif
0045 
0046    if(y > x)
0047       (std::swap)(x, y);
0048 
0049    if(x * tools::epsilon<T>() >= y)
0050       return x;
0051 
0052    T rat = y / x;
0053    return x * sqrt(1 + rat*rat);
0054 } // template <class T> T hypot(T x, T y)
0055 
0056 }
0057 
0058 template <class T1, class T2>
0059 inline typename tools::promote_args<T1, T2>::type
0060    hypot(T1 x, T2 y)
0061 {
0062    typedef typename tools::promote_args<T1, T2>::type result_type;
0063    return detail::hypot_imp(
0064       static_cast<result_type>(x), static_cast<result_type>(y), policies::policy<>());
0065 }
0066 
0067 template <class T1, class T2, class Policy>
0068 inline typename tools::promote_args<T1, T2>::type
0069    hypot(T1 x, T2 y, const Policy& pol)
0070 {
0071    typedef typename tools::promote_args<T1, T2>::type result_type;
0072    return detail::hypot_imp(
0073       static_cast<result_type>(x), static_cast<result_type>(y), pol);
0074 }
0075 
0076 } // namespace math
0077 } // namespace boost
0078 
0079 #endif // BOOST_MATH_HYPOT_INCLUDED
0080 
0081 
0082