File indexing completed on 2025-01-30 09:45:16
0001
0002
0003
0004
0005
0006 #ifndef BOOST_MATH_CCMATH_TRUNC_HPP
0007 #define BOOST_MATH_CCMATH_TRUNC_HPP
0008
0009 #include <boost/math/ccmath/detail/config.hpp>
0010
0011 #ifdef BOOST_MATH_NO_CCMATH
0012 #error "The header <boost/math/trunc.hpp> can only be used in C++17 and later."
0013 #endif
0014
0015 #include <boost/math/ccmath/abs.hpp>
0016 #include <boost/math/ccmath/isinf.hpp>
0017 #include <boost/math/ccmath/isnan.hpp>
0018 #include <boost/math/ccmath/floor.hpp>
0019 #include <boost/math/ccmath/ceil.hpp>
0020
0021 namespace boost::math::ccmath {
0022
0023 namespace detail {
0024
0025 template <typename T>
0026 inline constexpr T trunc_impl(T arg) noexcept
0027 {
0028 return (arg > 0) ? boost::math::ccmath::floor(arg) : boost::math::ccmath::ceil(arg);
0029 }
0030
0031 }
0032
0033 template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>
0034 inline constexpr Real trunc(Real arg) noexcept
0035 {
0036 if(BOOST_MATH_IS_CONSTANT_EVALUATED(arg))
0037 {
0038 return boost::math::ccmath::abs(arg) == Real(0) ? arg :
0039 boost::math::ccmath::isinf(arg) ? arg :
0040 boost::math::ccmath::isnan(arg) ? arg :
0041 boost::math::ccmath::detail::trunc_impl(arg);
0042 }
0043 else
0044 {
0045 using std::trunc;
0046 return trunc(arg);
0047 }
0048 }
0049
0050 template <typename Z, std::enable_if_t<std::is_integral_v<Z>, bool> = true>
0051 inline constexpr double trunc(Z arg) noexcept
0052 {
0053 return boost::math::ccmath::trunc(static_cast<double>(arg));
0054 }
0055
0056 inline constexpr float truncf(float arg) noexcept
0057 {
0058 return boost::math::ccmath::trunc(arg);
0059 }
0060
0061 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
0062 inline constexpr long double truncl(long double arg) noexcept
0063 {
0064 return boost::math::ccmath::trunc(arg);
0065 }
0066 #endif
0067
0068 }
0069
0070 #endif