lexical_cast_native.cpp 4.6 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. */
  24. #include <boost/math/special_functions/nonfinite_num_facets.hpp>
  25. using boost::math::nonfinite_num_get;
  26. using boost::math::nonfinite_num_put;
  27. #include <boost/lexical_cast.hpp>
  28. using boost::lexical_cast;
  29. #include <iostream>
  30. using std::cout;
  31. using std::endl;
  32. using std::cerr;
  33. #include <iomanip>
  34. using std::setw;
  35. using std::left;
  36. using std::right;
  37. using std::internal;
  38. #include <string>
  39. using std::string;
  40. #include <sstream>
  41. using std::istringstream;
  42. #include <limits>
  43. using std::numeric_limits;
  44. #include <locale>
  45. using std::locale;
  46. #include <boost/assert.hpp>
  47. int main ()
  48. {
  49. std::cout << "lexical_cast example (NOT using finite_num_facet)." << std::endl;
  50. if((std::numeric_limits<double>::has_infinity == false) || (std::numeric_limits<double>::infinity() == 0))
  51. {
  52. std::cout << "Infinity not supported on this platform." << std::endl;
  53. return 0;
  54. }
  55. if((std::numeric_limits<double>::has_quiet_NaN == false) || (std::numeric_limits<double>::quiet_NaN() == 0))
  56. {
  57. std::cout << "NaN not supported on this platform." << std::endl;
  58. return 0;
  59. }
  60. // Some tests that are expected to fail on some platforms.
  61. // (But these tests are expected to pass using non_finite num_put and num_get facets).
  62. // Use the current 'native' default locale.
  63. std::locale default_locale (std::locale::classic ()); // Note the currrent (default C) locale.
  64. // Create plus and minus infinity.
  65. double plus_infinity = +std::numeric_limits<double>::infinity();
  66. double minus_infinity = -std::numeric_limits<double>::infinity();
  67. // and create a NaN (NotANumber).
  68. double NaN = +std::numeric_limits<double>::quiet_NaN ();
  69. // Output the nonfinite values using the current (default C) locale.
  70. // The default representations differ from system to system,
  71. // for example, using Microsoft compilers, 1.#INF, -1.#INF, and 1.#QNAN.
  72. cout << "Using default locale" << endl;
  73. cout << "+std::numeric_limits<double>::infinity() = " << plus_infinity << endl;
  74. cout << "-std::numeric_limits<double>::infinity() = " << minus_infinity << endl;
  75. cout << "+std::numeric_limits<double>::quiet_NaN () = " << NaN << endl;
  76. // Checks below are expected to fail on some platforms!
  77. // Now try some 'round-tripping', 'reading' "inf"
  78. double x = boost::lexical_cast<double>("inf");
  79. // and check we get a floating-point infinity.
  80. BOOST_ASSERT(x == std::numeric_limits<double>::infinity());
  81. // Check we can convert the other way from floating-point infinity,
  82. string s = boost::lexical_cast<string>(numeric_limits<double>::infinity());
  83. // to a C99 string representation as "inf".
  84. BOOST_ASSERT(s == "inf");
  85. // Finally try full 'round-tripping' (in both directions):
  86. BOOST_ASSERT(lexical_cast<double>(lexical_cast<string>(numeric_limits<double>::infinity()))
  87. == numeric_limits<double>::infinity());
  88. BOOST_ASSERT(lexical_cast<string>(lexical_cast<double>("inf")) == "inf");
  89. return 0;
  90. } // int main()
  91. /*
  92. Output:
  93. from MSVC 10, fails (as expected)
  94. lexical_cast_native.vcxproj -> J:\Cpp\fp_facet\fp_facet\Debug\lexical_cast_native.exe
  95. lexical_cast example (NOT using finite_num_facet).
  96. Using default locale
  97. +std::numeric_limits<double>::infinity() = 1.#INF
  98. -std::numeric_limits<double>::infinity() = -1.#INF
  99. +std::numeric_limits<double>::quiet_NaN () = 1.#QNAN
  100. C:\Program Files\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppCommon.targets(183,5): error MSB3073: The command ""J:\Cpp\fp_facet\fp_facet\Debug\lexical_cast_native.exe"
  101. C:\Program Files\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppCommon.targets(183,5): error MSB3073: :VCEnd" exited with code 3.
  102. */