Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / multiprecision / detail / float_string_cvt.hpp
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
5 //
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
9 // in the last bit).
10 //
11
12 #ifndef BOOST_MP_FLOAT_STRING_CVT_HPP
13 #define BOOST_MP_FLOAT_STRING_CVT_HPP
14
15 #include <cctype>
16
17 namespace boost { namespace multiprecision { namespace detail {
18
19 template <class I>
20 inline void round_string_up_at(std::string& s, int pos, I& expon)
21 {
22    //
23    // Rounds up a string representation of a number at pos:
24    //
25    if (pos < 0)
26    {
27       s.insert(static_cast<std::string::size_type>(0), 1, '1');
28       s.erase(s.size() - 1);
29       ++expon;
30    }
31    else if (s[pos] == '9')
32    {
33       s[pos] = '0';
34       round_string_up_at(s, pos - 1, expon);
35    }
36    else
37    {
38       if ((pos == 0) && (s[pos] == '0') && (s.size() == 1))
39          ++expon;
40       ++s[pos];
41    }
42 }
43
44 template <class Backend>
45 std::string convert_to_string(Backend b, std::streamsize digits, std::ios_base::fmtflags f)
46 {
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;
55
56    typedef typename mpl::front<typename Backend::unsigned_types>::type ui_type;
57    typedef typename Backend::exponent_type                             exponent_type;
58
59    std::string     result;
60    bool            iszero     = false;
61    bool            isneg      = false;
62    exponent_type   expon      = 0;
63    std::streamsize org_digits = digits;
64    BOOST_ASSERT(digits > 0);
65
66    int fpt = eval_fpclassify(b);
67
68    if (fpt == (int)FP_ZERO)
69    {
70       result = "0";
71       iszero = true;
72    }
73    else if (fpt == (int)FP_INFINITE)
74    {
75       if (b.compare(ui_type(0)) < 0)
76          return "-inf";
77       else
78          return ((f & std::ios_base::showpos) == std::ios_base::showpos) ? "+inf" : "inf";
79    }
80    else if (fpt == (int)FP_NAN)
81    {
82       return "nan";
83    }
84    else
85    {
86       //
87       // Start by figuring out the exponent:
88       //
89       isneg = b.compare(ui_type(0)) < 0;
90       if (isneg)
91          b.negate();
92       Backend t;
93       Backend ten;
94       ten = ui_type(10);
95
96       eval_log10(t, b);
97       eval_floor(t, t);
98       eval_convert_to(&expon, t);
99       if (-expon > std::numeric_limits<number<Backend> >::max_exponent10 - 3)
100       {
101          int     e = -expon / 2;
102          Backend t2;
103          eval_pow(t2, ten, e);
104          eval_multiply(t, t2, b);
105          eval_multiply(t, t2);
106          if (expon & 1)
107             eval_multiply(t, ten);
108       }
109       else
110       {
111          eval_pow(t, ten, -expon);
112          eval_multiply(t, b);
113       }
114       //
115       // Make sure we're between [1,10) and adjust if not:
116       //
117       if (t.compare(ui_type(1)) < 0)
118       {
119          eval_multiply(t, ui_type(10));
120          --expon;
121       }
122       else if (t.compare(ui_type(10)) >= 0)
123       {
124          eval_divide(t, ui_type(10));
125          ++expon;
126       }
127       Backend digit;
128       ui_type cdigit;
129       //
130       // Adjust the number of digits required based on formatting options:
131       //
132       if (((f & std::ios_base::fixed) == std::ios_base::fixed) && (expon != -1))
133          digits += expon + 1;
134       if ((f & std::ios_base::scientific) == std::ios_base::scientific)
135          ++digits;
136       //
137       // Extract the digits one at a time:
138       //
139       for (unsigned i = 0; i < digits; ++i)
140       {
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);
146       }
147       //
148       // Possibly round result:
149       //
150       if (digits >= 0)
151       {
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))
156          {
157             // Bankers rounding:
158             if ((*result.rbegin() - '0') & 1)
159             {
160                round_string_up_at(result, result.size() - 1, expon);
161             }
162          }
163          else if (cdigit >= 5)
164          {
165             round_string_up_at(result, result.size() - 1, expon);
166          }
167       }
168    }
169    while ((result.size() > digits) && result.size())
170    {
171       // We may get here as a result of rounding...
172       if (result.size() > 1)
173          result.erase(result.size() - 1);
174       else
175       {
176          if (expon > 0)
177             --expon; // so we put less padding in the result.
178          else
179             ++expon;
180          ++digits;
181       }
182    }
183    BOOST_ASSERT(org_digits >= 0);
184    if (isneg)
185       result.insert(static_cast<std::string::size_type>(0), 1, '-');
186    format_float_string(result, expon, org_digits, f, iszero);
187
188    return result;
189 }
190
191 template <class Backend>
192 void convert_from_string(Backend& b, const char* p)
193 {
194    using default_ops::eval_add;
195    using default_ops::eval_divide;
196    using default_ops::eval_multiply;
197    using default_ops::eval_pow;
198
199    typedef typename mpl::front<typename Backend::unsigned_types>::type ui_type;
200    b = ui_type(0);
201    if (!p || (*p == 0))
202       return;
203
204    bool                                                  is_neg       = false;
205    bool                                                  is_neg_expon = false;
206    static const ui_type                                  ten          = ui_type(10);
207    typename Backend::exponent_type                       expon        = 0;
208    int                                                   digits_seen  = 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;
211
212    if (*p == '+')
213       ++p;
214    else if (*p == '-')
215    {
216       is_neg = true;
217       ++p;
218    }
219    if ((std::strcmp(p, "nan") == 0) || (std::strcmp(p, "NaN") == 0) || (std::strcmp(p, "NAN") == 0))
220    {
221       eval_divide(b, ui_type(0));
222       if (is_neg)
223          b.negate();
224       return;
225    }
226    if ((std::strcmp(p, "inf") == 0) || (std::strcmp(p, "Inf") == 0) || (std::strcmp(p, "INF") == 0))
227    {
228       b = ui_type(1);
229       eval_divide(b, ui_type(0));
230       if (is_neg)
231          b.negate();
232       return;
233    }
234    //
235    // Grab all the leading digits before the decimal point:
236    //
237    while (std::isdigit(*p))
238    {
239       eval_multiply(b, ten);
240       eval_add(b, ui_type(*p - '0'));
241       ++p;
242       ++digits_seen;
243    }
244    if (*p == '.')
245    {
246       //
247       // Grab everything after the point, stop when we've seen
248       // enough digits, even if there are actually more available:
249       //
250       ++p;
251       while (std::isdigit(*p))
252       {
253          eval_multiply(b, ten);
254          eval_add(b, ui_type(*p - '0'));
255          ++p;
256          --expon;
257          if (++digits_seen > max_digits)
258             break;
259       }
260       while (std::isdigit(*p))
261          ++p;
262    }
263    //
264    // Parse the exponent:
265    //
266    if ((*p == 'e') || (*p == 'E'))
267    {
268       ++p;
269       if (*p == '+')
270          ++p;
271       else if (*p == '-')
272       {
273          is_neg_expon = true;
274          ++p;
275       }
276       typename Backend::exponent_type e2 = 0;
277       while (std::isdigit(*p))
278       {
279          e2 *= 10;
280          e2 += (*p - '0');
281          ++p;
282       }
283       if (is_neg_expon)
284          e2 = -e2;
285       expon += e2;
286    }
287    if (expon)
288    {
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:
293       Backend t;
294       t = ten;
295       if (expon > limits::min_exponent10 + 2)
296       {
297          eval_pow(t, t, expon);
298          eval_multiply(b, t);
299       }
300       else
301       {
302          eval_pow(t, t, expon + digits_seen + 1);
303          eval_multiply(b, t);
304          t = ten;
305          eval_pow(t, t, -digits_seen - 1);
306          eval_multiply(b, t);
307       }
308    }
309    if (is_neg)
310       b.negate();
311    if (*p)
312    {
313       // Unexpected input in string:
314       BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected characters in string being interpreted as a float128."));
315    }
316 }
317
318 }}} // namespace boost::multiprecision::detail
319
320 #endif