Debian Codegraph @

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

raw
1 // (C) Copyright Matt Borland 2021 - 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_REMAINDER_HPP
7 #define BOOST_MATH_CCMATH_REMAINDER_HPP
8
9 #include <boost/math/ccmath/detail/config.hpp>
10
11 #ifdef BOOST_MATH_NO_CCMATH
12 #error "The header <boost/math/remainder.hpp> can only be used in C++17 and later."
13 #endif
14
15 #include <cstdint>
16 #include <boost/math/tools/promotion.hpp>
17 #include <boost/math/ccmath/abs.hpp>
18 #include <boost/math/ccmath/isinf.hpp>
19 #include <boost/math/ccmath/isnan.hpp>
20 #include <boost/math/ccmath/isfinite.hpp>
21 #include <boost/math/ccmath/modf.hpp>
22
23 namespace boost::math::ccmath {
24
25 namespace detail {
26
27 template <typename T>
28 constexpr T remainder_impl(const T x, const T y)
29 {
30 T n = 0;
31
32 if (T fractional_part = boost::math::ccmath::modf((x / y), &n); fractional_part > static_cast<T>(1.0/2))
33 {
34 ++n;
35 }
36 else if (fractional_part < static_cast<T>(-1.0/2))
37 {
38 --n;
39 }
40
41 return x - n*y;
42 }
43
44 } // Namespace detail
45
46 template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>
47 constexpr Real remainder(Real x, Real y)
48 {
49 if (BOOST_MATH_IS_CONSTANT_EVALUATED(x))
50 {
51 if (boost::math::ccmath::isinf(x) && !boost::math::ccmath::isnan(y))
52 {
53 return std::numeric_limits<Real>::quiet_NaN();
54 }
55 else if (boost::math::ccmath::abs(y) == static_cast<Real>(0) && !boost::math::ccmath::isnan(x))
56 {
57 return std::numeric_limits<Real>::quiet_NaN();
58 }
59 else if (boost::math::ccmath::isnan(x))
60 {
61 return x;
62 }
63 else if (boost::math::ccmath::isnan(y))
64 {
65 return y;
66 }
67
68 return boost::math::ccmath::detail::remainder_impl(x, y);
69 }
70 else
71 {
72 using std::remainder;
73 return remainder(x, y);
74 }
75 }
76
77 template <typename T1, typename T2>
78 constexpr auto remainder(T1 x, T2 y)
79 {
80 if (BOOST_MATH_IS_CONSTANT_EVALUATED(x))
81 {
82 using promoted_type = boost::math::tools::promote_args_t<T1, T2>;
83 return boost::math::ccmath::remainder(promoted_type(x), promoted_type(y));
84 }
85 else
86 {
87 using std::remainder;
88 return remainder(x, y);
89 }
90 }
91
92 constexpr float remainderf(float x, float y)
93 {
94 return boost::math::ccmath::remainder(x, y);
95 }
96
97 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
98 constexpr long double remainderl(long double x, long double y)
99 {
100 return boost::math::ccmath::remainder(x, y);
101 }
102 #endif
103
104 } // Namespaces
105
106 #endif // BOOST_MATH_CCMATH_REMAINDER_HPP