]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/math/example/lexical_cast_native.cpp
1 /** lexical_cast_nonfinite_facets.cpp
3 * Copyright (c) 2011 Paul A. Bristow
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE_1_0.txt
7 * or copy at http://www.boost.org/LICENSE_1_0.txt)
9 * This very simple program illustrates how to use the
10 * `boost/math/nonfinite_num_facets.hpp' with lexical cast
11 * to obtain C99 representation of infinity and NaN.
12 * This example is from the original Floating Point Utilities contribution by Johan Rade.
13 * Floating Point Utility library has been accepted into Boost,
14 * but the utilities are incorporated into Boost.Math library.
18 \brief A very simple example of using lexical cast with
19 non_finite_num facet for C99 standard output of infinity and NaN.
21 \detail This example shows how to create a C99 non-finite locale,
22 and imbue input and output streams with the non_finite_num put and get facets.
23 This allows lexical_cast output and input of infinity and NaN in a Standard portable way,
24 This permits 'loop-back' of output back into input (and portably across different system too).
28 #include <boost/math/special_functions/nonfinite_num_facets.hpp>
29 using boost::math::nonfinite_num_get
;
30 using boost::math::nonfinite_num_put
;
32 #include <boost/lexical_cast.hpp>
33 using boost::lexical_cast
;
50 using std::istringstream
;
53 using std::numeric_limits
;
58 #include <boost/math/tools/assert.hpp>
62 std::cout
<< "lexical_cast example (NOT using finite_num_facet)." << std::endl
;
64 if((std::numeric_limits
<double>::has_infinity
== false) || (std::numeric_limits
<double>::infinity() == 0))
66 std::cout
<< "Infinity not supported on this platform." << std::endl
;
70 if((std::numeric_limits
<double>::has_quiet_NaN
== false) || (std::numeric_limits
<double>::quiet_NaN() == 0))
72 std::cout
<< "NaN not supported on this platform." << std::endl
;
76 // Some tests that are expected to fail on some platforms.
77 // (But these tests are expected to pass using non_finite num_put and num_get facets).
79 // Use the current 'native' default locale.
80 std::locale
default_locale (std::locale::classic ()); // Note the current (default C) locale.
82 // Create plus and minus infinity.
83 double plus_infinity
= +std::numeric_limits
<double>::infinity();
84 double minus_infinity
= -std::numeric_limits
<double>::infinity();
86 // and create a NaN (NotANumber).
87 double NaN
= +std::numeric_limits
<double>::quiet_NaN ();
89 // Output the nonfinite values using the current (default C) locale.
90 // The default representations differ from system to system,
91 // for example, using Microsoft compilers, 1.#INF, -1.#INF, and 1.#QNAN.
92 cout
<< "Using default locale" << endl
;
93 cout
<< "+std::numeric_limits<double>::infinity() = " << plus_infinity
<< endl
;
94 cout
<< "-std::numeric_limits<double>::infinity() = " << minus_infinity
<< endl
;
95 cout
<< "+std::numeric_limits<double>::quiet_NaN () = " << NaN
<< endl
;
97 // Checks below are expected to fail on some platforms!
99 // Now try some 'round-tripping', 'reading' "inf"
100 double x
= boost::lexical_cast
<double>("inf");
101 // and check we get a floating-point infinity.
102 BOOST_MATH_ASSERT(x
== std::numeric_limits
<double>::infinity());
104 // Check we can convert the other way from floating-point infinity,
105 string s
= boost::lexical_cast
<string
>(numeric_limits
<double>::infinity());
106 // to a C99 string representation as "inf".
107 BOOST_MATH_ASSERT(s
== "inf");
109 // Finally try full 'round-tripping' (in both directions):
110 BOOST_MATH_ASSERT(lexical_cast
<double>(lexical_cast
<string
>(numeric_limits
<double>::infinity()))
111 == numeric_limits
<double>::infinity());
112 BOOST_MATH_ASSERT(lexical_cast
<string
>(lexical_cast
<double>("inf")) == "inf");
121 from MSVC 10, fails (as expected)
123 lexical_cast_native.vcxproj -> J:\Cpp\fp_facet\fp_facet\Debug\lexical_cast_native.exe
124 lexical_cast example (NOT using finite_num_facet).
126 +std::numeric_limits<double>::infinity() = 1.#INF
127 -std::numeric_limits<double>::infinity() = -1.#INF
128 +std::numeric_limits<double>::quiet_NaN () = 1.#QNAN
129 C:\Program Files\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppCommon.targets(183,5): error MSB3073: The command ""J:\Cpp\fp_facet\fp_facet\Debug\lexical_cast_native.exe"
130 C:\Program Files\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppCommon.targets(183,5): error MSB3073: :VCEnd" exited with code 3.