Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:45:15

0001 //  (C) Copyright Matt Borland 2021 - 2022.
0002 //  Use, modification and distribution are subject to the
0003 //  Boost Software License, Version 1.0. (See accompanying file
0004 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0005 
0006 #ifndef BOOST_MATH_CCMATH_REMAINDER_HPP
0007 #define BOOST_MATH_CCMATH_REMAINDER_HPP
0008 
0009 #include <boost/math/ccmath/detail/config.hpp>
0010 
0011 #ifdef BOOST_MATH_NO_CCMATH
0012 #error "The header <boost/math/remainder.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 #include <boost/math/ccmath/modf.hpp>
0022 
0023 namespace boost::math::ccmath {
0024 
0025 namespace detail {
0026 
0027 template <typename T>
0028 constexpr T remainder_impl(const T x, const T y)
0029 {
0030     T n = 0;
0031 
0032     if (T fractional_part = boost::math::ccmath::modf((x / y), &n); fractional_part > static_cast<T>(1.0/2))
0033     {
0034         ++n;
0035     }
0036     else if (fractional_part < static_cast<T>(-1.0/2))
0037     {
0038         --n;
0039     }
0040 
0041     return x - n*y;
0042 }
0043 
0044 } // Namespace detail
0045 
0046 template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>
0047 constexpr Real remainder(Real x, Real y)
0048 {
0049     if (BOOST_MATH_IS_CONSTANT_EVALUATED(x))
0050     {
0051         if (boost::math::ccmath::isinf(x) && !boost::math::ccmath::isnan(y))
0052         {
0053             return std::numeric_limits<Real>::quiet_NaN();
0054         }
0055         else if (boost::math::ccmath::abs(y) == static_cast<Real>(0) && !boost::math::ccmath::isnan(x))
0056         {
0057             return std::numeric_limits<Real>::quiet_NaN();
0058         }
0059         else if (boost::math::ccmath::isnan(x))
0060         {
0061             return x;
0062         }
0063         else if (boost::math::ccmath::isnan(y))
0064         {
0065             return y;
0066         }
0067 
0068         return boost::math::ccmath::detail::remainder_impl(x, y);
0069     }
0070     else
0071     {
0072         using std::remainder;
0073         return remainder(x, y);
0074     }
0075 }
0076 
0077 template <typename T1, typename T2>
0078 constexpr auto remainder(T1 x, T2 y)
0079 {
0080     if (BOOST_MATH_IS_CONSTANT_EVALUATED(x))
0081     {
0082         using promoted_type = boost::math::tools::promote_args_t<T1, T2>;
0083         return boost::math::ccmath::remainder(promoted_type(x), promoted_type(y));
0084     }
0085     else
0086     {
0087         using std::remainder;
0088         return remainder(x, y);
0089     }
0090 }
0091 
0092 constexpr float remainderf(float x, float y)
0093 {
0094     return boost::math::ccmath::remainder(x, y);
0095 }
0096 
0097 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
0098 constexpr long double remainderl(long double x, long double y)
0099 {
0100     return boost::math::ccmath::remainder(x, y);
0101 }
0102 #endif
0103 
0104 } // Namespaces
0105 
0106 #endif // BOOST_MATH_CCMATH_REMAINDER_HPP