File indexing completed on 2025-07-11 08:14:50
0001
0002
0003
0004
0005
0006 #ifndef BOOST_MATH_CCMATH_FLOOR_HPP
0007 #define BOOST_MATH_CCMATH_FLOOR_HPP
0008
0009 #include <boost/math/ccmath/detail/config.hpp>
0010
0011 #ifdef BOOST_MATH_NO_CCMATH
0012 #error "The header <boost/math/floor.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 <limits>
0019
0020 namespace boost::math::ccmath {
0021
0022 namespace detail {
0023
0024 template <typename T>
0025 inline constexpr T floor_pos_impl(T arg) noexcept
0026 {
0027 constexpr auto max_comp_val = T(1) / std::numeric_limits<T>::epsilon();
0028
0029 if (arg >= max_comp_val)
0030 {
0031 return arg;
0032 }
0033
0034 T result = 1;
0035
0036 if(result < arg)
0037 {
0038 while(result < arg)
0039 {
0040 result *= 2;
0041 }
0042 while(result > arg)
0043 {
0044 --result;
0045 }
0046
0047 return result;
0048 }
0049 else
0050 {
0051 return T(0);
0052 }
0053 }
0054
0055 template <typename T>
0056 inline constexpr T floor_neg_impl(T arg) noexcept
0057 {
0058 T result = -1;
0059
0060 if(result > arg)
0061 {
0062 while(result > arg)
0063 {
0064 result *= 2;
0065 }
0066 while(result < arg)
0067 {
0068 ++result;
0069 }
0070 if(result != arg)
0071 {
0072 --result;
0073 }
0074 }
0075
0076 return result;
0077 }
0078
0079 template <typename T>
0080 inline constexpr T floor_impl(T arg) noexcept
0081 {
0082 if(arg > 0)
0083 {
0084 return floor_pos_impl(arg);
0085 }
0086 else
0087 {
0088 return floor_neg_impl(arg);
0089 }
0090 }
0091
0092 }
0093
0094 template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>
0095 inline constexpr Real floor(Real arg) noexcept
0096 {
0097 if(BOOST_MATH_IS_CONSTANT_EVALUATED(arg))
0098 {
0099 return boost::math::ccmath::abs(arg) == Real(0) ? arg :
0100 boost::math::ccmath::isinf(arg) ? arg :
0101 boost::math::ccmath::isnan(arg) ? arg :
0102 boost::math::ccmath::detail::floor_impl(arg);
0103 }
0104 else
0105 {
0106 using std::floor;
0107 return floor(arg);
0108 }
0109 }
0110
0111 template <typename Z, std::enable_if_t<std::is_integral_v<Z>, bool> = true>
0112 inline constexpr double floor(Z arg) noexcept
0113 {
0114 return boost::math::ccmath::floor(static_cast<double>(arg));
0115 }
0116
0117 inline constexpr float floorf(float arg) noexcept
0118 {
0119 return boost::math::ccmath::floor(arg);
0120 }
0121
0122 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
0123 inline constexpr long double floorl(long double arg) noexcept
0124 {
0125 return boost::math::ccmath::floor(arg);
0126 }
0127 #endif
0128
0129 }
0130
0131 #endif