nonfinite_legacy.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // nonfinite_legacy.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 Basic tests of nonfinite loopback with output and input facet.
  10. \detail Basic loopback test outputs using the so-called 'legacy' facets,
  11. "1.#INF" and "1.#QNAN".
  12. and reads back in using nonfinite input 'legacy' facet, and
  13. (if possible) checks if loopback OK.
  14. */
  15. #include <boost/math/special_functions/nonfinite_num_facets.hpp>
  16. using boost::math::nonfinite_num_put;
  17. using boost::math::nonfinite_num_get;
  18. using boost::math::legacy;
  19. #include <iostream>
  20. using std::cout;
  21. using std::endl;
  22. #include <iomanip>
  23. using std::setfill;
  24. using std::setw;
  25. #include <locale>
  26. using std::locale;
  27. #include <sstream>
  28. using std::stringstream;
  29. #include <limits>
  30. using std::numeric_limits;
  31. #include <assert.h>
  32. int main()
  33. {
  34. // Create a new locale with both the nonfinite facets.
  35. std::locale new_locale(std::locale(std::locale(),
  36. new boost::math::nonfinite_num_put<char>),
  37. new boost::math::nonfinite_num_get<char>);
  38. {
  39. stringstream ss;
  40. ss.imbue(new_locale);
  41. double inf = numeric_limits<double>::infinity();
  42. ss << inf; // Write out.
  43. double r;
  44. ss >> r; // Read back in.
  45. cout << "infinity output was " << inf << endl;
  46. cout << "infinity input was " << r << endl;
  47. BOOST_ASSERT(inf == r);
  48. }
  49. {
  50. stringstream ss;
  51. ss.imbue(new_locale);
  52. double nan = numeric_limits<double>::quiet_NaN();
  53. ss << nan; // Write out.
  54. double v;
  55. ss >> v; // Read back in.
  56. cout << "NaN output was " << nan << endl;
  57. cout << "NaN input was " << v << endl;
  58. // BOOST_ASSERT(nan == v); // Always fails because NaN == NaN fails!
  59. // BOOST_ASSERT(nan == numeric_limits<double>::quiet_NaN()); asserts!
  60. }
  61. } // int main()
  62. /*
  63. Output:
  64. infinity output was 1.#INF
  65. infinity input was 1.#INF
  66. NaN output was 1.#QNAN
  67. NaN input was 1.#QNAN
  68. */