real.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*=============================================================================
  2. Copyright (c) 2001-2010 Joel de Guzman
  3. Copyright (c) 2001-2010 Hartmut Kaiser
  4. Use, modification and distribution is subject to the Boost Software
  5. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. #if !defined(BOOST_SPIRIT_TEST_QI_REAL_HPP)
  9. #define BOOST_SPIRIT_TEST_QI_REAL_HPP
  10. #include <climits>
  11. #include <boost/math/concepts/real_concept.hpp>
  12. #include <boost/detail/lightweight_test.hpp>
  13. #include <boost/spirit/include/qi_char.hpp>
  14. #include <boost/spirit/include/qi_numeric.hpp>
  15. #include <boost/spirit/include/qi_operator.hpp>
  16. #include <boost/math/special_functions/fpclassify.hpp>
  17. #include <boost/math/special_functions/sign.hpp>
  18. #include "test.hpp"
  19. ///////////////////////////////////////////////////////////////////////////////
  20. // These policies can be used to parse thousand separated
  21. // numbers with at most 2 decimal digits after the decimal
  22. // point. e.g. 123,456,789.01
  23. ///////////////////////////////////////////////////////////////////////////////
  24. template <typename T>
  25. struct ts_real_policies : boost::spirit::qi::ureal_policies<T>
  26. {
  27. // 2 decimal places Max
  28. template <typename Iterator, typename Attribute>
  29. static bool
  30. parse_frac_n(Iterator& first, Iterator const& last, Attribute& attr, int& frac_digits)
  31. {
  32. namespace qi = boost::spirit::qi;
  33. Iterator savef = first;
  34. bool r = qi::extract_uint<Attribute, 10, 1, 2, true>::call(first, last, attr);
  35. frac_digits = static_cast<int>(std::distance(savef, first));
  36. return r;
  37. }
  38. // No exponent
  39. template <typename Iterator>
  40. static bool
  41. parse_exp(Iterator&, Iterator const&)
  42. {
  43. return false;
  44. }
  45. // No exponent
  46. template <typename Iterator, typename Attribute>
  47. static bool
  48. parse_exp_n(Iterator&, Iterator const&, Attribute&)
  49. {
  50. return false;
  51. }
  52. // Thousands separated numbers
  53. template <typename Iterator, typename Accumulator>
  54. static bool
  55. parse_n(Iterator& first, Iterator const& last, Accumulator& result)
  56. {
  57. using boost::spirit::qi::uint_parser;
  58. namespace qi = boost::spirit::qi;
  59. uint_parser<unsigned, 10, 1, 3> uint3;
  60. uint_parser<unsigned, 10, 3, 3> uint3_3;
  61. if (parse(first, last, uint3, result))
  62. {
  63. Accumulator n;
  64. Iterator iter = first;
  65. while (qi::parse(iter, last, ',') && qi::parse(iter, last, uint3_3, n))
  66. {
  67. result = result * 1000 + n;
  68. first = iter;
  69. }
  70. return true;
  71. }
  72. return false;
  73. }
  74. };
  75. template <typename T>
  76. struct no_trailing_dot_policy : boost::spirit::qi::real_policies<T>
  77. {
  78. static bool const allow_trailing_dot = false;
  79. };
  80. template <typename T>
  81. struct no_leading_dot_policy : boost::spirit::qi::real_policies<T>
  82. {
  83. static bool const allow_leading_dot = false;
  84. };
  85. template <typename T>
  86. bool
  87. compare(T n, double expected
  88. , T const eps = std::pow(10.0, -std::numeric_limits<T>::digits10))
  89. {
  90. T delta = n - expected;
  91. return (delta >= -eps) && (delta <= eps);
  92. }
  93. ///////////////////////////////////////////////////////////////////////////////
  94. // A custom real type
  95. struct custom_real
  96. {
  97. double n;
  98. custom_real() : n(0) {}
  99. custom_real(double n_) : n(n_) {}
  100. friend bool operator==(custom_real a, custom_real b)
  101. { return a.n == b.n; }
  102. friend custom_real operator*(custom_real a, custom_real b)
  103. { return custom_real(a.n * b.n); }
  104. friend custom_real operator+(custom_real a, custom_real b)
  105. { return custom_real(a.n + b.n); }
  106. friend custom_real operator-(custom_real a, custom_real b)
  107. { return custom_real(a.n - b.n); }
  108. };
  109. #endif