Debian Codegraph @

third-party/boost-math/include/boost/math/ccmath/fmod.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_FMOD_HPP
7 #define BOOST_MATH_CCMATH_FMOD_HPP
8
9 #include <boost/math/ccmath/detail/config.hpp>
10
11 #ifdef BOOST_MATH_NO_CCMATH
12 #error "The header <boost/math/fmod.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
22 namespace boost::math::ccmath {
23
24 namespace detail {
25
26 template <typename T>
27 constexpr T fmod_impl(T x, T y)
28 {
29 if (x == y)
30 {
31 return static_cast<T>(0);
32 }
33 else
34 {
35 while (x >= y)
36 {
37 x -= y;
38 }
39
40 return static_cast<T>(x);
41 }
42 }
43
44 } // Namespace detail
45
46 template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>
47 constexpr Real fmod(Real x, Real y)
48 {
49 if(BOOST_MATH_IS_CONSTANT_EVALUATED(x))
50 {
51 if (boost::math::ccmath::abs(x) == static_cast<Real>(0) && y != static_cast<Real>(0))
52 {
53 return x;
54 }
55 else if (boost::math::ccmath::isinf(x) && !boost::math::ccmath::isnan(y))
56 {
57 return std::numeric_limits<Real>::quiet_NaN();
58 }
59 else if (boost::math::ccmath::abs(y) == static_cast<Real>(0) && !boost::math::ccmath::isnan(x))
60 {
61 return std::numeric_limits<Real>::quiet_NaN();
62 }
63 else if (boost::math::ccmath::isinf(y) && boost::math::ccmath::isfinite(x))
64 {
65 return x;
66 }
67 else if (boost::math::ccmath::isnan(x))
68 {
69 return x;
70 }
71 else if (boost::math::ccmath::isnan(y))
72 {
73 return y;
74 }
75
76 return boost::math::ccmath::detail::fmod_impl<Real>(x, y);
77 }
78 else
79 {
80 using std::fmod;
81 return fmod(x, y);
82 }
83 }
84
85 template <typename T1, typename T2>
86 constexpr auto fmod(T1 x, T2 y)
87 {
88 if(BOOST_MATH_IS_CONSTANT_EVALUATED(x))
89 {
90 using promoted_type = boost::math::tools::promote_args_t<T1, T2>;
91 return boost::math::ccmath::fmod(promoted_type(x), promoted_type(y));
92 }
93 else
94 {
95 using std::fmod;
96 return fmod(x, y);
97 }
98 }
99
100 constexpr float fmodf(float x, float y)
101 {
102 return boost::math::ccmath::fmod(x, y);
103 }
104
105 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
106 constexpr long double fmodl(long double x, long double y)
107 {
108 return boost::math::ccmath::fmod(x, y);
109 }
110 #endif
111
112 } // Namespaces
113
114 #endif // BOOST_MATH_CCMATH_FMOD_HPP