1 // Distributed under the Boost Software License, Version 1.0.
2 // (See accompanying file LICENSE_1_0.txt
3 // or copy at http://www.boost.org/LICENSE_1_0.txt)
4 
5 // Copyright (c) 2006 Johan Rade
6 // Copyright (c) 2011 Paul A. Bristow
7 
8 /*!
9 \file
10 \brief Basic tests of native nonfinite loopback.
11 
12 \detail Basic loopback test outputs using the platforms built-in facets
13 and reads back in, and checks if loopback OK.
14 
15 Using MSVC this doesn't work OK:
16 input produces just "1" instead of "1.#QNAN", 1.#SNAN" or 1.#IND"!
17 
18 */
19 
20 #include <iostream>
21 using std::cout;
22 using std::endl;
23 #include <locale>
24 using std::locale;
25 #include <string>
26 using std::string;
27 #include <sstream>
28   using std::stringstream;
29 #include <limits>
30 using std::numeric_limits;
31 
main()32 int main()
33 {
34    locale default_locale; // Current global locale.
35   // Try to use the default locale first.
36   // On MSVC this doesn't work.
37 
38   { // Try infinity.
39     stringstream ss; // Both input and output.
40     ss.imbue(default_locale); // Redundant, of course.
41     string infs;
42     if(numeric_limits<double>::has_infinity)
43     {  // Make sure infinity is specialised for type double.
44       double inf = numeric_limits<double>::infinity();
45       ss << inf; // Output infinity.
46       infs = ss.str();  //
47     }
48     else
49     { // Need to provide a suitable string for infinity.
50      infs =  "1.#INF"; // Might suit MSVC?
51       ss << infs;
52     }
53     double r;
54     ss >> r; // Read back in.
55 
56     cout << "infinity output was " << infs << endl; // "1.#INF"
57     cout << "infinity input was " << r << endl; // "1"
58   }
59 
60     { // Try Quiet NaN
61     stringstream ss; // Both input and output.
62     ss.imbue(default_locale); // Redundant, of course.
63     string infs;
64     if(numeric_limits<double>::has_quiet_NaN)
65     {  // Make sure quiet NaN is specialised for type double.
66       double qnan = numeric_limits<double>::quiet_NaN();
67       ss << qnan; // Output quiet_NaN.
68       infs = ss.str();  //
69     }
70     else
71     { // Need to provide a suitable string for quiet_NAN.
72      infs =  "1.#QNAN";
73       ss << infs;
74     }
75     double r;
76     ss >> r; // Read back in.
77 
78     cout << "quiet_NaN output was " << infs << endl; // "1.#QNAN"
79     cout << "quiet_NaN input was " << r << endl; // "1#"
80   }
81 
82 
83 } // int main()
84 
85 /*
86 
87 Output (MSVC Version 10.0):
88 
89 
90   infinity output was 1.#INF
91   infinity input was 1
92   quiet_NaN output was 1.#QNAN
93   quiet_NaN input was 1
94 
95 
96 */
97 
98