• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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.
29 
30 */
31 
32 #include <boost/math/special_functions/nonfinite_num_facets.hpp>
33 using boost::math::nonfinite_num_get;
34 using boost::math::nonfinite_num_put;
35 
36 #include <boost/lexical_cast.hpp>
37 using boost::lexical_cast;
38 
39 #include <iostream>
40 using std::cout;
41 using std::endl;
42 using std::cerr;
43 
44 #include <iomanip>
45 using std::setw;
46 using std::left;
47 using std::right;
48 using std::internal;
49 
50 #include <string>
51 using std::string;
52 
53 #include <sstream>
54 using std::istringstream;
55 
56 #include <limits>
57 using std::numeric_limits;
58 
59 #include <locale>
60 using std::locale;
61 
62 #include <boost/assert.hpp>
63 
main()64 int main ()
65 {
66   std::cout << "finite_num_facet with lexical_cast example." << std::endl;
67 
68   // Example of using non_finite num_put and num_get facets with lexical_cast.
69   //locale old_locale;
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.
72 
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.
75 
76   // Note that you can only add facets one at a time,
77   // unless you chain thus:
78 
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>);
82 
83   locale::global(new_locale); // Newly constructed streams
84   // (including those streams inside lexical_cast)
85   // now use new_locale with nonfinite facets.
86 
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.
92 
93     // Create plus and minus infinity.
94   double plus_infinity = +std::numeric_limits<double>::infinity();
95   double minus_infinity = -std::numeric_limits<double>::infinity();
96 
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;
102 
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;
108 
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");
113 
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");
118 
119     return 0;
120 } // int main()
121 
122 /*
123 
124 Output:
125   finite_num_facet with lexical_cast example.
126   Using C99_out_locale
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
131 
132 
133 */
134