Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r71392 - trunk/libs/math/example
From: pbristow_at_[hidden]
Date: 2011-04-20 05:19:23


Author: pbristow
Date: 2011-04-20 05:19:21 EDT (Wed, 20 Apr 2011)
New Revision: 71392
URL: http://svn.boost.org/trac/boost/changeset/71392

Log:
Provided workaround for older compilers without max_digits10.
Text files modified:
   trunk/libs/math/example/nonfinite_num_facet_serialization.cpp | 219 +++++++++++++++++++++++++++++++--------
   1 files changed, 172 insertions(+), 47 deletions(-)

Modified: trunk/libs/math/example/nonfinite_num_facet_serialization.cpp
==============================================================================
--- trunk/libs/math/example/nonfinite_num_facet_serialization.cpp (original)
+++ trunk/libs/math/example/nonfinite_num_facet_serialization.cpp 2011-04-20 05:19:21 EDT (Wed, 20 Apr 2011)
@@ -7,23 +7,20 @@
  * (See accompanying file LICENSE_1_0.txt
  * or copy at http://www.boost.org/LICENSE_1_0.txt)
  *
- * This sample program illustrates how to use the
+ * This sample program by Francois Mauger illustrates how to use the
  * `boost/math/nonfinite_num_facets.hpp' material from the original
  * Floating Point Utilities contribution by Johan Rade. Here it is
  * shown how non finite floating number can be serialized and
  * deserialized from I/O streams and/or Boost text/XML archives. It
  * produces two archives stored in `test.txt' and `test.xml' files.
  *
- * Tested with Boost 1.44, gcc 4.4.1, Linux/i686 (32bits)
- *
+ * Tested with Boost 1.44, gcc 4.4.1, Linux/i686 (32bits).
+ * Tested with Boost.1.46.1 MSVC 10.0 32 bit.
  */
 
-
 #ifdef _MSC_VER
 # pragma warning(push)
-
-# pragma warning(disable : 4224) // formal parameter 'version' was previously defined as a type
-# pragma warning(disable : 4100) // unreferenced formal parameter
+//# pragma warning(disable : 4100) // unreferenced formal parameter.
 #endif
 
 #include <iostream>
@@ -39,35 +36,53 @@
 #include <boost/archive/xml_iarchive.hpp>
 #include <boost/archive/codecvt_null.hpp>
 
-// from Johan Rade Floating Point Utilities :
+// from the Floating Point Utilities :
 #include <boost/math/special_functions/nonfinite_num_facets.hpp>
 
+static const char sep = ','; // Separator of bracketed float and double values.
+
+// Use max_digits10 (or equivalent) to obtain
+// all potentially significant decimal digits for the floating-point types.
+
+#ifdef BOOST_NO_NUMERIC_LIMITS_LOWEST
+ cout << "BOOST_NO_NUMERIC_LIMITS_LOWEST is defined, so no max_digits10 available." << endl;
+ std::streamsize max_digits10_float = 2 + std::numeric_limits<float>::digits * 30103UL / 100000UL;
+ std::streamsize max_digits10_double = 2 + std::numeric_limits<double>::digits * 30103UL / 100000UL;
+#else
+ // Can use new C++0X max_digits10 (the maximum potentially significant digits).
+ std::streamsize max_digits10_float = std::numeric_limits<float>::max_digits10;
+ std::streamsize max_digits10_double = std::numeric_limits<double>::max_digits10;
+#endif
+
+
 /* A class with a float and a double */
 struct foo
 {
- foo () : fvalue (3.141593F), dvalue (3.14159265358979) {}
- // set the values at -infinity :
+ foo () : fvalue (3.1415927F), dvalue (3.1415926535897931)
+ { // Construct using 32 and 64-bit max_digits10 decimal digits value of pi.
+ }
+ // Set the values at -infinity :
   void minus_infinity ()
   {
     fvalue = -std::numeric_limits<float>::infinity ();
     dvalue = -std::numeric_limits<double>::infinity ();
     return;
   }
- // set the values at +infinity :
+ // Set the values at +infinity :
   void plus_infinity ()
   {
     fvalue = +std::numeric_limits<float>::infinity ();
     dvalue = +std::numeric_limits<double>::infinity ();
     return;
   }
- // set the values at NaN :
+ // Set the values at NaN :
   void nan ()
   {
     fvalue = +std::numeric_limits<float>::quiet_NaN ();
     dvalue = +std::numeric_limits<double>::quiet_NaN ();
     return;
   }
- // print :
+ // Print :
   void print (std::ostream & a_out, const std::string & a_title)
   {
     if (a_title.empty ()) a_out << "foo";
@@ -96,18 +111,22 @@
   }
 
   // Attributes :
- float fvalue; // single precision floating number
- double dvalue; // double precision floating number
+ float fvalue; // Single precision floating-point number.
+ double dvalue; // Double precision floating-point number.
 };
 
 std::ostream & operator<< (std::ostream & a_out, const foo & a_foo)
-{
- a_out << "(" << a_foo.fvalue << ";" << a_foo.dvalue << ")";
+{ // Output bracketed FPs, for example "(3.1415927,3.1415926535897931)"
+ a_out.precision (max_digits10_float);
+ a_out << "(" << a_foo.fvalue << sep ;
+ a_out.precision (max_digits10_double);
+ a_out << a_foo.dvalue << ")";
   return a_out;
 }
 
 std::istream & operator>> (std::istream & a_in, foo & a_foo)
-{
+{ // Input bracketed floating-point values into a foo structure,
+ // for example from "(3.1415927,3.1415926535897931)"
   char c = 0;
   a_in.get (c);
   if (c != '(')
@@ -124,10 +143,10 @@
   }
   a_in >> std::ws;
   a_in.get (c);
- if (c != ';')
+ if (c != sep)
   {
     std::cerr << "ERROR: operator>> c='" << c << "'" << std::endl;
- std::cerr << "ERROR: operator>> No ; " << std::endl;
+ std::cerr << "ERROR: operator>> No '" << sep << "'" << std::endl;
     a_in.setstate(std::ios::failbit);
     return a_in;
   }
@@ -152,36 +171,43 @@
 
 int main (void)
 {
- std::clog << "Hello Booster !" << std::endl
- << "This is the `test_nonfinite_num_facets_1.cpp' sample program !" << std::endl;
+ std::clog << std::endl
+ << "Nonfinite_serialization.cpp' example program." << std::endl;
+
+
+ std::cout << "std::numeric_limits<float>::max_digits10 is " << max_digits10_float << std::endl;
+ std::cout << "std::numeric_limits<double>::max_digits10 is " << max_digits10_double << std::endl;
 
   std::locale the_default_locale (std::locale::classic (),
           new boost::archive::codecvt_null<char>);
 
+ // Demonstrate use of nonfinite facets with stringstreams.
   {
- std::clog << "Write to some string buffer..." << std::endl;
+ std::clog << "Construct some foo structures with a finite and nonfinites." << std::endl;
     foo f0;
     foo f1; f1.minus_infinity ();
     foo f2; f2.plus_infinity ();
     foo f3; f3.nan ();
-
+ // Display them.
     f0.print (std::clog, "f0");
     f1.print (std::clog, "f1");
     f2.print (std::clog, "f2");
     f3.print (std::clog, "f3");
+ std::clog << " Write to a string buffer." << std::endl;
 
     std::ostringstream oss;
     std::locale the_out_locale (the_default_locale, new boost::math::nonfinite_num_put<char>);
     oss.imbue (the_out_locale);
- oss.precision (15);
+ oss.precision (max_digits10_double);
     oss << f0 << f1 << f2 << f3;
     std::clog << "Output is: `" << oss.str () << "'" << std::endl;
- std::clog << "Done." << std::endl;
+ std::clog << "Done output to ostringstream." << std::endl;
   }
 
   {
- std::clog << "Read from to some string buffer..." << std::endl;
- std::string the_string = "(3.14159;3.14159)(-inf;-inf)(inf;inf)(nan;nan)";
+ std::clog << "Read foo structures from a string buffer." << std::endl;
+
+ std::string the_string = "(3.1415927,3.1415926535897931)(-inf,-inf)(inf,inf)(nan,nan)";
     std::clog << "Input is: `" << the_string << "'" << std::endl;
 
     std::locale the_in_locale (the_default_locale, new boost::math::nonfinite_num_get<char>);
@@ -196,32 +222,33 @@
     }
     else
     {
- std::cerr << "Success !" << std::endl;
+ std::cerr << "Read OK." << std::endl;
       f0.print (std::clog, "f0");
       f1.print (std::clog, "f1");
       f2.print (std::clog, "f2");
       f3.print (std::clog, "f3");
     }
- std::clog << "Done." << std::endl;
+ std::clog << "Done input from istringstream." << std::endl;
   }
 
- {
- std::clog << "Serialize (boost text archive)..." << std::endl;
+ { // Demonstrate use of nonfinite facets for Serialization with Boost text archives.
+ std::clog << "Serialize (using Boost text archive)." << std::endl;
+ // Construct some foo structures with a finite and nonfinites.
     foo f0;
     foo f1; f1.minus_infinity ();
     foo f2; f2.plus_infinity ();
     foo f3; f3.nan ();
-
+ // Display them.
     f0.print (std::clog, "f0");
     f1.print (std::clog, "f1");
     f2.print (std::clog, "f2");
     f3.print (std::clog, "f3");
 
     std::locale the_out_locale (the_default_locale, new boost::math::nonfinite_num_put<char>);
- std::ofstream fout ("test.txt");
+ std::ofstream fout ("nonfinite_archive_test.txt");
     fout.imbue (the_out_locale);
     boost::archive::text_oarchive toar (fout, boost::archive::no_codecvt);
-
+ // Write to archive.
     toar & f0;
     toar & f1;
     toar & f2;
@@ -230,18 +257,18 @@
   }
 
   {
- std::clog << "Deserialize (boost text archive)..." << std::endl;
+ std::clog << "Deserialize (Boost text archive)..." << std::endl;
     std::locale the_in_locale (the_default_locale, new boost::math::nonfinite_num_get<char>);
- std::ifstream fin ("test.txt");
+ std::ifstream fin ("nonfinite_archive_test.txt");
     fin.imbue (the_in_locale);
     boost::archive::text_iarchive tiar (fin, boost::archive::no_codecvt);
     foo f0, f1, f2, f3;
-
+ // Read from archive.
     tiar & f0;
     tiar & f1;
     tiar & f2;
     tiar & f3;
-
+ // Display foos.
     f0.print (std::clog, "f0");
     f1.print (std::clog, "f1");
     f2.print (std::clog, "f2");
@@ -250,20 +277,21 @@
     std::clog << "Done." << std::endl;
   }
 
- {
- std::clog << "Serialize (boost XML archive)..." << std::endl;
+ { // Demonstrate use of nonfinite facets for Serialization with Boost XML Archive.
+ std::clog << "Serialize (Boost XML archive)..." << std::endl;
+ // Construct some foo structures with a finite and nonfinites.
     foo f0;
     foo f1; f1.minus_infinity ();
     foo f2; f2.plus_infinity ();
     foo f3; f3.nan ();
-
+ // Display foos.
     f0.print (std::clog, "f0");
     f1.print (std::clog, "f1");
     f2.print (std::clog, "f2");
     f3.print (std::clog, "f3");
 
     std::locale the_out_locale (the_default_locale, new boost::math::nonfinite_num_put<char>);
- std::ofstream fout ("test.xml");
+ std::ofstream fout ("nonfinite_XML_archive_test.txt");
     fout.imbue (the_out_locale);
     boost::archive::xml_oarchive xoar (fout, boost::archive::no_codecvt);
 
@@ -275,9 +303,9 @@
   }
 
   {
- std::clog << "Deserialize (boost XML archive)..." << std::endl;
+ std::clog << "Deserialize (Boost XML archive)..." << std::endl;
     std::locale the_in_locale (the_default_locale, new boost::math::nonfinite_num_get<char>);
- std::ifstream fin ("test.xml");
+ std::ifstream fin ("nonfinite_XML_archive_test.txt");
     fin.imbue (the_in_locale);
     boost::archive::xml_iarchive xiar (fin, boost::archive::no_codecvt);
     foo f0, f1, f2, f3;
@@ -295,8 +323,105 @@
     std::clog << "Done." << std::endl;
   }
 
- std::clog << "Bye ! " << std::endl;
+ std::clog << "End nonfinite_serialization.cpp' example program." << std::endl;
   return 0;
 }
 
- /* end of test_nonfinite_num_facets_1.cpp */
+/*
+
+Output:
+
+ Nonfinite_serialization.cpp' example program.
+ std::numeric_limits<float>::max_digits10 is 8
+ std::numeric_limits<double>::max_digits10 is 17
+ Construct some foo structures with a finite and nonfinites.
+ f0 :
+ |-- fvalue = 3.141593
+ `-- dvalue = 3.14159265358979
+ f1 :
+ |-- fvalue = -1.#INF
+ `-- dvalue = -1.#INF
+ f2 :
+ |-- fvalue = 1.#INF
+ `-- dvalue = 1.#INF
+ f3 :
+ |-- fvalue = 1.#QNAN
+ `-- dvalue = 1.#QNAN
+ Write to a string buffer.
+ Output is: `(3.1415927,3.1415926535897931)(-inf,-inf)(inf,inf)(nan,nan)'
+ Done output to ostringstream.
+ Read foo structures from a string buffer.
+ Input is: `(3.1415927,3.1415926535897931)(-inf,-inf)(inf,inf)(nan,nan)'
+ Read OK.
+ f0 :
+ |-- fvalue = 3.141593
+ `-- dvalue = 3.14159265358979
+ f1 :
+ |-- fvalue = -1.#INF
+ `-- dvalue = -1.#INF
+ f2 :
+ |-- fvalue = 1.#INF
+ `-- dvalue = 1.#INF
+ f3 :
+ |-- fvalue = 1.#QNAN
+ `-- dvalue = 1.#QNAN
+ Done input from istringstream.
+ Serialize (using Boost text archive).
+ f0 :
+ |-- fvalue = 3.141593
+ `-- dvalue = 3.14159265358979
+ f1 :
+ |-- fvalue = -1.#INF
+ `-- dvalue = -1.#INF
+ f2 :
+ |-- fvalue = 1.#INF
+ `-- dvalue = 1.#INF
+ f3 :
+ |-- fvalue = 1.#QNAN
+ `-- dvalue = 1.#QNAN
+ Done.
+ Deserialize (Boost text archive)...
+ f0 :
+ |-- fvalue = 3.141593
+ `-- dvalue = 3.14159265358979
+ f1 :
+ |-- fvalue = -1.#INF
+ `-- dvalue = -1.#INF
+ f2 :
+ |-- fvalue = 1.#INF
+ `-- dvalue = 1.#INF
+ f3 :
+ |-- fvalue = 1.#QNAN
+ `-- dvalue = 1.#QNAN
+ Done.
+ Serialize (Boost XML archive)...
+ f0 :
+ |-- fvalue = 3.141593
+ `-- dvalue = 3.14159265358979
+ f1 :
+ |-- fvalue = -1.#INF
+ `-- dvalue = -1.#INF
+ f2 :
+ |-- fvalue = 1.#INF
+ `-- dvalue = 1.#INF
+ f3 :
+ |-- fvalue = 1.#QNAN
+ `-- dvalue = 1.#QNAN
+ Done.
+ Deserialize (Boost XML archive)...
+ f0 :
+ |-- fvalue = 3.141593
+ `-- dvalue = 3.14159265358979
+ f1 :
+ |-- fvalue = -1.#INF
+ `-- dvalue = -1.#INF
+ f2 :
+ |-- fvalue = 1.#INF
+ `-- dvalue = 1.#INF
+ f3 :
+ |-- fvalue = 1.#QNAN
+ `-- dvalue = 1.#QNAN
+ Done.
+ End nonfinite_serialization.cpp' example program.
+
+ */


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