354 lines · plain
1// Copyright John Maddock 2007.2// Copyright Matt Borland 2023.3// Use, modification and distribution are subject to the4// Boost Software License, Version 1.0. (See accompanying file5// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)6 7#ifndef BOOST_MATH_ROUND_HPP8#define BOOST_MATH_ROUND_HPP9 10#ifdef _MSC_VER11#pragma once12#endif13 14#include <boost/math/tools/config.hpp>15 16#ifndef BOOST_MATH_HAS_NVRTC17 18#include <boost/math/ccmath/detail/config.hpp>19#include <boost/math/policies/error_handling.hpp>20#include <boost/math/special_functions/math_fwd.hpp>21#include <boost/math/special_functions/fpclassify.hpp>22#include <type_traits>23#include <limits>24#include <cmath>25 26#if !defined(BOOST_MATH_NO_CCMATH) && !defined(BOOST_MATH_NO_CONSTEXPR_DETECTION)27#include <boost/math/ccmath/ldexp.hpp>28# define BOOST_MATH_HAS_CONSTEXPR_LDEXP29#endif30 31namespace boost{ namespace math{32 33namespace detail{34 35template <class T, class Policy>36BOOST_MATH_GPU_ENABLED inline tools::promote_args_t<T> round(const T& v, const Policy& pol, const std::false_type&)37{38 BOOST_MATH_STD_USING39 using result_type = tools::promote_args_t<T>;40 41 if(!(boost::math::isfinite)(v))42 {43 return policies::raise_rounding_error("boost::math::round<%1%>(%1%)", nullptr, static_cast<result_type>(v), static_cast<result_type>(v), pol);44 }45 //46 // The logic here is rather convoluted, but avoids a number of traps,47 // see discussion here https://github.com/boostorg/math/pull/848 //49 if (T(-0.5) < v && v < T(0.5))50 {51 // special case to avoid rounding error on the direct52 // predecessor of +0.5 resp. the direct successor of -0.5 in53 // IEEE floating point types54 return static_cast<result_type>(0);55 }56 else if (v > 0)57 {58 // subtract v from ceil(v) first in order to avoid rounding59 // errors on largest representable integer numbers60 result_type c(ceil(v));61 return T(0.5) < c - v ? c - 1 : c;62 }63 else64 {65 // see former branch66 result_type f(floor(v));67 return T(0.5) < v - f ? f + 1 : f;68 }69}70template <class T, class Policy>71BOOST_MATH_GPU_ENABLED inline tools::promote_args_t<T> round(const T& v, const Policy&, const std::true_type&)72{73 return v;74}75 76} // namespace detail77 78template <class T, class Policy>79BOOST_MATH_GPU_ENABLED inline tools::promote_args_t<T> round(const T& v, const Policy& pol)80{81 return detail::round(v, pol, std::integral_constant<bool, detail::is_integer_for_rounding<T>::value>());82}83template <class T>84BOOST_MATH_GPU_ENABLED inline tools::promote_args_t<T> round(const T& v)85{86 return round(v, policies::policy<>());87}88//89// The following functions will not compile unless T has an90// implicit conversion to the integer types. For user-defined91// number types this will likely not be the case. In that case92// these functions should either be specialized for the UDT in93// question, or else overloads should be placed in the same94// namespace as the UDT: these will then be found via argument95// dependent lookup. See our concept archetypes for examples.96//97// Non-standard numeric limits syntax "(std::numeric_limits<int>::max)()"98// is to avoid macro substiution from MSVC99// https://stackoverflow.com/questions/27442885/syntax-error-with-stdnumeric-limitsmax100//101template <class T, class Policy>102inline int iround(const T& v, const Policy& pol)103{104 BOOST_MATH_STD_USING105 using result_type = tools::promote_args_t<T>;106 107 result_type r = boost::math::round(v, pol);108 109 #if defined(BOOST_MATH_HAS_CONSTEXPR_LDEXP) && !defined(BOOST_MATH_HAS_GPU_SUPPORT)110 if constexpr (std::is_arithmetic_v<result_type>111 #ifdef BOOST_MATH_FLOAT128_TYPE112 && !std::is_same_v<BOOST_MATH_FLOAT128_TYPE, result_type>113 #endif114 )115 {116 constexpr result_type max_val = boost::math::ccmath::ldexp(static_cast<result_type>(1), std::numeric_limits<int>::digits);117 118 if (r >= max_val || r < -max_val)119 {120 return static_cast<int>(boost::math::policies::raise_rounding_error("boost::math::iround<%1%>(%1%)", nullptr, v, static_cast<int>(0), pol));121 }122 }123 else124 {125 static const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<int>::digits);126 127 if (r >= max_val || r < -max_val)128 {129 return static_cast<int>(boost::math::policies::raise_rounding_error("boost::math::iround<%1%>(%1%)", nullptr, v, static_cast<int>(0), pol));130 }131 }132 #else133 BOOST_MATH_STATIC_LOCAL_VARIABLE const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<int>::digits);134 135 if (r >= max_val || r < -max_val)136 {137 return static_cast<int>(boost::math::policies::raise_rounding_error("boost::math::iround<%1%>(%1%)", nullptr, v, static_cast<int>(0), pol));138 }139 #endif140 141 return static_cast<int>(r);142}143template <class T>144BOOST_MATH_GPU_ENABLED inline int iround(const T& v)145{146 return iround(v, policies::policy<>());147}148 149template <class T, class Policy>150BOOST_MATH_GPU_ENABLED inline long lround(const T& v, const Policy& pol)151{152 BOOST_MATH_STD_USING153 using result_type = tools::promote_args_t<T>;154 155 result_type r = boost::math::round(v, pol);156 157 #if defined(BOOST_MATH_HAS_CONSTEXPR_LDEXP) && !defined(BOOST_MATH_HAS_GPU_SUPPORT)158 if constexpr (std::is_arithmetic_v<result_type>159 #ifdef BOOST_MATH_FLOAT128_TYPE160 && !std::is_same_v<BOOST_MATH_FLOAT128_TYPE, result_type>161 #endif162 )163 {164 constexpr result_type max_val = boost::math::ccmath::ldexp(static_cast<result_type>(1), std::numeric_limits<long>::digits);165 166 if (r >= max_val || r < -max_val)167 {168 return static_cast<long>(boost::math::policies::raise_rounding_error("boost::math::lround<%1%>(%1%)", nullptr, v, static_cast<long>(0), pol));169 }170 }171 else172 {173 static const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<long>::digits);174 175 if (r >= max_val || r < -max_val)176 {177 return static_cast<long>(boost::math::policies::raise_rounding_error("boost::math::lround<%1%>(%1%)", nullptr, v, static_cast<long>(0), pol));178 }179 }180 #else181 BOOST_MATH_STATIC_LOCAL_VARIABLE const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<long>::digits);182 183 if (r >= max_val || r < -max_val)184 {185 return static_cast<long>(boost::math::policies::raise_rounding_error("boost::math::lround<%1%>(%1%)", nullptr, v, static_cast<long>(0), pol));186 }187 #endif188 189 return static_cast<long>(r);190}191template <class T>192BOOST_MATH_GPU_ENABLED inline long lround(const T& v)193{194 return lround(v, policies::policy<>());195}196 197template <class T, class Policy>198BOOST_MATH_GPU_ENABLED inline long long llround(const T& v, const Policy& pol)199{200 BOOST_MATH_STD_USING201 using result_type = boost::math::tools::promote_args_t<T>;202 203 result_type r = boost::math::round(v, pol);204 205 #if defined(BOOST_MATH_HAS_CONSTEXPR_LDEXP) && !defined(BOOST_MATH_HAS_GPU_SUPPORT)206 if constexpr (std::is_arithmetic_v<result_type>207 #ifdef BOOST_MATH_FLOAT128_TYPE208 && !std::is_same_v<BOOST_MATH_FLOAT128_TYPE, result_type>209 #endif210 )211 {212 constexpr result_type max_val = boost::math::ccmath::ldexp(static_cast<result_type>(1), std::numeric_limits<long long>::digits);213 214 if (r >= max_val || r < -max_val)215 {216 return static_cast<long long>(boost::math::policies::raise_rounding_error("boost::math::llround<%1%>(%1%)", nullptr, v, static_cast<long long>(0), pol));217 }218 }219 else220 {221 static const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<long long>::digits);222 223 if (r >= max_val || r < -max_val)224 {225 return static_cast<long long>(boost::math::policies::raise_rounding_error("boost::math::llround<%1%>(%1%)", nullptr, v, static_cast<long long>(0), pol));226 }227 }228 #else229 BOOST_MATH_STATIC_LOCAL_VARIABLE const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<long long>::digits);230 231 if (r >= max_val || r < -max_val)232 {233 return static_cast<long long>(boost::math::policies::raise_rounding_error("boost::math::llround<%1%>(%1%)", nullptr, v, static_cast<long long>(0), pol));234 }235 #endif236 237 return static_cast<long long>(r);238}239template <class T>240BOOST_MATH_GPU_ENABLED inline long long llround(const T& v)241{242 return llround(v, policies::policy<>());243}244 245}} // namespaces246 247#else // Specialized NVRTC overloads248 249namespace boost {250namespace math {251 252template <typename T>253BOOST_MATH_GPU_ENABLED T round(T x)254{255 return ::round(x);256}257 258template <>259BOOST_MATH_GPU_ENABLED float round(float x)260{261 return ::roundf(x);262}263 264template <typename T, typename Policy>265BOOST_MATH_GPU_ENABLED T round(T x, const Policy&)266{267 return ::round(x);268}269 270template <typename Policy>271BOOST_MATH_GPU_ENABLED float round(float x, const Policy&)272{273 return ::roundf(x);274}275 276template <typename T>277BOOST_MATH_GPU_ENABLED int iround(T x)278{279 return static_cast<int>(::lround(x));280}281 282template <>283BOOST_MATH_GPU_ENABLED int iround(float x)284{285 return static_cast<int>(::lroundf(x));286}287 288template <typename T, typename Policy>289BOOST_MATH_GPU_ENABLED int iround(T x, const Policy&)290{291 return static_cast<int>(::lround(x));292}293 294template <typename Policy>295BOOST_MATH_GPU_ENABLED int iround(float x, const Policy&)296{297 return static_cast<int>(::lroundf(x));298}299 300template <typename T>301BOOST_MATH_GPU_ENABLED long lround(T x)302{303 return ::lround(x);304}305 306template <>307BOOST_MATH_GPU_ENABLED long lround(float x)308{309 return ::lroundf(x);310}311 312template <typename T, typename Policy>313BOOST_MATH_GPU_ENABLED long lround(T x, const Policy&)314{315 return ::lround(x);316}317 318template <typename Policy>319BOOST_MATH_GPU_ENABLED long lround(float x, const Policy&)320{321 return ::lroundf(x);322}323 324template <typename T>325BOOST_MATH_GPU_ENABLED long long llround(T x)326{327 return ::llround(x);328}329 330template <>331BOOST_MATH_GPU_ENABLED long long llround(float x)332{333 return ::llroundf(x);334}335 336template <typename T, typename Policy>337BOOST_MATH_GPU_ENABLED long long llround(T x, const Policy&)338{339 return ::llround(x);340}341 342template <typename Policy>343BOOST_MATH_GPU_ENABLED long long llround(float x, const Policy&)344{345 return ::llroundf(x);346}347 348} // Namespace math349} // Namespace boost350 351#endif // BOOST_MATH_HAS_NVRTC352 353#endif // BOOST_MATH_ROUND_HPP354