]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/math/example/lexical_cast_native.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / math / example / lexical_cast_native.cpp
1 /** lexical_cast_nonfinite_facets.cpp
2 *
3 * Copyright (c) 2011 Paul A. Bristow
4 *
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)
8 *
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.
15 *
16 \file
17
18 \brief A very simple example of using lexical cast with
19 non_finite_num facet for C99 standard output of infinity and NaN.
20
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).
25
26 */
27
28 #include <boost/math/special_functions/nonfinite_num_facets.hpp>
29 using boost::math::nonfinite_num_get;
30 using boost::math::nonfinite_num_put;
31
32 #include <boost/lexical_cast.hpp>
33 using boost::lexical_cast;
34
35 #include <iostream>
36 using std::cout;
37 using std::endl;
38 using std::cerr;
39
40 #include <iomanip>
41 using std::setw;
42 using std::left;
43 using std::right;
44 using std::internal;
45
46 #include <string>
47 using std::string;
48
49 #include <sstream>
50 using std::istringstream;
51
52 #include <limits>
53 using std::numeric_limits;
54
55 #include <locale>
56 using std::locale;
57
58 #include <boost/math/tools/assert.hpp>
59
60 int main ()
61 {
62 std::cout << "lexical_cast example (NOT using finite_num_facet)." << std::endl;
63
64 if((std::numeric_limits<double>::has_infinity == false) || (std::numeric_limits<double>::infinity() == 0))
65 {
66 std::cout << "Infinity not supported on this platform." << std::endl;
67 return 0;
68 }
69
70 if((std::numeric_limits<double>::has_quiet_NaN == false) || (std::numeric_limits<double>::quiet_NaN() == 0))
71 {
72 std::cout << "NaN not supported on this platform." << std::endl;
73 return 0;
74 }
75
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).
78
79 // Use the current 'native' default locale.
80 std::locale default_locale (std::locale::classic ()); // Note the current (default C) locale.
81
82 // Create plus and minus infinity.
83 double plus_infinity = +std::numeric_limits<double>::infinity();
84 double minus_infinity = -std::numeric_limits<double>::infinity();
85
86 // and create a NaN (NotANumber).
87 double NaN = +std::numeric_limits<double>::quiet_NaN ();
88
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;
96
97 // Checks below are expected to fail on some platforms!
98
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());
103
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");
108
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");
113
114 return 0;
115 } // int main()
116
117 /*
118
119 Output:
120
121 from MSVC 10, fails (as expected)
122
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).
125 Using default locale
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.
131
132
133 */