brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.6 KiB · b57420b Raw
107 lines · plain
1//  (C) Copyright Matt Borland 2021 - 2022.2//  Use, modification and distribution are subject to the3//  Boost Software License, Version 1.0. (See accompanying file4//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)5 6#ifndef BOOST_MATH_CCMATH_REMAINDER_HPP7#define BOOST_MATH_CCMATH_REMAINDER_HPP8 9#include <boost/math/ccmath/detail/config.hpp>10 11#ifdef BOOST_MATH_NO_CCMATH12#error "The header <boost/math/remainder.hpp> can only be used in C++17 and later."13#endif14 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 23namespace boost::math::ccmath {24 25namespace detail {26 27template <typename T>28constexpr 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 detail45 46template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>47constexpr 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    else71    {72        using std::remainder;73        return remainder(x, y);74    }75}76 77template <typename T1, typename T2>78constexpr 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    else86    {87        using std::remainder;88        return remainder(x, y);89    }90}91 92constexpr 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_FUNCTIONS98constexpr long double remainderl(long double x, long double y)99{100    return boost::math::ccmath::remainder(x, y);101}102#endif103 104} // Namespaces105 106#endif // BOOST_MATH_CCMATH_REMAINDER_HPP107