brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.9 KiB · 34dd5ab Raw
117 lines · plain
1//  (C) Copyright John Maddock 2005-2021.2//  (C) Copyright Matt Borland 2021.3//  Use, modification and distribution are subject to the4//  Boost Software License, Version 1.0. (See accompanying file5//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)6 7#ifndef BOOST_MATH_CCMATH_HYPOT_HPP8#define BOOST_MATH_CCMATH_HYPOT_HPP9 10#include <boost/math/ccmath/detail/config.hpp>11 12#ifdef BOOST_MATH_NO_CCMATH13#error "The header <boost/math/hypot.hpp> can only be used in C++17 and later."14#endif15 16#include <array>17#include <boost/math/tools/config.hpp>18#include <boost/math/tools/promotion.hpp>19#include <boost/math/ccmath/sqrt.hpp>20#include <boost/math/ccmath/abs.hpp>21#include <boost/math/ccmath/isinf.hpp>22#include <boost/math/ccmath/isnan.hpp>23#include <boost/math/ccmath/detail/swap.hpp>24 25namespace boost::math::ccmath {26 27namespace detail {28 29template <typename T>30constexpr T hypot_impl(T x, T y) noexcept31{32    x = boost::math::ccmath::abs(x);33    y = boost::math::ccmath::abs(y);34 35    if (y > x)36    {37        boost::math::ccmath::detail::swap(x, y);38    }39 40    if(x * std::numeric_limits<T>::epsilon() >= y)41    {42        return x;43    }44 45    T rat = y / x;46    return x * boost::math::ccmath::sqrt(1 + rat * rat);47}48 49} // Namespace detail50 51template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>52constexpr Real hypot(Real x, Real y) noexcept53{54    if(BOOST_MATH_IS_CONSTANT_EVALUATED(x))55    {56        if (boost::math::ccmath::abs(x) == static_cast<Real>(0))57        {58            return boost::math::ccmath::abs(y);59        }60        else if (boost::math::ccmath::abs(y) == static_cast<Real>(0))61        {62            return boost::math::ccmath::abs(x);63        }64        // Return +inf even if the other argument is NaN65        else if (boost::math::ccmath::isinf(x) || boost::math::ccmath::isinf(y))66        {67            return std::numeric_limits<Real>::infinity();68        }69        else if (boost::math::ccmath::isnan(x))70        {71            return x;72        }73        else if (boost::math::ccmath::isnan(y))74        {75            return y;76        }77        78        return boost::math::ccmath::detail::hypot_impl(x, y);79    }80    else81    {82        using std::hypot;83        return hypot(x, y);84    }85}86 87template <typename T1, typename T2>88constexpr auto hypot(T1 x, T2 y) noexcept89{90    if(BOOST_MATH_IS_CONSTANT_EVALUATED(x))91    {92        using promoted_type = boost::math::tools::promote_args_t<T1, T2>;93        return boost::math::ccmath::hypot(static_cast<promoted_type>(x), static_cast<promoted_type>(y));94    }95    else96    {97        using std::hypot;98        return hypot(x, y);99    }100}101 102constexpr float hypotf(float x, float y) noexcept103{104    return boost::math::ccmath::hypot(x, y);105}106 107#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS108constexpr long double hypotl(long double x, long double y) noexcept109{110    return boost::math::ccmath::hypot(x, y);111}112#endif113 114} // Namespaces115 116#endif // BOOST_MATH_CCMATH_HYPOT_HPP117