Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / math / example / nonfinite_legacy.cpp
1 // nonfinite_legacy.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 Basic tests of nonfinite loopback with output and input facet.
13
14 \detail Basic loopback test outputs using the so-called 'legacy' facets,
15 "1.#INF"  and "1.#QNAN".
16
17 and reads back in using nonfinite input 'legacy' facet, and
18 (if possible) checks if loopback OK.
19
20 */
21
22 #include <boost/math/special_functions/nonfinite_num_facets.hpp>
23 using boost::math::nonfinite_num_put;
24 using boost::math::nonfinite_num_get;
25
26 using boost::math::legacy;
27
28 #include <iostream>
29 using std::cout;
30 using std::endl;
31
32 #include <iomanip>
33 using std::setfill;
34 using std::setw;
35
36 #include <locale>
37 using std::locale;
38
39 #include <sstream>
40 using std::stringstream;
41 #include <limits>
42 using std::numeric_limits;
43
44 #include <assert.h>
45
46 int main()
47 {
48   // Create a new locale with both the nonfinite facets.
49   std::locale new_locale(std::locale(std::locale(),
50     new boost::math::nonfinite_num_put<char>),
51     new boost::math::nonfinite_num_get<char>);
52
53   {
54     stringstream ss;
55     ss.imbue(new_locale);
56     double inf = numeric_limits<double>::infinity();
57     ss << inf; // Write out.
58     double r;
59     ss >> r; // Read back in.
60
61     cout << "infinity output was " << inf << endl;
62     cout << "infinity input was " << r << endl;
63
64     BOOST_ASSERT(inf == r);
65   }
66   {
67     stringstream ss;
68     ss.imbue(new_locale);
69
70     double nan = numeric_limits<double>::quiet_NaN();
71     ss << nan; // Write out.
72     double v;
73     ss >> v; // Read back in.
74
75     cout << "NaN output was " << nan << endl;
76     cout << "NaN input was " << v << endl;
77
78     // BOOST_ASSERT(nan == v); // Always fails because NaN == NaN fails!
79     // BOOST_ASSERT(nan == numeric_limits<double>::quiet_NaN()); asserts!
80   }
81
82 } // int main()
83
84 /*
85
86 Output:
87
88 infinity output was 1.#INF
89 infinity input was 1.#INF
90 NaN output was 1.#QNAN
91 NaN input was 1.#QNAN
92
93 */
94