1 ///////////////////////////////////////////////////////////////
2 // Copyright 2013 John Maddock. Distributed under the Boost
3 // Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
6 // Generic routines for converting floating point values to and from decimal strings.
7 // Note that these use "naive" algorithms which result in rounding error - so they
8 // do not round trip to and from the string representation (but should only be out
12 #ifndef BOOST_MP_FLOAT_STRING_CVT_HPP
13 #define BOOST_MP_FLOAT_STRING_CVT_HPP
17 namespace boost { namespace multiprecision { namespace detail {
20 inline void round_string_up_at(std::string& s, int pos, I& expon)
23 // Rounds up a string representation of a number at pos:
27 s.insert(static_cast<std::string::size_type>(0), 1, '1');
28 s.erase(s.size() - 1);
31 else if (s[pos] == '9')
34 round_string_up_at(s, pos - 1, expon);
38 if ((pos == 0) && (s[pos] == '0') && (s.size() == 1))
44 template <class Backend>
45 std::string convert_to_string(Backend b, std::streamsize digits, std::ios_base::fmtflags f)
47 using default_ops::eval_convert_to;
48 using default_ops::eval_divide;
49 using default_ops::eval_floor;
50 using default_ops::eval_fpclassify;
51 using default_ops::eval_log10;
52 using default_ops::eval_multiply;
53 using default_ops::eval_pow;
54 using default_ops::eval_subtract;
56 typedef typename mpl::front<typename Backend::unsigned_types>::type ui_type;
57 typedef typename Backend::exponent_type exponent_type;
62 exponent_type expon = 0;
63 std::streamsize org_digits = digits;
64 BOOST_ASSERT(digits > 0);
66 int fpt = eval_fpclassify(b);
68 if (fpt == (int)FP_ZERO)
73 else if (fpt == (int)FP_INFINITE)
75 if (b.compare(ui_type(0)) < 0)
78 return ((f & std::ios_base::showpos) == std::ios_base::showpos) ? "+inf" : "inf";
80 else if (fpt == (int)FP_NAN)
87 // Start by figuring out the exponent:
89 isneg = b.compare(ui_type(0)) < 0;
98 eval_convert_to(&expon, t);
99 if (-expon > std::numeric_limits<number<Backend> >::max_exponent10 - 3)
103 eval_pow(t2, ten, e);
104 eval_multiply(t, t2, b);
105 eval_multiply(t, t2);
107 eval_multiply(t, ten);
111 eval_pow(t, ten, -expon);
115 // Make sure we're between [1,10) and adjust if not:
117 if (t.compare(ui_type(1)) < 0)
119 eval_multiply(t, ui_type(10));
122 else if (t.compare(ui_type(10)) >= 0)
124 eval_divide(t, ui_type(10));
130 // Adjust the number of digits required based on formatting options:
132 if (((f & std::ios_base::fixed) == std::ios_base::fixed) && (expon != -1))
134 if ((f & std::ios_base::scientific) == std::ios_base::scientific)
137 // Extract the digits one at a time:
139 for (unsigned i = 0; i < digits; ++i)
141 eval_floor(digit, t);
142 eval_convert_to(&cdigit, digit);
143 result += static_cast<char>('0' + cdigit);
144 eval_subtract(t, digit);
145 eval_multiply(t, ten);
148 // Possibly round result:
152 eval_floor(digit, t);
153 eval_convert_to(&cdigit, digit);
154 eval_subtract(t, digit);
155 if ((cdigit == 5) && (t.compare(ui_type(0)) == 0))
158 if ((*result.rbegin() - '0') & 1)
160 round_string_up_at(result, result.size() - 1, expon);
163 else if (cdigit >= 5)
165 round_string_up_at(result, result.size() - 1, expon);
169 while ((result.size() > digits) && result.size())
171 // We may get here as a result of rounding...
172 if (result.size() > 1)
173 result.erase(result.size() - 1);
177 --expon; // so we put less padding in the result.
183 BOOST_ASSERT(org_digits >= 0);
185 result.insert(static_cast<std::string::size_type>(0), 1, '-');
186 format_float_string(result, expon, org_digits, f, iszero);
191 template <class Backend>
192 void convert_from_string(Backend& b, const char* p)
194 using default_ops::eval_add;
195 using default_ops::eval_divide;
196 using default_ops::eval_multiply;
197 using default_ops::eval_pow;
199 typedef typename mpl::front<typename Backend::unsigned_types>::type ui_type;
205 bool is_neg_expon = false;
206 static const ui_type ten = ui_type(10);
207 typename Backend::exponent_type expon = 0;
209 typedef std::numeric_limits<number<Backend, et_off> > limits;
210 static const int max_digits = limits::is_specialized ? limits::max_digits10 + 1 : INT_MAX;
219 if ((std::strcmp(p, "nan") == 0) || (std::strcmp(p, "NaN") == 0) || (std::strcmp(p, "NAN") == 0))
221 eval_divide(b, ui_type(0));
226 if ((std::strcmp(p, "inf") == 0) || (std::strcmp(p, "Inf") == 0) || (std::strcmp(p, "INF") == 0))
229 eval_divide(b, ui_type(0));
235 // Grab all the leading digits before the decimal point:
237 while (std::isdigit(*p))
239 eval_multiply(b, ten);
240 eval_add(b, ui_type(*p - '0'));
247 // Grab everything after the point, stop when we've seen
248 // enough digits, even if there are actually more available:
251 while (std::isdigit(*p))
253 eval_multiply(b, ten);
254 eval_add(b, ui_type(*p - '0'));
257 if (++digits_seen > max_digits)
260 while (std::isdigit(*p))
264 // Parse the exponent:
266 if ((*p == 'e') || (*p == 'E'))
276 typename Backend::exponent_type e2 = 0;
277 while (std::isdigit(*p))
289 // Scale by 10^expon, note that 10^expon can be
290 // outside the range of our number type, even though the
291 // result is within range, if that looks likely, then split
292 // the calculation in two:
295 if (expon > limits::min_exponent10 + 2)
297 eval_pow(t, t, expon);
302 eval_pow(t, t, expon + digits_seen + 1);
305 eval_pow(t, t, -digits_seen - 1);
313 // Unexpected input in string:
314 BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected characters in string being interpreted as a float128."));
318 }}} // namespace boost::multiprecision::detail