]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/math/example/lexical_cast_nonfinite_facets.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).
26 See also lexical_cast_native.cpp which is expected to fail on many systems,
27 but might succeed if the default locale num_put and num_get facets
28 comply with C99 nonfinite input and output specification.
32 #include <boost/math/special_functions/nonfinite_num_facets.hpp>
33 using boost::math::nonfinite_num_get
;
34 using boost::math::nonfinite_num_put
;
36 #include <boost/lexical_cast.hpp>
37 using boost::lexical_cast
;
54 using std::istringstream
;
57 using std::numeric_limits
;
62 #include <boost/assert.hpp>
66 std::cout
<< "finite_num_facet with lexical_cast example." << std::endl
;
68 // Example of using non_finite num_put and num_get facets with lexical_cast.
70 //locale tmp_locale(old_locale, new nonfinite_num_put<char>);
71 //// Create a new temporary output locale, and add the output nonfinite_num_put facet.
73 //locale new_locale(tmp_locale, new nonfinite_num_get<char>);
74 // Create a new output locale (from the tmp locale), and add the input nonfinite_num_get facet.
76 // Note that you can only add facets one at a time,
77 // unless you chain thus:
79 std::locale
new_locale(std::locale(std::locale(),
80 new boost::math::nonfinite_num_put
<char>),
81 new boost::math::nonfinite_num_get
<char>);
83 locale::global(new_locale
); // Newly constructed streams
84 // (including those streams inside lexical_cast)
85 // now use new_locale with nonfinite facets.
87 // Output using the new locale.
88 cout
<< "Using C99_out_locale " << endl
;
89 cout
.imbue(new_locale
);
90 // Necessary because cout already constructed using default C locale,
91 // and default facets for nonfinites.
93 // Create plus and minus infinity.
94 double plus_infinity
= +std::numeric_limits
<double>::infinity();
95 double minus_infinity
= -std::numeric_limits
<double>::infinity();
97 // and create a NaN (NotANumber)
98 double NaN
= +std::numeric_limits
<double>::quiet_NaN ();
99 cout
<< "+std::numeric_limits<double>::infinity() = " << plus_infinity
<< endl
;
100 cout
<< "-std::numeric_limits<double>::infinity() = " << minus_infinity
<< endl
;
101 cout
<< "+std::numeric_limits<double>::quiet_NaN () = " << NaN
<< endl
;
103 // Now try some 'round-tripping', 'reading' "inf".
104 double x
= boost::lexical_cast
<double>("inf");
105 // and check we get a floating-point infinity.
106 BOOST_ASSERT(x
== std::numeric_limits
<double>::infinity());
107 cout
<< "boost::lexical_cast<double>(\"inf\") = " << x
<< endl
;
109 // Check we can convert the other way from floating-point infinity,
110 string s
= boost::lexical_cast
<string
>(numeric_limits
<double>::infinity());
111 // to a C99 string representation as "inf".
112 BOOST_ASSERT(s
== "inf");
114 // Finally try full 'round-tripping' (in both directions):
115 BOOST_ASSERT(lexical_cast
<double>(lexical_cast
<string
>(numeric_limits
<double>::infinity()))
116 == numeric_limits
<double>::infinity());
117 BOOST_ASSERT(lexical_cast
<string
>(lexical_cast
<double>("inf")) == "inf");
125 finite_num_facet with lexical_cast example.
127 +std::numeric_limits<double>::infinity() = inf
128 -std::numeric_limits<double>::infinity() = -inf
129 +std::numeric_limits<double>::quiet_NaN () = nan
130 boost::lexical_cast<double>("inf") = inf