Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:45:14

0001 //  (C) Copyright Matt Borland 2022.
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_CCMATH_FDIM_HPP
0007 #define BOOST_MATH_CCMATH_FDIM_HPP
0008 
0009 #include <boost/math/ccmath/detail/config.hpp>
0010 
0011 #ifdef BOOST_MATH_NO_CCMATH
0012 #error "The header <boost/math/fdim.hpp> can only be used in C++17 and later."
0013 #endif
0014 
0015 #include <boost/math/tools/promotion.hpp>
0016 #include <boost/math/ccmath/isnan.hpp>
0017 
0018 namespace boost::math::ccmath {
0019 
0020 namespace detail {
0021 
0022 template <typename T>
0023 constexpr T fdim_impl(const T x, const T y) noexcept
0024 {
0025     if (x <= y)
0026     {
0027         return 0;
0028     }
0029     else if ((y < 0) && (x > (std::numeric_limits<T>::max)() + y))
0030     {
0031         return std::numeric_limits<T>::infinity();
0032     }
0033     else
0034     {
0035         return x - y;
0036     }
0037 }
0038 
0039 } // Namespace detail
0040 
0041 template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>
0042 constexpr Real fdim(Real x, Real y) noexcept
0043 {
0044     if (BOOST_MATH_IS_CONSTANT_EVALUATED(x))
0045     {
0046         if (boost::math::ccmath::isnan(x))
0047         {
0048             return x;
0049         }
0050         else if (boost::math::ccmath::isnan(y))
0051         {
0052             return y;
0053         }
0054 
0055         return boost::math::ccmath::detail::fdim_impl(x, y);
0056     }
0057     else
0058     {
0059         using std::fdim;
0060         return fdim(x, y);
0061     }
0062 }
0063 
0064 template <typename T1, typename T2>
0065 constexpr auto fdim(T1 x, T2 y) noexcept
0066 {
0067     if (BOOST_MATH_IS_CONSTANT_EVALUATED(x))
0068     {
0069         using promoted_type = boost::math::tools::promote_args_2_t<T1, T2>;
0070         return boost::math::ccmath::fdim(promoted_type(x), promoted_type(y));
0071     }
0072     else
0073     {
0074         using std::fdim;
0075         return fdim(x, y);
0076     }
0077 }
0078 
0079 constexpr float fdimf(float x, float y) noexcept
0080 {
0081     return boost::math::ccmath::fdim(x, y);
0082 }
0083 
0084 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
0085 constexpr long double fdiml(long double x, long double y) noexcept
0086 {
0087     return boost::math::ccmath::fdim(x, y);
0088 }
0089 #endif
0090 
0091 } // Namespace boost::math::ccmath
0092 
0093 #endif // BOOST_MATH_CCMATH_FDIM_HPP