1 /*=============================================================================
2 Copyright (c) 2001-2019 Joel de Guzman
3 Copyright (c) 2001-2011 Hartmut Kaiser
4 http://spirit.sourceforge.net/
6 Distributed under the Boost Software License, Version 1.0. (See accompanying
7 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 =============================================================================*/
9 #ifndef BOOST_SPIRIT_QI_NUMERIC_DETAIL_REAL_IMPL_HPP
10 #define BOOST_SPIRIT_QI_NUMERIC_DETAIL_REAL_IMPL_HPP
17 #include <boost/limits.hpp>
18 #include <boost/type_traits/is_same.hpp>
19 #include <boost/spirit/home/support/unused.hpp>
20 #include <boost/spirit/home/qi/detail/attributes.hpp>
21 #include <boost/spirit/home/support/detail/pow10.hpp>
22 #include <boost/integer.hpp>
23 #include <boost/assert.hpp>
25 #include <boost/core/cmath.hpp>
27 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
28 # pragma warning(push)
29 # pragma warning(disable: 4100) // 'p': unreferenced formal parameter
30 # pragma warning(disable: 4127) // conditional expression is constant
33 namespace boost { namespace spirit { namespace traits
35 using spirit::traits::pow10;
39 template <typename T, typename AccT>
40 void compensate_roundoff(T& n, AccT acc_n, mpl::true_)
42 // at the lowest extremes, we compensate for floating point
43 // roundoff errors by doing imprecise computation using T
45 n = T((acc_n / comp) * comp);
49 template <typename T, typename AccT>
50 void compensate_roundoff(T& n, AccT acc_n, mpl::false_)
52 // no need to compensate
56 template <typename T, typename AccT>
57 void compensate_roundoff(T& n, AccT acc_n)
59 compensate_roundoff(n, acc_n, is_integral<AccT>());
63 template <typename T, typename AccT>
65 scale(int exp, T& n, AccT acc_n)
69 int const max_exp = std::numeric_limits<T>::max_exponent10;
71 // return false if exp exceeds the max_exp
72 // do this check only for primitive types!
73 if (is_floating_point<T>() && (exp > max_exp))
75 n = acc_n * pow10<T>(exp);
79 if (exp < std::numeric_limits<T>::min_exponent10)
81 int const min_exp = std::numeric_limits<T>::min_exponent10;
82 detail::compensate_roundoff(n, acc_n);
83 n /= pow10<T>(-min_exp);
85 // return false if exp still exceeds the min_exp
86 // do this check only for primitive types!
88 if (is_floating_point<T>() && exp < min_exp)
95 n = T(acc_n) / pow10<T>(-exp);
102 scale(int /*exp*/, unused_type /*n*/, unused_type /*acc_n*/)
104 // no-op for unused_type
108 template <typename T, typename AccT>
110 scale(int exp, int frac, T& n, AccT acc_n)
112 return scale(exp - frac, n, acc_n);
116 scale(int /*exp*/, int /*frac*/, unused_type /*n*/)
118 // no-op for unused_type
123 negate(bool neg, float n)
125 return neg ? (core::copysign)(n, -1.f) : n;
129 negate(bool neg, double n)
131 return neg ? (core::copysign)(n, -1.) : n;
135 negate(bool neg, long double n)
137 return neg ? (core::copysign)(n, static_cast<long double>(-1)) : n;
140 template <typename T>
142 negate(bool neg, T const& n)
148 negate(bool /*neg*/, unused_type n)
150 // no-op for unused_type
154 template <typename T>
155 struct real_accumulator : mpl::identity<T> {};
158 struct real_accumulator<float>
159 : mpl::identity<uint_t<(sizeof(float)*CHAR_BIT)>::least> {};
162 struct real_accumulator<double>
163 : mpl::identity<uint_t<(sizeof(double)*CHAR_BIT)>::least> {};
166 namespace boost { namespace spirit { namespace qi { namespace detail
168 BOOST_MPL_HAS_XXX_TRAIT_DEF(version)
170 template <typename T, typename RealPolicies>
173 template <typename Iterator>
175 ignore_excess_digits(Iterator& /* first */, Iterator const& /* last */, mpl::false_)
180 template <typename Iterator>
182 ignore_excess_digits(Iterator& first, Iterator const& last, mpl::true_)
184 return RealPolicies::ignore_excess_digits(first, last);
187 template <typename Iterator>
189 ignore_excess_digits(Iterator& first, Iterator const& last)
191 typedef mpl::bool_<has_version<RealPolicies>::value> has_version;
192 return ignore_excess_digits(first, last, has_version());
195 template <typename Iterator, typename Attribute>
197 parse(Iterator& first, Iterator const& last, Attribute& attr,
198 RealPolicies const& p)
202 Iterator save = first;
204 // Start by parsing the sign. neg will be true if
205 // we got a "-" sign, false otherwise.
206 bool neg = p.parse_sign(first, last);
208 // Now attempt to parse an integer
211 typename traits::real_accumulator<T>::type acc_n = 0;
212 bool got_a_number = p.parse_n(first, last, acc_n);
215 // If we did not get a number it might be a NaN, Inf or a leading
219 // Check whether the number to parse is a NaN or Inf
220 if (p.parse_nan(first, last, n) ||
221 p.parse_inf(first, last, n))
223 // If we got a negative sign, negate the number
224 traits::assign_to(traits::negate(neg, n), attr);
225 return true; // got a NaN or Inf, return early
228 // If we did not get a number and our policies do not
229 // allow a leading dot, fail and return early (no-match)
230 if (!p.allow_leading_dot)
238 // We got a number and we still see digits. This happens if acc_n (an integer)
239 // exceeds the integer's capacity. Collect the excess digits.
240 excess_n = static_cast<int>(ignore_excess_digits(first, last));
247 // Try to parse the dot ('.' decimal point)
248 if (p.parse_dot(first, last))
250 // We got the decimal point. Now we will try to parse
251 // the fraction if it is there. If not, it defaults
252 // to zero (0) only if we already got a number.
255 // We skip the fractions if we already exceeded our digits capacity
256 ignore_excess_digits(first, last);
258 else if (p.parse_frac_n(first, last, acc_n, frac_digits))
260 BOOST_ASSERT(frac_digits >= 0);
262 else if (!got_a_number || !p.allow_trailing_dot)
264 // We did not get a fraction. If we still haven't got a
265 // number and our policies do not allow a trailing dot,
271 // Now, let's see if we can parse the exponent prefix
273 e_hit = p.parse_exp(first, last);
277 // No dot and no number! Return no-match.
284 // If we must expect a dot and we didn't see an exponent
285 // prefix, return no-match.
287 e_hit = p.parse_exp(first, last);
288 if (p.expect_dot && !e_hit)
297 // We got the exponent prefix. Now we will try to parse the
300 if (p.parse_exp_n(first, last, exp))
302 // Got the exponent value. Scale the number by
303 // exp + excess_n - frac_digits.
304 if (!traits::scale(exp + excess_n, frac_digits, n, acc_n))
309 // If there is no number, disregard the exponent altogether.
310 // by resetting 'first' prior to the exponent prefix (e|E)
312 // Scale the number by -frac_digits.
313 bool r = traits::scale(-frac_digits, n, acc_n);
317 else if (frac_digits)
319 // No exponent found. Scale the number by -frac_digits.
320 bool r = traits::scale(-frac_digits, n, acc_n);
327 if (!traits::scale(excess_n, n, acc_n))
332 n = static_cast<T>(acc_n);
336 // If we got a negative sign, negate the number
337 traits::assign_to(traits::negate(neg, n), attr);
344 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
345 # pragma warning(pop)