File indexing completed on 2025-01-30 09:45:15
0001
0002
0003
0004
0005
0006 #ifndef BOOST_MATH_CCMATH_FMOD_HPP
0007 #define BOOST_MATH_CCMATH_FMOD_HPP
0008
0009 #include <boost/math/ccmath/detail/config.hpp>
0010
0011 #ifdef BOOST_MATH_NO_CCMATH
0012 #error "The header <boost/math/fmod.hpp> can only be used in C++17 and later."
0013 #endif
0014
0015 #include <cstdint>
0016 #include <boost/math/tools/promotion.hpp>
0017 #include <boost/math/ccmath/abs.hpp>
0018 #include <boost/math/ccmath/isinf.hpp>
0019 #include <boost/math/ccmath/isnan.hpp>
0020 #include <boost/math/ccmath/isfinite.hpp>
0021
0022 namespace boost::math::ccmath {
0023
0024 namespace detail {
0025
0026 template <typename T>
0027 constexpr T fmod_impl(T x, T y)
0028 {
0029 if (x == y)
0030 {
0031 return static_cast<T>(0);
0032 }
0033 else
0034 {
0035 while (x >= y)
0036 {
0037 x -= y;
0038 }
0039
0040 return static_cast<T>(x);
0041 }
0042 }
0043
0044 }
0045
0046 template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>
0047 constexpr Real fmod(Real x, Real y)
0048 {
0049 if(BOOST_MATH_IS_CONSTANT_EVALUATED(x))
0050 {
0051 if (boost::math::ccmath::abs(x) == static_cast<Real>(0) && y != static_cast<Real>(0))
0052 {
0053 return x;
0054 }
0055 else if (boost::math::ccmath::isinf(x) && !boost::math::ccmath::isnan(y))
0056 {
0057 return std::numeric_limits<Real>::quiet_NaN();
0058 }
0059 else if (boost::math::ccmath::abs(y) == static_cast<Real>(0) && !boost::math::ccmath::isnan(x))
0060 {
0061 return std::numeric_limits<Real>::quiet_NaN();
0062 }
0063 else if (boost::math::ccmath::isinf(y) && boost::math::ccmath::isfinite(x))
0064 {
0065 return x;
0066 }
0067 else if (boost::math::ccmath::isnan(x))
0068 {
0069 return x;
0070 }
0071 else if (boost::math::ccmath::isnan(y))
0072 {
0073 return y;
0074 }
0075
0076 return boost::math::ccmath::detail::fmod_impl<Real>(x, y);
0077 }
0078 else
0079 {
0080 using std::fmod;
0081 return fmod(x, y);
0082 }
0083 }
0084
0085 template <typename T1, typename T2>
0086 constexpr auto fmod(T1 x, T2 y)
0087 {
0088 if(BOOST_MATH_IS_CONSTANT_EVALUATED(x))
0089 {
0090 using promoted_type = boost::math::tools::promote_args_t<T1, T2>;
0091 return boost::math::ccmath::fmod(promoted_type(x), promoted_type(y));
0092 }
0093 else
0094 {
0095 using std::fmod;
0096 return fmod(x, y);
0097 }
0098 }
0099
0100 constexpr float fmodf(float x, float y)
0101 {
0102 return boost::math::ccmath::fmod(x, y);
0103 }
0104
0105 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
0106 constexpr long double fmodl(long double x, long double y)
0107 {
0108 return boost::math::ccmath::fmod(x, y);
0109 }
0110 #endif
0111
0112 }
0113
0114 #endif