]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/math/example/nonfinite_facet_sstream.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / math / example / nonfinite_facet_sstream.cpp
1 // nonfinite_facet_sstream.cpp
2
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)
6
7 // Copyright (c) 2006 Johan Rade
8 // Copyright (c) 2011 Paul A. Bristow
9
10 /*!
11 \file
12 \brief Examples of nonfinite with output and input facets and stringstreams.
13
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).
17
18 */
19
20 #include <boost/math/special_functions/nonfinite_num_facets.hpp>
21 using boost::math::nonfinite_num_put;
22 using boost::math::nonfinite_num_get;
23
24 using boost::math::legacy;
25
26 #include <iostream>
27 using std::cout;
28 using std::endl;
29 #include <locale>
30 using std::locale;
31
32 #include <sstream>
33 using std::stringstream;
34 using std::istringstream;
35 using std::ostringstream;
36
37 #include <limits>
38 using std::numeric_limits;
39
40 #include <assert.h>
41
42 int main()
43 {
44 //[nonfinite_facets_sstream_1
45 locale old_locale;
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]
49
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.
52
53 {
54 ostringstream oss;
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");
60 }
61 {
62 istringstream iss;
63 iss.str("inf");
64 iss.imbue(new_locale);
65 double inf;
66 iss >> inf; // Read from "inf"
67 cout << "Infinity input was " << iss.str() << endl;
68 BOOST_MATH_ASSERT(inf == numeric_limits<double>::infinity());
69 }
70
71 {
72 //[nonfinite_facets_sstream_2
73 stringstream ss;
74 ss.imbue(new_locale);
75 double inf = numeric_limits<double>::infinity();
76 ss << inf; // Write out.
77 BOOST_MATH_ASSERT(ss.str() == "inf");
78 double r;
79 ss >> r; // Read back in.
80 BOOST_MATH_ASSERT(inf == r); // Confirms that the double values really are identical.
81
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.
89
90 //] [/nonfinite_facets_sstream_2]
91 }
92
93 {
94 stringstream ss;
95 ss.imbue(new_locale);
96
97 double nan = numeric_limits<double>::quiet_NaN();
98 ss << nan; // Write out.
99 BOOST_MATH_ASSERT(ss.str() == "nan");
100
101 double v;
102 ss >> v; // Read back in.
103
104 cout << "NaN output was " << ss.str() << endl;
105 cout << "NaN input was " << v << endl;
106
107 // assert(nan == v); // Always fails because NaN == NaN fails!
108 // assert(nan == numeric_limits<double>::quiet_NaN()); asserts!
109
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.
115 }
116
117 } // int main()
118
119
120 /*
121 //[nonfinite_facet_sstream_output
122
123 infinity output was inf
124 Infinity input was inf
125 infinity output was inf
126 infinity input was 1.#INF
127 NaN output was nan
128 NaN input was 1.#QNAN
129
130 //] [nonfinite_facet_sstream_output]
131 */
132