lexical_cast_nonfinite_facets.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /** lexical_cast_nonfinite_facets.cpp
  2. *
  3. * Copyright (c) 2011 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' with lexical cast
  11. * to obtain C99 representation of infinity and NaN.
  12. * This example is from the original Floating Point Utilities contribution by Johan Rade.
  13. * Floating Point Utility library has been accepted into Boost,
  14. * but the utilities are incorporated into Boost.Math library.
  15. *
  16. \file
  17. \brief A very simple example of using lexical cast with
  18. non_finite_num facet for C99 standard output of infinity and NaN.
  19. \detail This example shows how to create a C99 non-finite locale,
  20. and imbue input and output streams with the non_finite_num put and get facets.
  21. This allows lexical_cast output and input of infinity and NaN in a Standard portable way,
  22. This permits 'loop-back' of output back into input (and portably across different system too).
  23. See also lexical_cast_native.cpp which is expected to fail on many systems,
  24. but might succeed if the default locale num_put and num_get facets
  25. comply with C99 nonfinite input and output specification.
  26. */
  27. #include <boost/math/special_functions/nonfinite_num_facets.hpp>
  28. using boost::math::nonfinite_num_get;
  29. using boost::math::nonfinite_num_put;
  30. #include <boost/lexical_cast.hpp>
  31. using boost::lexical_cast;
  32. #include <iostream>
  33. using std::cout;
  34. using std::endl;
  35. using std::cerr;
  36. #include <iomanip>
  37. using std::setw;
  38. using std::left;
  39. using std::right;
  40. using std::internal;
  41. #include <string>
  42. using std::string;
  43. #include <sstream>
  44. using std::istringstream;
  45. #include <limits>
  46. using std::numeric_limits;
  47. #include <locale>
  48. using std::locale;
  49. #include <boost/assert.hpp>
  50. int main ()
  51. {
  52. std::cout << "finite_num_facet with lexical_cast example." << std::endl;
  53. // Example of using non_finite num_put and num_get facets with lexical_cast.
  54. //locale old_locale;
  55. //locale tmp_locale(old_locale, new nonfinite_num_put<char>);
  56. //// Create a new temporary output locale, and add the output nonfinite_num_put facet.
  57. //locale new_locale(tmp_locale, new nonfinite_num_get<char>);
  58. // Create a new output locale (from the tmp locale), and add the input nonfinite_num_get facet.
  59. // Note that you can only add facets one at a time,
  60. // unless you chain thus:
  61. std::locale new_locale(std::locale(std::locale(),
  62. new boost::math::nonfinite_num_put<char>),
  63. new boost::math::nonfinite_num_get<char>);
  64. locale::global(new_locale); // Newly constructed streams
  65. // (including those streams inside lexical_cast)
  66. // now use new_locale with nonfinite facets.
  67. // Output using the new locale.
  68. cout << "Using C99_out_locale " << endl;
  69. cout.imbue(new_locale);
  70. // Necessary because cout already constructed using default C locale,
  71. // and default facets for nonfinites.
  72. // Create plus and minus infinity.
  73. double plus_infinity = +std::numeric_limits<double>::infinity();
  74. double minus_infinity = -std::numeric_limits<double>::infinity();
  75. // and create a NaN (NotANumber)
  76. double NaN = +std::numeric_limits<double>::quiet_NaN ();
  77. cout << "+std::numeric_limits<double>::infinity() = " << plus_infinity << endl;
  78. cout << "-std::numeric_limits<double>::infinity() = " << minus_infinity << endl;
  79. cout << "+std::numeric_limits<double>::quiet_NaN () = " << NaN << endl;
  80. // Now try some 'round-tripping', 'reading' "inf".
  81. double x = boost::lexical_cast<double>("inf");
  82. // and check we get a floating-point infinity.
  83. BOOST_ASSERT(x == std::numeric_limits<double>::infinity());
  84. cout << "boost::lexical_cast<double>(\"inf\") = " << x << endl;
  85. // Check we can convert the other way from floating-point infinity,
  86. string s = boost::lexical_cast<string>(numeric_limits<double>::infinity());
  87. // to a C99 string representation as "inf".
  88. BOOST_ASSERT(s == "inf");
  89. // Finally try full 'round-tripping' (in both directions):
  90. BOOST_ASSERT(lexical_cast<double>(lexical_cast<string>(numeric_limits<double>::infinity()))
  91. == numeric_limits<double>::infinity());
  92. BOOST_ASSERT(lexical_cast<string>(lexical_cast<double>("inf")) == "inf");
  93. return 0;
  94. } // int main()
  95. /*
  96. Output:
  97. finite_num_facet with lexical_cast example.
  98. Using C99_out_locale
  99. +std::numeric_limits<double>::infinity() = inf
  100. -std::numeric_limits<double>::infinity() = -inf
  101. +std::numeric_limits<double>::quiet_NaN () = nan
  102. boost::lexical_cast<double>("inf") = inf
  103. */