nonfinite_num_facet.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /** nonfinite_num_facet.cpp
  2. *
  3. * Copyright (c) 2011 Francois Mauger
  4. * Copyright (c) 2011 Paul A. Bristow
  5. *
  6. * Distributed under the Boost Software License, Version 1.0.
  7. * (See accompanying file LICENSE_1_0.txt
  8. * or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. *
  10. * This simple program illustrates how to use the
  11. * `boost/math/nonfinite_num_facets.hpp' material from the original
  12. * Floating Point Utilities contribution by Johan Rade.
  13. * Floating Point Utility library has been accepted into Boost,
  14. * but the utilities have been/will be incorporated into Boost.Math library.
  15. *
  16. \file
  17. \brief A fairly simple example of using non_finite_num facet for
  18. C99 standard output of infinity and NaN.
  19. \detail This program illustrates how to use the
  20. `boost/math/nonfinite_num_facets.hpp' material from the original
  21. Floating Point Utilities contribution by Johan Rade.
  22. Floating Point Utility library has been accepted into Boost,
  23. but the utilities have been/will be incorporated into Boost.Math library.
  24. Based on an example from Francois Mauger.
  25. Double and float variables are assigned ordinary finite values (pi),
  26. and nonfinite like infinity and NaN.
  27. These values are then output and read back in, and then redisplayed.
  28. */
  29. #ifdef _MSC_VER
  30. # pragma warning(disable : 4127) // conditional expression is constant.
  31. #endif
  32. #include <iostream>
  33. #include <iomanip>
  34. using std::cout;
  35. using std::endl;
  36. #include <limits> // numeric_limits
  37. using std::numeric_limits;
  38. #include <boost/cstdint.hpp>
  39. #include <boost/math/special_functions/nonfinite_num_facets.hpp>
  40. static const char sep = ','; // Separator of bracketed float and double values.
  41. // Use max_digits10 (or equivalent) to obtain
  42. // all potentially significant decimal digits for the floating-point types.
  43. #ifdef BOOST_NO_CXX11_NUMERIC_LIMITS
  44. std::streamsize max_digits10_float = 2 + std::numeric_limits<float>::digits * 30103UL / 100000UL;
  45. std::streamsize max_digits10_double = 2 + std::numeric_limits<double>::digits * 30103UL / 100000UL;
  46. #else
  47. // Can use new C++0X max_digits10 (the maximum potentially significant digits).
  48. std::streamsize max_digits10_float = std::numeric_limits<float>::max_digits10;
  49. std::streamsize max_digits10_double = std::numeric_limits<double>::max_digits10;
  50. #endif
  51. /* A class with a float and a double */
  52. struct foo
  53. {
  54. foo () : fvalue (3.1415927F), dvalue (3.1415926535897931)
  55. {
  56. }
  57. // Set both the values to -infinity :
  58. void minus_infinity ()
  59. {
  60. fvalue = -std::numeric_limits<float>::infinity ();
  61. dvalue = -std::numeric_limits<double>::infinity ();
  62. return;
  63. }
  64. // Set the values to +infinity :
  65. void plus_infinity ()
  66. {
  67. fvalue = +std::numeric_limits<float>::infinity ();
  68. dvalue = +std::numeric_limits<double>::infinity ();
  69. return;
  70. }
  71. // Set the values to NaN :
  72. void nan ()
  73. {
  74. fvalue = +std::numeric_limits<float>::quiet_NaN ();
  75. dvalue = +std::numeric_limits<double>::quiet_NaN ();
  76. return;
  77. }
  78. // Print a foo:
  79. void print (std::ostream & a_out, const std::string & a_title)
  80. {
  81. if (a_title.empty ()) a_out << "foo";
  82. else a_out << a_title;
  83. a_out << " : " << std::endl;
  84. a_out << "|-- " << "fvalue = ";
  85. a_out.precision (max_digits10_float);
  86. a_out << fvalue << std::endl;
  87. a_out << "`-- " << "dvalue = ";
  88. a_out.precision (max_digits10_double);
  89. a_out << dvalue << std::endl;
  90. return;
  91. }
  92. // I/O operators for a foo structure of a float and a double :
  93. friend std::ostream & operator<< (std::ostream & a_out, const foo & a_foo);
  94. friend std::istream & operator>> (std::istream & a_in, foo & a_foo);
  95. // Attributes :
  96. float fvalue; // Single precision floating number.
  97. double dvalue; // Double precision floating number.
  98. };
  99. std::ostream & operator<< (std::ostream & a_out, const foo & a_foo)
  100. { // Output bracketed FPs, for example "(3.1415927,3.1415926535897931)"
  101. a_out.precision (max_digits10_float);
  102. a_out << "(" << a_foo.fvalue << sep ;
  103. a_out.precision (max_digits10_double);
  104. a_out << a_foo.dvalue << ")";
  105. return a_out;
  106. }
  107. std::istream & operator>> (std::istream & a_in, foo & a_foo)
  108. { // Input bracketed floating-point values into a foo structure,
  109. // for example from "(3.1415927,3.1415926535897931)"
  110. char c = 0;
  111. a_in.get (c);
  112. if (c != '(')
  113. {
  114. std::cerr << "ERROR: operator>> No ( " << std::endl;
  115. a_in.setstate(std::ios::failbit);
  116. return a_in;
  117. }
  118. float f;
  119. a_in >> std::ws >> f;
  120. if (! a_in)
  121. {
  122. return a_in;
  123. }
  124. a_in >> std::ws;
  125. a_in.get (c);
  126. if (c != sep)
  127. {
  128. std::cerr << "ERROR: operator>> c='" << c << "'" << std::endl;
  129. std::cerr << "ERROR: operator>> No '" << sep << "'" << std::endl;
  130. a_in.setstate(std::ios::failbit);
  131. return a_in;
  132. }
  133. double d;
  134. a_in >> std::ws >> d;
  135. if (! a_in)
  136. {
  137. return a_in;
  138. }
  139. a_in >> std::ws;
  140. a_in.get (c);
  141. if (c != ')')
  142. {
  143. std::cerr << "ERROR: operator>> No ) " << std::endl;
  144. a_in.setstate(std::ios::failbit);
  145. return a_in;
  146. }
  147. a_foo.fvalue = f;
  148. a_foo.dvalue = d;
  149. return a_in;
  150. } // std::istream & operator>> (std::istream & a_in, foo & a_foo)
  151. int main ()
  152. {
  153. std::cout << "nonfinite_num_facet simple example." << std::endl;
  154. if((std::numeric_limits<double>::has_infinity == false) || (std::numeric_limits<double>::infinity() == 0))
  155. {
  156. std::cout << "Infinity not supported on this platform." << std::endl;
  157. return 0;
  158. }
  159. if((std::numeric_limits<double>::has_quiet_NaN == false) || (std::numeric_limits<double>::quiet_NaN() == 0))
  160. {
  161. std::cout << "NaN not supported on this platform." << std::endl;
  162. return 0;
  163. }
  164. #ifdef BOOST_NO_CXX11_NUMERIC_LIMITS
  165. cout << "BOOST_NO_CXX11_NUMERIC_LIMITS is defined, so no max_digits10 available either:"
  166. "\n we'll have to calculate our own version." << endl;
  167. #endif
  168. std::cout << "std::numeric_limits<float>::max_digits10 is " << max_digits10_float << endl;
  169. std::cout << "std::numeric_limits<double>::max_digits10 is " << max_digits10_double << endl;
  170. std::locale the_default_locale (std::locale::classic ());
  171. {
  172. std::cout << "Write to a string buffer (using default locale) :" << std::endl;
  173. foo f0; // pi
  174. foo f1; f1.minus_infinity ();
  175. foo f2; f2.plus_infinity ();
  176. foo f3; f3.nan ();
  177. f0.print (std::cout, "f0"); // pi
  178. f1.print (std::cout, "f1"); // +inf
  179. f2.print (std::cout, "f2"); // -inf
  180. f3.print (std::cout, "f3"); // NaN
  181. std::ostringstream oss;
  182. std::locale C99_out_locale (the_default_locale, new boost::math::nonfinite_num_put<char>);
  183. oss.imbue (C99_out_locale);
  184. oss.precision (15);
  185. oss << f0 << f1 << f2 << f3;
  186. std::cout << "Output in C99 format is: \"" << oss.str () << "\"" << std::endl;
  187. std::cout << "Output done." << std::endl;
  188. }
  189. {
  190. std::string the_string = "(3.1415927,3.1415926535897931)(-inf,-inf)(inf,inf)(nan,nan)"; // C99 format
  191. // Must have correct separator!
  192. std::cout << "Read C99 format from a string buffer containing \"" << the_string << "\""<< std::endl;
  193. std::locale C99_in_locale (the_default_locale, new boost::math::nonfinite_num_get<char>);
  194. std::istringstream iss (the_string);
  195. iss.imbue (C99_in_locale);
  196. foo f0, f1, f2, f3;
  197. iss >> f0 >> f1 >> f2 >> f3;
  198. if (! iss)
  199. {
  200. std::cerr << "Input Format error !" << std::endl;
  201. }
  202. else
  203. {
  204. std::cerr << "Input OK." << std::endl;
  205. cout << "Display in default locale format " << endl;
  206. f0.print (std::cout, "f0");
  207. f1.print (std::cout, "f1");
  208. f2.print (std::cout, "f2");
  209. f3.print (std::cout, "f3");
  210. }
  211. std::cout << "Input done." << std::endl;
  212. }
  213. std::cout << "End nonfinite_num_facet.cpp" << std::endl;
  214. return 0;
  215. } // int main()
  216. // end of test_nonfinite_num_facets.cpp
  217. /*
  218. Output:
  219. nonfinite_num_facet simple example.
  220. std::numeric_limits<float>::max_digits10 is 8
  221. std::numeric_limits<double>::max_digits10 is 17
  222. Write to a string buffer (using default locale) :
  223. f0 :
  224. |-- fvalue = 3.1415927
  225. `-- dvalue = 3.1415926535897931
  226. f1 :
  227. |-- fvalue = -1.#INF
  228. `-- dvalue = -1.#INF
  229. f2 :
  230. |-- fvalue = 1.#INF
  231. `-- dvalue = 1.#INF
  232. f3 :
  233. |-- fvalue = 1.#QNAN
  234. `-- dvalue = 1.#QNAN
  235. Output in C99 format is: "(3.1415927,3.1415926535897931)(-inf,-inf)(inf,inf)(nan,nan)"
  236. Output done.
  237. Read C99 format from a string buffer containing "(3.1415927,3.1415926535897931)(-inf,-inf)(inf,inf)(nan,nan)"
  238. Display in default locale format
  239. f0 :
  240. |-- fvalue = 3.1415927
  241. `-- dvalue = 3.1415926535897931
  242. f1 :
  243. |-- fvalue = -1.#INF
  244. `-- dvalue = -1.#INF
  245. f2 :
  246. |-- fvalue = 1.#INF
  247. `-- dvalue = 1.#INF
  248. f3 :
  249. |-- fvalue = 1.#QNAN
  250. `-- dvalue = 1.#QNAN
  251. Input done.
  252. End nonfinite_num_facet.cpp
  253. */