annotate DEPENDENCIES/generic/include/boost/math/special_functions/round.hpp @ 133:4acb5d8d80b6 tip

Don't fail environmental check if README.md exists (but .txt and no-suffix don't)
author Chris Cannam
date Tue, 30 Jul 2019 12:25:44 +0100
parents c530137014c0
children
rev   line source
Chris@16 1 // Copyright John Maddock 2007.
Chris@16 2 // Use, modification and distribution are subject to the
Chris@16 3 // Boost Software License, Version 1.0. (See accompanying file
Chris@16 4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Chris@16 5
Chris@16 6 #ifndef BOOST_MATH_ROUND_HPP
Chris@16 7 #define BOOST_MATH_ROUND_HPP
Chris@16 8
Chris@16 9 #ifdef _MSC_VER
Chris@16 10 #pragma once
Chris@16 11 #endif
Chris@16 12
Chris@16 13 #include <boost/math/tools/config.hpp>
Chris@16 14 #include <boost/math/policies/error_handling.hpp>
Chris@101 15 #include <boost/math/special_functions/math_fwd.hpp>
Chris@16 16 #include <boost/math/special_functions/fpclassify.hpp>
Chris@16 17
Chris@16 18 namespace boost{ namespace math{
Chris@16 19
Chris@101 20 namespace detail{
Chris@101 21
Chris@101 22 template <class T, class Policy>
Chris@101 23 inline typename tools::promote_args<T>::type round(const T& v, const Policy& pol, const mpl::false_)
Chris@101 24 {
Chris@101 25 BOOST_MATH_STD_USING
Chris@101 26 typedef typename tools::promote_args<T>::type result_type;
Chris@101 27 if(!(boost::math::isfinite)(v))
Chris@101 28 return policies::raise_rounding_error("boost::math::round<%1%>(%1%)", 0, static_cast<result_type>(v), static_cast<result_type>(v), pol);
Chris@101 29 //
Chris@101 30 // The logic here is rather convoluted, but avoids a number of traps,
Chris@101 31 // see discussion here https://github.com/boostorg/math/pull/8
Chris@101 32 //
Chris@101 33 if (-0.5 < v && v < 0.5)
Chris@101 34 {
Chris@101 35 // special case to avoid rounding error on the direct
Chris@101 36 // predecessor of +0.5 resp. the direct successor of -0.5 in
Chris@101 37 // IEEE floating point types
Chris@101 38 return 0;
Chris@101 39 }
Chris@101 40 else if (v > 0)
Chris@101 41 {
Chris@101 42 // subtract v from ceil(v) first in order to avoid rounding
Chris@101 43 // errors on largest representable integer numbers
Chris@101 44 result_type c(ceil(v));
Chris@101 45 return 0.5 < c - v ? c - 1 : c;
Chris@101 46 }
Chris@101 47 else
Chris@101 48 {
Chris@101 49 // see former branch
Chris@101 50 result_type f(floor(v));
Chris@101 51 return 0.5 < v - f ? f + 1 : f;
Chris@101 52 }
Chris@101 53 }
Chris@101 54 template <class T, class Policy>
Chris@101 55 inline typename tools::promote_args<T>::type round(const T& v, const Policy&, const mpl::true_)
Chris@101 56 {
Chris@101 57 return v;
Chris@101 58 }
Chris@101 59
Chris@101 60 } // namespace detail
Chris@101 61
Chris@16 62 template <class T, class Policy>
Chris@16 63 inline typename tools::promote_args<T>::type round(const T& v, const Policy& pol)
Chris@16 64 {
Chris@101 65 return detail::round(v, pol, mpl::bool_<detail::is_integer_for_rounding<T>::value>());
Chris@16 66 }
Chris@16 67 template <class T>
Chris@16 68 inline typename tools::promote_args<T>::type round(const T& v)
Chris@16 69 {
Chris@16 70 return round(v, policies::policy<>());
Chris@16 71 }
Chris@16 72 //
Chris@16 73 // The following functions will not compile unless T has an
Chris@16 74 // implicit convertion to the integer types. For user-defined
Chris@16 75 // number types this will likely not be the case. In that case
Chris@16 76 // these functions should either be specialized for the UDT in
Chris@16 77 // question, or else overloads should be placed in the same
Chris@16 78 // namespace as the UDT: these will then be found via argument
Chris@16 79 // dependent lookup. See our concept archetypes for examples.
Chris@16 80 //
Chris@16 81 template <class T, class Policy>
Chris@16 82 inline int iround(const T& v, const Policy& pol)
Chris@16 83 {
Chris@16 84 BOOST_MATH_STD_USING
Chris@16 85 T r = boost::math::round(v, pol);
Chris@16 86 if((r > (std::numeric_limits<int>::max)()) || (r < (std::numeric_limits<int>::min)()))
Chris@16 87 return static_cast<int>(policies::raise_rounding_error("boost::math::iround<%1%>(%1%)", 0, v, 0, pol));
Chris@16 88 return static_cast<int>(r);
Chris@16 89 }
Chris@16 90 template <class T>
Chris@16 91 inline int iround(const T& v)
Chris@16 92 {
Chris@16 93 return iround(v, policies::policy<>());
Chris@16 94 }
Chris@16 95
Chris@16 96 template <class T, class Policy>
Chris@16 97 inline long lround(const T& v, const Policy& pol)
Chris@16 98 {
Chris@16 99 BOOST_MATH_STD_USING
Chris@16 100 T r = boost::math::round(v, pol);
Chris@16 101 if((r > (std::numeric_limits<long>::max)()) || (r < (std::numeric_limits<long>::min)()))
Chris@16 102 return static_cast<long int>(policies::raise_rounding_error("boost::math::lround<%1%>(%1%)", 0, v, 0L, pol));
Chris@16 103 return static_cast<long int>(r);
Chris@16 104 }
Chris@16 105 template <class T>
Chris@16 106 inline long lround(const T& v)
Chris@16 107 {
Chris@16 108 return lround(v, policies::policy<>());
Chris@16 109 }
Chris@16 110
Chris@16 111 #ifdef BOOST_HAS_LONG_LONG
Chris@16 112
Chris@16 113 template <class T, class Policy>
Chris@16 114 inline boost::long_long_type llround(const T& v, const Policy& pol)
Chris@16 115 {
Chris@16 116 BOOST_MATH_STD_USING
Chris@16 117 T r = boost::math::round(v, pol);
Chris@16 118 if((r > (std::numeric_limits<boost::long_long_type>::max)()) || (r < (std::numeric_limits<boost::long_long_type>::min)()))
Chris@16 119 return static_cast<boost::long_long_type>(policies::raise_rounding_error("boost::math::llround<%1%>(%1%)", 0, v, static_cast<boost::long_long_type>(0), pol));
Chris@16 120 return static_cast<boost::long_long_type>(r);
Chris@16 121 }
Chris@16 122 template <class T>
Chris@16 123 inline boost::long_long_type llround(const T& v)
Chris@16 124 {
Chris@16 125 return llround(v, policies::policy<>());
Chris@16 126 }
Chris@16 127
Chris@16 128 #endif
Chris@16 129
Chris@16 130 }} // namespaces
Chris@16 131
Chris@16 132 #endif // BOOST_MATH_ROUND_HPP