nonfinite_facet_sstream.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // nonfinite_facet_sstream.cpp
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt
  4. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. // Copyright (c) 2006 Johan Rade
  6. // Copyright (c) 2011 Paul A. Bristow
  7. /*!
  8. \file
  9. \brief Examples of nonfinite with output and input facets and stringstreams.
  10. \detail Contruct a new locale with the nonfinite_num_put and nonfinite_num_get
  11. facets and imbue istringstream, ostringstream and stringstreams,
  12. showing output and input (and loopback for the stringstream).
  13. */
  14. #include <boost/math/special_functions/nonfinite_num_facets.hpp>
  15. using boost::math::nonfinite_num_put;
  16. using boost::math::nonfinite_num_get;
  17. using boost::math::legacy;
  18. #include <iostream>
  19. using std::cout;
  20. using std::endl;
  21. #include <locale>
  22. using std::locale;
  23. #include <sstream>
  24. using std::stringstream;
  25. using std::istringstream;
  26. using std::ostringstream;
  27. #include <limits>
  28. using std::numeric_limits;
  29. #include <assert.h>
  30. int main()
  31. {
  32. //[nonfinite_facets_sstream_1
  33. locale old_locale;
  34. locale tmp_locale(old_locale, new nonfinite_num_put<char>);
  35. locale new_locale(tmp_locale, new nonfinite_num_get<char>);
  36. //] [/nonfinite_facets_sstream_1]
  37. // Note that to add two facets, nonfinite_num_put and nonfinite_num_get,
  38. // you have to add one at a time, using a temporary locale.
  39. {
  40. ostringstream oss;
  41. oss.imbue(new_locale);
  42. double inf = numeric_limits<double>::infinity();
  43. oss << inf; // Write out.
  44. cout << "infinity output was " << oss.str() << endl;
  45. BOOST_ASSERT(oss.str() == "inf");
  46. }
  47. {
  48. istringstream iss;
  49. iss.str("inf");
  50. iss.imbue(new_locale);
  51. double inf;
  52. iss >> inf; // Read from "inf"
  53. cout << "Infinity input was " << iss.str() << endl;
  54. BOOST_ASSERT(inf == numeric_limits<double>::infinity());
  55. }
  56. {
  57. //[nonfinite_facets_sstream_2
  58. stringstream ss;
  59. ss.imbue(new_locale);
  60. double inf = numeric_limits<double>::infinity();
  61. ss << inf; // Write out.
  62. BOOST_ASSERT(ss.str() == "inf");
  63. double r;
  64. ss >> r; // Read back in.
  65. BOOST_ASSERT(inf == r); // Confirms that the double values really are identical.
  66. cout << "infinity output was " << ss.str() << endl;
  67. cout << "infinity input was " << r << endl;
  68. // But the string representation of r displayed will be the native type
  69. // because, when it was constructed, cout had NOT been imbued
  70. // with the new locale containing the nonfinite_numput facet.
  71. // So the cout output will be "1.#INF on MS platforms
  72. // and may be "inf" or other string representation on other platforms.
  73. //] [/nonfinite_facets_sstream_2]
  74. }
  75. {
  76. stringstream ss;
  77. ss.imbue(new_locale);
  78. double nan = numeric_limits<double>::quiet_NaN();
  79. ss << nan; // Write out.
  80. BOOST_ASSERT(ss.str() == "nan");
  81. double v;
  82. ss >> v; // Read back in.
  83. cout << "NaN output was " << ss.str() << endl;
  84. cout << "NaN input was " << v << endl;
  85. // assert(nan == v); // Always fails because NaN == NaN fails!
  86. // assert(nan == numeric_limits<double>::quiet_NaN()); asserts!
  87. // And the string representation will be the native type
  88. // because cout has NOT been imbued with a locale containing
  89. // the nonfinite_numput facet.
  90. // So the output will be "1.#QNAN on MS platforms
  91. // and may be "nan" or other string representation on other platforms.
  92. }
  93. } // int main()
  94. /*
  95. //[nonfinite_facet_sstream_output
  96. infinity output was inf
  97. Infinity input was inf
  98. infinity output was inf
  99. infinity input was 1.#INF
  100. NaN output was nan
  101. NaN input was 1.#QNAN
  102. //] [nonfinite_facet_sstream_output]
  103. */