]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/math/example/nonfinite_facet_sstream.cpp
1 // nonfinite_facet_sstream.cpp
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt
5 // or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // Copyright (c) 2006 Johan Rade
8 // Copyright (c) 2011 Paul A. Bristow
12 \brief Examples of nonfinite with output and input facets and stringstreams.
14 \detail Construct a new locale with the nonfinite_num_put and nonfinite_num_get
15 facets and imbue istringstream, ostringstream and stringstreams,
16 showing output and input (and loopback for the stringstream).
20 #include <boost/math/special_functions/nonfinite_num_facets.hpp>
21 using boost::math::nonfinite_num_put
;
22 using boost::math::nonfinite_num_get
;
24 using boost::math::legacy
;
33 using std::stringstream
;
34 using std::istringstream
;
35 using std::ostringstream
;
38 using std::numeric_limits
;
44 //[nonfinite_facets_sstream_1
46 locale
tmp_locale(old_locale
, new nonfinite_num_put
<char>);
47 locale
new_locale(tmp_locale
, new nonfinite_num_get
<char>);
48 //] [/nonfinite_facets_sstream_1]
50 // Note that to add two facets, nonfinite_num_put and nonfinite_num_get,
51 // you have to add one at a time, using a temporary locale.
55 oss
.imbue(new_locale
);
56 double inf
= numeric_limits
<double>::infinity();
57 oss
<< inf
; // Write out.
58 cout
<< "infinity output was " << oss
.str() << endl
;
59 BOOST_MATH_ASSERT(oss
.str() == "inf");
64 iss
.imbue(new_locale
);
66 iss
>> inf
; // Read from "inf"
67 cout
<< "Infinity input was " << iss
.str() << endl
;
68 BOOST_MATH_ASSERT(inf
== numeric_limits
<double>::infinity());
72 //[nonfinite_facets_sstream_2
75 double inf
= numeric_limits
<double>::infinity();
76 ss
<< inf
; // Write out.
77 BOOST_MATH_ASSERT(ss
.str() == "inf");
79 ss
>> r
; // Read back in.
80 BOOST_MATH_ASSERT(inf
== r
); // Confirms that the double values really are identical.
82 cout
<< "infinity output was " << ss
.str() << endl
;
83 cout
<< "infinity input was " << r
<< endl
;
84 // But the string representation of r displayed will be the native type
85 // because, when it was constructed, cout had NOT been imbued
86 // with the new locale containing the nonfinite_numput facet.
87 // So the cout output will be "1.#INF on MS platforms
88 // and may be "inf" or other string representation on other platforms.
90 //] [/nonfinite_facets_sstream_2]
97 double nan
= numeric_limits
<double>::quiet_NaN();
98 ss
<< nan
; // Write out.
99 BOOST_MATH_ASSERT(ss
.str() == "nan");
102 ss
>> v
; // Read back in.
104 cout
<< "NaN output was " << ss
.str() << endl
;
105 cout
<< "NaN input was " << v
<< endl
;
107 // assert(nan == v); // Always fails because NaN == NaN fails!
108 // assert(nan == numeric_limits<double>::quiet_NaN()); asserts!
110 // And the string representation will be the native type
111 // because cout has NOT been imbued with a locale containing
112 // the nonfinite_numput facet.
113 // So the output will be "1.#QNAN on MS platforms
114 // and may be "nan" or other string representation on other platforms.
121 //[nonfinite_facet_sstream_output
123 infinity output was inf
124 Infinity input was inf
125 infinity output was inf
126 infinity input was 1.#INF
128 NaN input was 1.#QNAN
130 //] [nonfinite_facet_sstream_output]