]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | // (C) Copyright Matt Borland 2021. |
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 <cmath> | |
10 | #include <cstdint> | |
11 | #include <limits> | |
12 | #include <type_traits> | |
13 | #include <boost/math/tools/is_constant_evaluated.hpp> | |
14 | #include <boost/math/ccmath/abs.hpp> | |
15 | #include <boost/math/ccmath/isinf.hpp> | |
16 | #include <boost/math/ccmath/isnan.hpp> | |
17 | #include <boost/math/ccmath/isfinite.hpp> | |
18 | ||
19 | namespace boost::math::ccmath { | |
20 | ||
21 | namespace detail { | |
22 | ||
23 | template <typename ReturnType, typename T1, typename T2> | |
24 | inline constexpr ReturnType fmod_impl(T1 x, T2 y) noexcept | |
25 | { | |
26 | if(x == y) | |
27 | { | |
28 | return ReturnType(0); | |
29 | } | |
30 | else | |
31 | { | |
32 | while(x >= y) | |
33 | { | |
34 | x -= y; | |
35 | } | |
36 | ||
37 | return static_cast<ReturnType>(x); | |
38 | } | |
39 | } | |
40 | ||
41 | } // Namespace detail | |
42 | ||
43 | template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true> | |
44 | inline constexpr Real fmod(Real x, Real y) noexcept | |
45 | { | |
46 | if(BOOST_MATH_IS_CONSTANT_EVALUATED(x)) | |
47 | { | |
48 | return boost::math::ccmath::abs(x) == Real(0) && y != Real(0) ? x : | |
49 | boost::math::ccmath::isinf(x) && !boost::math::ccmath::isnan(y) ? std::numeric_limits<Real>::quiet_NaN() : | |
50 | boost::math::ccmath::abs(y) == Real(0) && !boost::math::ccmath::isnan(x) ? std::numeric_limits<Real>::quiet_NaN() : | |
51 | boost::math::ccmath::isinf(y) && boost::math::ccmath::isfinite(x) ? x : | |
52 | boost::math::ccmath::isnan(x) ? std::numeric_limits<Real>::quiet_NaN() : | |
53 | boost::math::ccmath::isnan(y) ? std::numeric_limits<Real>::quiet_NaN() : | |
54 | boost::math::ccmath::detail::fmod_impl<Real>(x, y); | |
55 | } | |
56 | else | |
57 | { | |
58 | using std::fmod; | |
59 | return fmod(x, y); | |
60 | } | |
61 | } | |
62 | ||
63 | template <typename T1, typename T2> | |
64 | inline constexpr auto fmod(T1 x, T2 y) noexcept | |
65 | { | |
66 | if(BOOST_MATH_IS_CONSTANT_EVALUATED(x)) | |
67 | { | |
68 | // If the type is an integer (e.g. epsilon == 0) then set the epsilon value to 1 so that type is at a minimum | |
69 | // cast to double | |
70 | constexpr auto T1p = std::numeric_limits<T1>::epsilon() > 0 ? std::numeric_limits<T1>::epsilon() : 1; | |
71 | constexpr auto T2p = std::numeric_limits<T2>::epsilon() > 0 ? std::numeric_limits<T2>::epsilon() : 1; | |
72 | ||
73 | using promoted_type = | |
74 | #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS | |
75 | std::conditional_t<T1p <= LDBL_EPSILON && T1p <= T2p, T1, | |
76 | std::conditional_t<T2p <= LDBL_EPSILON && T2p <= T1p, T2, | |
77 | #endif | |
78 | std::conditional_t<T1p <= DBL_EPSILON && T1p <= T2p, T1, | |
79 | std::conditional_t<T2p <= DBL_EPSILON && T2p <= T1p, T2, double | |
80 | #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS | |
81 | >>>>; | |
82 | #else | |
83 | >>; | |
84 | #endif | |
85 | ||
86 | return boost::math::ccmath::fmod(promoted_type(x), promoted_type(y)); | |
87 | } | |
88 | else | |
89 | { | |
90 | using std::fmod; | |
91 | return fmod(x, y); | |
92 | } | |
93 | } | |
94 | ||
95 | inline constexpr float fmodf(float x, float y) noexcept | |
96 | { | |
97 | return boost::math::ccmath::fmod(x, y); | |
98 | } | |
99 | ||
100 | #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS | |
101 | inline constexpr long double fmodl(long double x, long double y) noexcept | |
102 | { | |
103 | return boost::math::ccmath::fmod(x, y); | |
104 | } | |
105 | #endif | |
106 | ||
107 | } // Namespaces | |
108 | ||
109 | #endif // BOOST_MATH_CCMATH_FMOD_HPP |