real.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*==============================================================================
  2. Copyright (c) 2001-2010 Hartmut Kaiser
  3. Copyright (c) 2010 Bryce Lelbach
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #if !defined(BOOST_SPIRIT_TEST_REAL_NUMERICS_HPP)
  8. #define BOOST_SPIRIT_TEST_REAL_NUMERICS_HPP
  9. #include <boost/version.hpp>
  10. #include <boost/config/warning_disable.hpp>
  11. #include <boost/detail/lightweight_test.hpp>
  12. #include <boost/math/concepts/real_concept.hpp>
  13. #include <boost/spirit/include/karma_char.hpp>
  14. #include <boost/spirit/include/karma_numeric.hpp>
  15. #include <boost/spirit/include/karma_generate.hpp>
  16. #include <boost/spirit/include/karma_directive.hpp>
  17. #include <boost/limits.hpp>
  18. #include "test.hpp"
  19. using namespace spirit_test;
  20. ///////////////////////////////////////////////////////////////////////////////
  21. // policy for real_generator, which forces the scientific notation
  22. template <typename T>
  23. struct scientific_policy : boost::spirit::karma::real_policies<T>
  24. {
  25. // we want the numbers always to be in scientific format
  26. typedef boost::spirit::karma::real_policies<T> base_type;
  27. static int floatfield(T) { return base_type::fmtflags::scientific; }
  28. };
  29. ///////////////////////////////////////////////////////////////////////////////
  30. // policy for real_generator, which forces the fixed notation
  31. template <typename T>
  32. struct fixed_policy : boost::spirit::karma::real_policies<T>
  33. {
  34. typedef boost::spirit::karma::real_policies<T> base_type;
  35. // we want the numbers always to be in fixed format
  36. static int floatfield(T) { return base_type::fmtflags::fixed; }
  37. };
  38. ///////////////////////////////////////////////////////////////////////////////
  39. // policy for real_generator, which forces to output trailing zeros in the
  40. // fractional part
  41. template <typename T>
  42. struct trailing_zeros_policy : boost::spirit::karma::real_policies<T> // 4 digits
  43. {
  44. // we want the numbers always to contain trailing zeros up to 4 digits in
  45. // the fractional part
  46. static bool trailing_zeros(T) { return true; }
  47. // we want to generate up to 4 fractional digits
  48. static unsigned int precision(T) { return 4; }
  49. };
  50. ///////////////////////////////////////////////////////////////////////////////
  51. // policy for real_generator, which forces the sign to be generated
  52. template <typename T>
  53. struct signed_policy : boost::spirit::karma::real_policies<T>
  54. {
  55. // we want to always have a sign generated
  56. static bool force_sign(T)
  57. {
  58. return true;
  59. }
  60. };
  61. ///////////////////////////////////////////////////////////////////////////////
  62. // policy for real_generator, which forces to output trailing zeros in the
  63. // fractional part
  64. template <typename T>
  65. struct bordercase_policy : boost::spirit::karma::real_policies<T>
  66. {
  67. // we want to generate up to the maximum significant amount of fractional
  68. // digits
  69. static unsigned int precision(T)
  70. {
  71. return std::numeric_limits<T>::digits10 + 1;
  72. }
  73. };
  74. ///////////////////////////////////////////////////////////////////////////////
  75. // policy for real_generator, which forces to output trailing zeros in the
  76. // fractional part
  77. template <typename T>
  78. struct statefull_policy : boost::spirit::karma::real_policies<T>
  79. {
  80. statefull_policy(int precision = 4, bool trailingzeros = false)
  81. : precision_(precision), trailingzeros_(trailingzeros)
  82. {}
  83. // we want to generate up to the maximum significant amount of fractional
  84. // digits
  85. unsigned int precision(T) const
  86. {
  87. return precision_;
  88. }
  89. bool trailing_zeros(T) const
  90. {
  91. return trailingzeros_;
  92. }
  93. int precision_;
  94. bool trailingzeros_;
  95. };
  96. #endif // BOOST_SPIRIT_TEST_REAL_NUMERICS_HPP