real.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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_X3_REAL_HPP)
  9. #define BOOST_SPIRIT_TEST_X3_REAL_HPP
  10. #include <climits>
  11. #include <boost/math/concepts/real_concept.hpp>
  12. #include <boost/detail/lightweight_test.hpp>
  13. #include <boost/spirit/home/x3/char.hpp>
  14. #include <boost/spirit/home/x3/numeric.hpp>
  15. #include <boost/spirit/home/x3/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::x3::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)
  31. {
  32. namespace x3 = boost::spirit::x3;
  33. return boost::spirit::x3::extract_uint<T, 10, 1, 2, true>::call(first, last, attr);
  34. }
  35. // No exponent
  36. template <typename Iterator>
  37. static bool
  38. parse_exp(Iterator&, Iterator const&)
  39. {
  40. return false;
  41. }
  42. // No exponent
  43. template <typename Iterator, typename Attribute>
  44. static bool
  45. parse_exp_n(Iterator&, Iterator const&, Attribute&)
  46. {
  47. return false;
  48. }
  49. // Thousands separated numbers
  50. template <typename Iterator, typename Accumulator>
  51. static bool
  52. parse_n(Iterator& first, Iterator const& last, Accumulator& result)
  53. {
  54. using boost::spirit::x3::uint_parser;
  55. namespace x3 = boost::spirit::x3;
  56. uint_parser<unsigned, 10, 1, 3> uint3;
  57. uint_parser<unsigned, 10, 3, 3> uint3_3;
  58. if (parse(first, last, uint3, result))
  59. {
  60. Accumulator n;
  61. Iterator iter = first;
  62. while (x3::parse(iter, last, ',') && x3::parse(iter, last, uint3_3, n))
  63. {
  64. result = result * 1000 + n;
  65. first = iter;
  66. }
  67. return true;
  68. }
  69. return false;
  70. }
  71. };
  72. template <typename T>
  73. struct no_trailing_dot_policy : boost::spirit::x3::real_policies<T>
  74. {
  75. static bool const allow_trailing_dot = false;
  76. };
  77. template <typename T>
  78. struct no_leading_dot_policy : boost::spirit::x3::real_policies<T>
  79. {
  80. static bool const allow_leading_dot = false;
  81. };
  82. template <typename T, typename T2>
  83. bool
  84. compare(T n, T2 expected)
  85. {
  86. T const eps = std::pow(10.0, -std::numeric_limits<T>::digits10);
  87. T delta = n - expected;
  88. return (delta >= -eps) && (delta <= eps);
  89. }
  90. ///////////////////////////////////////////////////////////////////////////////
  91. // A custom real type
  92. struct custom_real
  93. {
  94. double n;
  95. custom_real() : n(0) {}
  96. custom_real(double n_) : n(n_) {}
  97. friend bool operator==(custom_real a, custom_real b)
  98. { return a.n == b.n; }
  99. friend custom_real operator*(custom_real a, custom_real b)
  100. { return custom_real(a.n * b.n); }
  101. friend custom_real operator+(custom_real a, custom_real b)
  102. { return custom_real(a.n + b.n); }
  103. friend custom_real operator-(custom_real a, custom_real b)
  104. { return custom_real(a.n - b.n); }
  105. };
  106. #endif