|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r74925 - in trunk: boost/math/special_functions libs/math/test
From: john_at_[hidden]
Date: 2011-10-12 07:06:10
Author: johnmaddock
Date: 2011-10-12 07:06:09 EDT (Wed, 12 Oct 2011)
New Revision: 74925
URL: http://svn.boost.org/trac/boost/changeset/74925
Log:
Fix error checking for trunc and round functions.
Update tests to match.
Text files modified:
trunk/boost/math/special_functions/round.hpp | 6 +-
trunk/boost/math/special_functions/trunc.hpp | 6 +-
trunk/libs/math/test/test_round.cpp | 68 +++++++++++++++++++++++++++++++++++++++
3 files changed, 73 insertions(+), 7 deletions(-)
Modified: trunk/boost/math/special_functions/round.hpp
==============================================================================
--- trunk/boost/math/special_functions/round.hpp (original)
+++ trunk/boost/math/special_functions/round.hpp 2011-10-12 07:06:09 EDT (Wed, 12 Oct 2011)
@@ -43,7 +43,7 @@
{
BOOST_MATH_STD_USING
T r = boost::math::round(v, pol);
- if(fabs(r) > (std::numeric_limits<int>::max)())
+ if((r > (std::numeric_limits<int>::max)()) || (r < (std::numeric_limits<int>::min)()))
return static_cast<int>(policies::raise_rounding_error("boost::math::iround<%1%>(%1%)", 0, v, 0, pol));
return static_cast<int>(r);
}
@@ -58,7 +58,7 @@
{
BOOST_MATH_STD_USING
T r = boost::math::round(v, pol);
- if(fabs(r) > (std::numeric_limits<long>::max)())
+ if((r > (std::numeric_limits<long>::max)()) || (r < (std::numeric_limits<long>::min)()))
return static_cast<long int>(policies::raise_rounding_error("boost::math::lround<%1%>(%1%)", 0, v, 0L, pol));
return static_cast<long int>(r);
}
@@ -75,7 +75,7 @@
{
BOOST_MATH_STD_USING
T r = boost::math::round(v, pol);
- if(fabs(r) > (std::numeric_limits<boost::long_long_type>::max)())
+ if((r > (std::numeric_limits<boost::long_long_type>::max)()) || (r < (std::numeric_limits<boost::long_long_type>::min)()))
return static_cast<boost::long_long_type>(policies::raise_rounding_error("boost::math::llround<%1%>(%1%)", 0, v, 0LL, pol));
return static_cast<boost::long_long_type>(r);
}
Modified: trunk/boost/math/special_functions/trunc.hpp
==============================================================================
--- trunk/boost/math/special_functions/trunc.hpp (original)
+++ trunk/boost/math/special_functions/trunc.hpp 2011-10-12 07:06:09 EDT (Wed, 12 Oct 2011)
@@ -43,7 +43,7 @@
{
BOOST_MATH_STD_USING
T r = boost::math::trunc(v, pol);
- if(fabs(r) > (std::numeric_limits<int>::max)())
+ if((r > (std::numeric_limits<int>::max)()) || (r < (std::numeric_limits<int>::min)()))
return static_cast<int>(policies::raise_rounding_error("boost::math::itrunc<%1%>(%1%)", 0, v, 0, pol));
return static_cast<int>(r);
}
@@ -58,7 +58,7 @@
{
BOOST_MATH_STD_USING
T r = boost::math::trunc(v, pol);
- if(fabs(r) > (std::numeric_limits<long>::max)())
+ if((r > (std::numeric_limits<long>::max)()) || (r < (std::numeric_limits<long>::min)()))
return static_cast<long>(policies::raise_rounding_error("boost::math::ltrunc<%1%>(%1%)", 0, v, 0L, pol));
return static_cast<long>(r);
}
@@ -75,7 +75,7 @@
{
BOOST_MATH_STD_USING
T r = boost::math::trunc(v, pol);
- if(fabs(r) > (std::numeric_limits<boost::long_long_type>::max)())
+ if((r > (std::numeric_limits<boost::long_long_type>::max)()) || (r < (std::numeric_limits<boost::long_long_type>::min)()))
return static_cast<boost::long_long_type>(policies::raise_rounding_error("boost::math::lltrunc<%1%>(%1%)", 0, v, 0LL, pol));
return static_cast<boost::long_long_type>(r);
}
Modified: trunk/libs/math/test/test_round.cpp
==============================================================================
--- trunk/libs/math/test/test_round.cpp (original)
+++ trunk/libs/math/test/test_round.cpp 2011-10-12 07:06:09 EDT (Wed, 12 Oct 2011)
@@ -145,7 +145,17 @@
r = boost::math::modf(arg, &i);
check_modf_result(arg, r, i);
}
-
+ if(std::numeric_limits<T>::digits >= std::numeric_limits<int>::digits)
+ {
+ int si = boost::math::iround(static_cast<T>((std::numeric_limits<int>::max)()));
+ check_within_half(static_cast<T>((std::numeric_limits<int>::max)()), si);
+ si = boost::math::iround(static_cast<T>((std::numeric_limits<int>::min)()));
+ check_within_half(static_cast<T>((std::numeric_limits<int>::min)()), si);
+ si = boost::math::itrunc(static_cast<T>((std::numeric_limits<int>::max)()));
+ check_trunc_result(static_cast<T>((std::numeric_limits<int>::max)()), si);
+ si = boost::math::itrunc(static_cast<T>((std::numeric_limits<int>::min)()));
+ check_trunc_result(static_cast<T>((std::numeric_limits<int>::min)()), si);
+ }
if(abs(r) < (std::numeric_limits<long>::max)())
{
long l = boost::math::lround(arg);
@@ -155,6 +165,17 @@
r = boost::math::modf(arg, &l);
check_modf_result(arg, r, l);
}
+ if(std::numeric_limits<T>::digits >= std::numeric_limits<long>::digits)
+ {
+ long k = boost::math::lround(static_cast<T>((std::numeric_limits<long>::max)()));
+ check_within_half(static_cast<T>((std::numeric_limits<long>::max)()), k);
+ k = boost::math::lround(static_cast<T>((std::numeric_limits<long>::min)()));
+ check_within_half(static_cast<T>((std::numeric_limits<long>::min)()), k);
+ k = boost::math::ltrunc(static_cast<T>((std::numeric_limits<long>::max)()));
+ check_trunc_result(static_cast<T>((std::numeric_limits<long>::max)()), k);
+ k = boost::math::ltrunc(static_cast<T>((std::numeric_limits<long>::min)()));
+ check_trunc_result(static_cast<T>((std::numeric_limits<long>::min)()), k);
+ }
#ifdef BOOST_HAS_LONG_LONG
if(abs(r) < (std::numeric_limits<boost::long_long_type>::max)())
@@ -166,6 +187,17 @@
r = boost::math::modf(arg, &ll);
check_modf_result(arg, r, ll);
}
+ if(std::numeric_limits<T>::digits >= std::numeric_limits<boost::long_long_type>::digits)
+ {
+ boost::long_long_type j = boost::math::llround(static_cast<T>((std::numeric_limits<boost::long_long_type>::max)()));
+ check_within_half(static_cast<T>((std::numeric_limits<boost::long_long_type>::max)()), j);
+ j = boost::math::llround(static_cast<T>((std::numeric_limits<boost::long_long_type>::min)()));
+ check_within_half(static_cast<T>((std::numeric_limits<boost::long_long_type>::min)()), j);
+ j = boost::math::lltrunc(static_cast<T>((std::numeric_limits<boost::long_long_type>::max)()));
+ check_trunc_result(static_cast<T>((std::numeric_limits<boost::long_long_type>::max)()), j);
+ j = boost::math::lltrunc(static_cast<T>((std::numeric_limits<boost::long_long_type>::min)()));
+ check_trunc_result(static_cast<T>((std::numeric_limits<boost::long_long_type>::min)()), j);
+ }
#endif
}
//
@@ -229,6 +261,40 @@
BOOST_CHECK_THROW(boost::math::lltrunc(std::numeric_limits<T>::quiet_NaN()), boost::math::rounding_error);
#endif
}
+ if(std::numeric_limits<T>::digits >= std::numeric_limits<int>::digits)
+ {
+ BOOST_CHECK_THROW(boost::math::itrunc(static_cast<T>((std::numeric_limits<int>::max)()) + 1), boost::math::rounding_error);
+ BOOST_CHECK_THROW(boost::math::itrunc(static_cast<T>((std::numeric_limits<int>::min)()) - 1), boost::math::rounding_error);
+ }
+ if(std::numeric_limits<T>::digits >= std::numeric_limits<long>::digits)
+ {
+ BOOST_CHECK_THROW(boost::math::ltrunc(static_cast<T>((std::numeric_limits<long>::max)()) + 1), boost::math::rounding_error);
+ BOOST_CHECK_THROW(boost::math::ltrunc(static_cast<T>((std::numeric_limits<long>::min)()) - 1), boost::math::rounding_error);
+ }
+#ifndef BOOST_NO_LONG_LONG
+ if(std::numeric_limits<T>::digits >= std::numeric_limits<boost::long_long_type>::digits)
+ {
+ BOOST_CHECK_THROW(boost::math::lltrunc(static_cast<T>((std::numeric_limits<boost::long_long_type>::max)()) + 1), boost::math::rounding_error);
+ BOOST_CHECK_THROW(boost::math::lltrunc(static_cast<T>((std::numeric_limits<boost::long_long_type>::min)()) - 1), boost::math::rounding_error);
+ }
+#endif
+ if(std::numeric_limits<T>::digits >= std::numeric_limits<int>::digits)
+ {
+ BOOST_CHECK_THROW(boost::math::iround(static_cast<T>((std::numeric_limits<int>::max)()) + 1), boost::math::rounding_error);
+ BOOST_CHECK_THROW(boost::math::iround(static_cast<T>((std::numeric_limits<int>::min)()) - 1), boost::math::rounding_error);
+ }
+ if(std::numeric_limits<T>::digits >= std::numeric_limits<long>::digits)
+ {
+ BOOST_CHECK_THROW(boost::math::lround(static_cast<T>((std::numeric_limits<long>::max)()) + 1), boost::math::rounding_error);
+ BOOST_CHECK_THROW(boost::math::lround(static_cast<T>((std::numeric_limits<long>::min)()) - 1), boost::math::rounding_error);
+ }
+#ifndef BOOST_NO_LONG_LONG
+ if(std::numeric_limits<T>::digits >= std::numeric_limits<boost::long_long_type>::digits)
+ {
+ BOOST_CHECK_THROW(boost::math::llround(static_cast<T>((std::numeric_limits<boost::long_long_type>::max)()) + 1), boost::math::rounding_error);
+ BOOST_CHECK_THROW(boost::math::llround(static_cast<T>((std::numeric_limits<boost::long_long_type>::min)()) - 1), boost::math::rounding_error);
+ }
+#endif
}
int test_main(int, char* [])
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk