1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
|
/** nonfinite_num_facet_serialization.cpp
*
* Copyright (c) 2011 Francois Mauger
* Copyright (c) 2011 Paul A. Bristow
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt
* or copy at http://www.boost.org/LICENSE_1_0.txt)
*
* 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.46.1 MSVC 10.0 32 bit.
*/
#ifdef _MSC_VER
# pragma warning(push)
//# pragma warning(disable : 4100) // unreferenced formal parameter.
#endif
#include <iostream>
#include <sstream>
#include <fstream>
#include <limits>
#include <boost/cstdint.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/codecvt_null.hpp>
// 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_CXX11_NUMERIC_LIMITS
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.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 :
void plus_infinity ()
{
fvalue = +std::numeric_limits<float>::infinity ();
dvalue = +std::numeric_limits<double>::infinity ();
return;
}
// Set the values at NaN :
void nan ()
{
fvalue = +std::numeric_limits<float>::quiet_NaN ();
dvalue = +std::numeric_limits<double>::quiet_NaN ();
return;
}
// Print :
void print (std::ostream & a_out, const std::string & a_title)
{
if (a_title.empty ()) a_out << "foo";
else a_out << a_title;
a_out << " : " << std::endl;
a_out << "|-- " << "fvalue = ";
a_out.precision (7);
a_out << fvalue << std::endl;
a_out << "`-- " << "dvalue = ";
a_out.precision (15);
a_out << dvalue << std::endl;
return;
}
// I/O operators :
friend std::ostream & operator<< (std::ostream & a_out, const foo & a_foo);
friend std::istream & operator>> (std::istream & a_in, foo & a_foo);
// Boost serialization :
template <class Archive>
void serialize (Archive & ar, int /*version*/)
{
ar & BOOST_SERIALIZATION_NVP (fvalue);
ar & BOOST_SERIALIZATION_NVP (dvalue);
return;
}
// Attributes :
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)
{ // 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 != '(')
{
std::cerr << "ERROR: operator>> No ( " << std::endl;
a_in.setstate(std::ios::failbit);
return a_in;
}
float f;
a_in >> std::ws >> f;
if (! a_in)
{
return a_in;
}
a_in >> std::ws;
a_in.get (c);
if (c != sep)
{
std::cerr << "ERROR: operator>> c='" << c << "'" << std::endl;
std::cerr << "ERROR: operator>> No '" << sep << "'" << std::endl;
a_in.setstate(std::ios::failbit);
return a_in;
}
double d;
a_in >> std::ws >> d;
if (! a_in)
{
return a_in;
}
a_in >> std::ws;
a_in.get (c);
if (c != ')')
{
std::cerr << "ERROR: operator>> No ) " << std::endl;
a_in.setstate(std::ios::failbit);
return a_in;
}
a_foo.fvalue = f;
a_foo.dvalue = d;
return a_in;
}
int main (void)
{
std::clog << std::endl
<< "Nonfinite_serialization.cpp' example program." << std::endl;
#ifdef BOOST_NO_CXX11_NUMERIC_LIMITS
std::cout << "BOOST_NO_CXX11_NUMERIC_LIMITS is defined, so no max_digits10 available either,"
"using our own version instead." << std::endl;
#endif
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 << "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 (max_digits10_double);
oss << f0 << f1 << f2 << f3;
std::clog << "Output is: `" << oss.str () << "'" << std::endl;
std::clog << "Done output to ostringstream." << std::endl;
}
{
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>);
std::istringstream iss (the_string);
iss.imbue (the_in_locale);
foo f0, f1, f2, f3;
iss >> f0 >> f1 >> f2 >> f3;
if (! iss)
{
std::cerr << "Format error !" << std::endl;
}
else
{
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 input from istringstream." << 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>);
// Use a temporary folder .temps (which contains "boost-no-inspect" so that it will not be inspected, and made 'hidden' too).
std::ofstream fout ("./.temps/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;
toar & f3;
std::clog << "Done." << 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>);
// Use a temporary folder .temps (which contains "boost-no-inspect" so that it will not be inspected, and made 'hidden' too).
std::ifstream fin ("./.temps/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");
f3.print (std::clog, "f3");
std::clog << "Done." << 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>);
// Use a temporary folder /.temps (which contains "boost-no-inspect" so that it will not be inspected, and made 'hidden' too).
std::ofstream fout ("./.temps/nonfinite_XML_archive_test.txt");
fout.imbue (the_out_locale);
boost::archive::xml_oarchive xoar (fout, boost::archive::no_codecvt);
xoar & BOOST_SERIALIZATION_NVP (f0);
xoar & BOOST_SERIALIZATION_NVP (f1);
xoar & BOOST_SERIALIZATION_NVP (f2);
xoar & BOOST_SERIALIZATION_NVP (f3);
std::clog << "Done." << 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>);
// Use a temporary folder /.temps (which contains "boost-no-inspect" so that it will not be inspected, and made 'hidden' too).
std::ifstream fin ("./.temps/nonfinite_XML_archive_test.txt"); // Previously written above.
fin.imbue (the_in_locale);
boost::archive::xml_iarchive xiar (fin, boost::archive::no_codecvt);
foo f0, f1, f2, f3;
xiar & BOOST_SERIALIZATION_NVP (f0);
xiar & BOOST_SERIALIZATION_NVP (f1);
xiar & BOOST_SERIALIZATION_NVP (f2);
xiar & BOOST_SERIALIZATION_NVP (f3);
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 << "End nonfinite_serialization.cpp' example program." << std::endl;
return 0;
}
/*
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.
*/
|