94 lines · plain
1// (C) Copyright Matt Borland 2022.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_CCMATH_FDIM_HPP7#define BOOST_MATH_CCMATH_FDIM_HPP8 9#include <boost/math/ccmath/detail/config.hpp>10 11#ifdef BOOST_MATH_NO_CCMATH12#error "The header <boost/math/fdim.hpp> can only be used in C++17 and later."13#endif14 15#include <boost/math/tools/promotion.hpp>16#include <boost/math/ccmath/isnan.hpp>17 18namespace boost::math::ccmath {19 20namespace detail {21 22template <typename T>23constexpr T fdim_impl(const T x, const T y) noexcept24{25 if (x <= y)26 {27 return 0;28 }29 else if ((y < 0) && (x > (std::numeric_limits<T>::max)() + y))30 {31 return std::numeric_limits<T>::infinity();32 }33 else34 {35 return x - y;36 }37}38 39} // Namespace detail40 41template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>42constexpr Real fdim(Real x, Real y) noexcept43{44 if (BOOST_MATH_IS_CONSTANT_EVALUATED(x))45 {46 if (boost::math::ccmath::isnan(x))47 {48 return x;49 }50 else if (boost::math::ccmath::isnan(y))51 {52 return y;53 }54 55 return boost::math::ccmath::detail::fdim_impl(x, y);56 }57 else58 {59 using std::fdim;60 return fdim(x, y);61 }62}63 64template <typename T1, typename T2>65constexpr auto fdim(T1 x, T2 y) noexcept66{67 if (BOOST_MATH_IS_CONSTANT_EVALUATED(x))68 {69 using promoted_type = boost::math::tools::promote_args_t<T1, T2>;70 return boost::math::ccmath::fdim(promoted_type(x), promoted_type(y));71 }72 else73 {74 using std::fdim;75 return fdim(x, y);76 }77}78 79constexpr float fdimf(float x, float y) noexcept80{81 return boost::math::ccmath::fdim(x, y);82}83 84#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS85constexpr long double fdiml(long double x, long double y) noexcept86{87 return boost::math::ccmath::fdim(x, y);88}89#endif90 91} // Namespace boost::math::ccmath92 93#endif // BOOST_MATH_CCMATH_FDIM_HPP94