nonfinite_num_facet_trap.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /** nonfinite_num_facet_trap.cpp
  2. *
  3. * Copyright (c) 2012 Paul A. Bristow
  4. *
  5. * Distributed under the Boost Software License, Version 1.0.
  6. * (See accompanying file LICENSE_1_0.txt
  7. * or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. *
  9. * This very simple program illustrates how to use the
  10. * `boost/math/nonfinite_num_facets.hpp` trapping output of infinity and/or NaNs.
  11. *
  12. \file
  13. \brief A very simple example of using non_finite_num facet for
  14. trapping output of infinity and/or NaNs.
  15. \note To actually get an exception throw by the iostream library
  16. one must enable exceptions.
  17. `oss.exceptions(std::ios_base::failbit | std::ios_base::badbit);`
  18. \note Which bit is set is implementation dependent, so enable exceptions for both.
  19. This is a fairly brutal method of catching nonfinites on output,
  20. but may suit some applications.
  21. */
  22. #ifdef _MSC_VER
  23. # pragma warning(disable : 4127) // conditional expression is constant.
  24. // assumes C++ exceptions enabled /EHsc
  25. #endif
  26. #include <boost/cstdint.hpp>
  27. #include <boost/math/special_functions/nonfinite_num_facets.hpp>
  28. #include <iostream>
  29. #include <iomanip>
  30. using std::cout;
  31. using std::endl;
  32. using std::hex;
  33. #include <exception>
  34. #include <limits> // numeric_limits
  35. using std::numeric_limits;
  36. int main()
  37. {
  38. using namespace boost::math;
  39. std::cout << "nonfinite_num_facet_trap.cpp" << std::endl;
  40. const double inf = +std::numeric_limits<double>::infinity ();
  41. const double nan = +std::numeric_limits<double>::quiet_NaN ();
  42. { // Output infinity and NaN with default flags (no trapping).
  43. std::ostringstream oss;
  44. std::locale default_locale (std::locale::classic ());
  45. std::locale C99_out_locale (default_locale, new boost::math::nonfinite_num_put<char>);
  46. oss.imbue (C99_out_locale);
  47. oss.exceptions(std::ios_base::failbit | std::ios_base::badbit);
  48. oss << inf << ' ' << nan;
  49. cout << "oss.rdstate() = " << hex << oss.rdstate() << endl; // 0
  50. cout << "os.str() = " << oss.str() << endl; // os.str() = inf nan
  51. }
  52. try
  53. { // // Output infinity with flags set to trap and catch any infinity.
  54. std::ostringstream oss;
  55. std::locale default_locale (std::locale::classic ());
  56. std::locale C99_out_locale (default_locale, new boost::math::nonfinite_num_put<char>(trap_infinity));
  57. oss.imbue (C99_out_locale);
  58. oss.exceptions(std::ios_base::failbit | std::ios_base::badbit);
  59. // Note that which bit is set is implementation dependent, so enable exceptions for both.
  60. oss << inf;
  61. cout << "oss.rdstate() = " << hex << oss.rdstate() << endl;
  62. cout << "oss.str() = " << oss.str() << endl;
  63. }
  64. catch(const std::ios_base::failure& e)
  65. { // Expect "Infinity".
  66. std::cout << "\n""Message from thrown exception was: " << e.what() << std::endl;
  67. }
  68. try
  69. { // // Output NaN with flags set to catch any NaNs.
  70. std::ostringstream oss;
  71. std::locale default_locale (std::locale::classic ());
  72. std::locale C99_out_locale (default_locale, new boost::math::nonfinite_num_put<char>(trap_nan));
  73. oss.imbue (C99_out_locale);
  74. oss.exceptions(std::ios_base::failbit | std::ios_base::badbit);
  75. // Note that which bit is set is implementation dependent, so enable exceptions for both.
  76. oss << nan;
  77. cout << "oss.str() = " << oss.str() << endl;
  78. }
  79. catch(const std::ios_base::failure& e)
  80. { // Expect "Infinity".
  81. std::cout << "\n""Message from thrown exception was: " << e.what() << std::endl;
  82. }
  83. return 0; // end of nonfinite_num_facet_trap.cpp
  84. } // int main()
  85. /*
  86. Output:
  87. nonfinite_num_facet_trap.cpp
  88. oss.rdstate() = 0
  89. os.str() = inf nan
  90. Message from thrown exception was: Infinity
  91. Message from thrown exception was: NaN
  92. */