[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [5010] Support infinities
From: |
Greg Chicares |
Subject: |
[lmi-commits] [5010] Support infinities |
Date: |
Tue, 22 Jun 2010 17:09:31 +0000 |
Revision: 5010
http://svn.sv.gnu.org/viewvc/?view=rev&root=lmi&revision=5010
Author: chicares
Date: 2010-06-22 17:09:31 +0000 (Tue, 22 Jun 2010)
Log Message:
-----------
Support infinities
Modified Paths:
--------------
lmi/trunk/ChangeLog
lmi/trunk/numeric_io_test.cpp
lmi/trunk/numeric_io_traits.hpp
Modified: lmi/trunk/ChangeLog
===================================================================
--- lmi/trunk/ChangeLog 2010-06-22 15:23:09 UTC (rev 5009)
+++ lmi/trunk/ChangeLog 2010-06-22 17:09:31 UTC (rev 5010)
@@ -26162,3 +26162,19 @@
ihs_basicval.cpp
Refactor.
+20100622T1522Z <address@hidden> [699]
+
+ numeric_io_traits.hpp
+Reformat documentation.
+
+20100622T1523Z <address@hidden> [698]
+
+ numeric_io_test.cpp
+Refactor.
+
+20100622T1709Z <address@hidden> [698]
+
+ numeric_io_test.cpp
+ numeric_io_traits.hpp
+Support infinities.
+
Modified: lmi/trunk/numeric_io_test.cpp
===================================================================
--- lmi/trunk/numeric_io_test.cpp 2010-06-22 15:23:09 UTC (rev 5009)
+++ lmi/trunk/numeric_io_test.cpp 2010-06-22 17:09:31 UTC (rev 5010)
@@ -28,6 +28,8 @@
#include "numeric_io_cast.hpp"
+#include "handle_exceptions.hpp"
+#include "ieee754.hpp" // infinity<>()
#include "miscellany.hpp"
#include "test_tools.hpp"
#include "timer.hpp"
@@ -36,7 +38,7 @@
# include <boost/lexical_cast.hpp>
#endif // ! defined __BORLANDC__
-#include <cmath> // std::exp()
+#include <cmath> // std::exp()
#include <limits>
#include <sstream>
#include <stdexcept>
@@ -94,6 +96,13 @@
#endif // ! defined __BORLANDC__
}
+void mete_infinity()
+{
+ std::string s = numeric_io_cast<std::string>(infinity<double>());
+ double d = numeric_io_cast<double>(s);
+ stifle_warning_for_unused_value(d);
+}
+
// These tests generally assume IEC 60559 floating point. Hardware
// that deviates from that standard is probably so rare that it can
// reasonably be ignored, with an appropriate runtime message.
@@ -123,6 +132,8 @@
BOOST_TEST_EQUAL( 3, floating_point_decimals(-1000.0f));
BOOST_TEST_EQUAL(15, floating_point_decimals(-1000.0L));
+ BOOST_TEST_EQUAL( 0, floating_point_decimals(infinity<double>()));
+
// Consider the number of exact decimal digits in the neighborhood
// of epsilon's reciprocal for type double, which is approximately
// 0.450359962737049596e16 .
@@ -151,19 +162,43 @@
BOOST_TEST_EQUAL( "0", simplify_floating_point( "0."));
BOOST_TEST_EQUAL( "-0", simplify_floating_point( "-0."));
BOOST_TEST_EQUAL( "nan", simplify_floating_point( "nan"));
+ BOOST_TEST_EQUAL( "inf", simplify_floating_point( "inf"));
+ BOOST_TEST_EQUAL( "-inf", simplify_floating_point( "-inf"));
- double volatile d;
-
std::cout
<< "Conversions:"
<< "\n 2/3, lmi : " << TimeAnAliquot(mete_two_thirds )
<< "\n 2/3, boost: " << TimeAnAliquot(mete_two_thirds_boost)
+ << "\n inf, lmi : " << TimeAnAliquot(mete_infinity )
<< std::endl
;
- std::cout << std::endl;
- stifle_warning_for_unused_value(d);
+ // Infinities.
+ double volatile const inf_dbl = std::numeric_limits<double>::infinity();
+ std::string const inf_str = numeric_io_cast<std::string>(inf_dbl);
+
+ // This test fails for como with msvcrt, because the latter
+ // defectively prints infinity as "1.#INF". Distressingly,
+ // that converts to '1.0'.
+ BOOST_TEST_EQUAL(inf_dbl, numeric_io_cast<double>(inf_str));
+
+ // These conversions fail for como with msvcrt, because the latter
+ // defectively prints infinity as "1.#INF"; and for borland
+ // (FWIW), which prints infinity as "+INF".
+ try
+ {
+ BOOST_TEST_EQUAL(inf_dbl, numeric_io_cast<double>("inf"));
+ BOOST_TEST_EQUAL(inf_dbl, numeric_io_cast<double>("INF"));
+ BOOST_TEST_EQUAL(inf_dbl, numeric_io_cast<double>("infinity"));
+ BOOST_TEST_EQUAL(inf_dbl, numeric_io_cast<double>("INFINITY"));
+ }
+ catch(...)
+ {
+ report_exception();
+ BOOST_TEST(false);
+ }
+
// Interpreted as decimal, not as octal.
BOOST_TEST_EQUAL(77, numeric_io_cast<int>( "077"));
Modified: lmi/trunk/numeric_io_traits.hpp
===================================================================
--- lmi/trunk/numeric_io_traits.hpp 2010-06-22 15:23:09 UTC (rev 5009)
+++ lmi/trunk/numeric_io_traits.hpp 2010-06-22 17:09:31 UTC (rev 5010)
@@ -26,6 +26,8 @@
#include "config.hpp"
+#include "ieee754.hpp" // is_infinite<>()
+
#include <algorithm> // std::max()
#include <cmath> // C99 functions fabsl(), log10l(), strtold()
#include <cstdlib> // std::strto*()
@@ -78,8 +80,8 @@
inline int floating_point_decimals(T t)
{
BOOST_STATIC_ASSERT(boost::is_float<T>::value);
- // Avoid taking zero's logarithm.
- if(0 == t)
+ // Avoid taking the logarithm of zero or infinity.
+ if(0 == t || is_infinite(t))
{
return 0;
}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [lmi-commits] [5010] Support infinities,
Greg Chicares <=