Debian Codegraph @

third-party/boost-math/include/boost/math/ccmath/fdim.hpp

raw
1 // (C) Copyright Matt Borland 2022.
2 // Use, modification and distribution are subject to the
3 // Boost Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6 #ifndef BOOST_MATH_CCMATH_FDIM_HPP
7 #define BOOST_MATH_CCMATH_FDIM_HPP
8
9 #include <boost/math/ccmath/detail/config.hpp>
10
11 #ifdef BOOST_MATH_NO_CCMATH
12 #error "The header <boost/math/fdim.hpp> can only be used in C++17 and later."
13 #endif
14
15 #include <boost/math/tools/promotion.hpp>
16 #include <boost/math/ccmath/isnan.hpp>
17
18 namespace boost::math::ccmath {
19
20 namespace detail {
21
22 template <typename T>
23 constexpr T fdim_impl(const T x, const T y) noexcept
24 {
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 else
34 {
35 return x - y;
36 }
37 }
38
39 } // Namespace detail
40
41 template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>
42 constexpr Real fdim(Real x, Real y) noexcept
43 {
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 else
58 {
59 using std::fdim;
60 return fdim(x, y);
61 }
62 }
63
64 template <typename T1, typename T2>
65 constexpr auto fdim(T1 x, T2 y) noexcept
66 {
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 else
73 {
74 using std::fdim;
75 return fdim(x, y);
76 }
77 }
78
79 constexpr float fdimf(float x, float y) noexcept
80 {
81 return boost::math::ccmath::fdim(x, y);
82 }
83
84 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
85 constexpr long double fdiml(long double x, long double y) noexcept
86 {
87 return boost::math::ccmath::fdim(x, y);
88 }
89 #endif
90
91 } // Namespace boost::math::ccmath
92
93 #endif // BOOST_MATH_CCMATH_FDIM_HPP