Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / multiprecision / test / test_float_io.cpp
1 // Copyright John Maddock 2011.
2
3 // Use, modification and distribution are subject to the
4 // Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt
6 // or copy at http://www.boost.org/LICENSE_1_0.txt)
7
8 #ifdef _MSC_VER
9 #define _SCL_SECURE_NO_WARNINGS
10 #endif
11
12 #if !defined(TEST_MPF_50) && !defined(TEST_CPP_DEC_FLOAT) && !defined(TEST_MPFR_50) && !defined(TEST_MPFI_50) && !defined(TEST_FLOAT128)
13 #define TEST_MPF_50
14 #define TEST_CPP_DEC_FLOAT
15 #define TEST_MPFR_50
16 #define TEST_MPFI_50
17 #define TEST_FLOAT128
18
19 #ifdef _MSC_VER
20 #pragma message("CAUTION!!: No backend type specified so testing everything.... this will take some time!!")
21 #endif
22 #ifdef __GNUC__
23 #pragma warning "CAUTION!!: No backend type specified so testing everything.... this will take some time!!"
24 #endif
25
26 #endif
27
28 #if defined(TEST_MPF_50)
29 #include <boost/multiprecision/gmp.hpp>
30 #endif
31 #if defined(TEST_MPFR_50)
32 #include <boost/multiprecision/mpfr.hpp>
33 #endif
34 #if defined(TEST_MPFI_50)
35 #include <boost/multiprecision/mpfi.hpp>
36 #endif
37 #ifdef TEST_CPP_DEC_FLOAT
38 #include <boost/multiprecision/cpp_dec_float.hpp>
39 #endif
40 #ifdef TEST_FLOAT128
41 #include <boost/multiprecision/float128.hpp>
42 #endif
43
44 #include <boost/random/mersenne_twister.hpp>
45 #include <boost/random/uniform_int.hpp>
46 #include "test.hpp"
47 #include <boost/array.hpp>
48 #include <iostream>
49 #include <iomanip>
50
51 #ifdef BOOST_MSVC
52 #pragma warning(disable : 4127)
53 #endif
54
55 #if defined(TEST_MPF_50)
56 template <unsigned N, boost::multiprecision::expression_template_option ET>
57 bool has_bad_bankers_rounding(const boost::multiprecision::number<boost::multiprecision::gmp_float<N>, ET>&)
58 {
59    return true;
60 }
61 #endif
62 #if defined(TEST_FLOAT128) && defined(BOOST_INTEL)
63 bool has_bad_bankers_rounding(const boost::multiprecision::float128&)
64 {
65    return true;
66 }
67 #endif
68 template <class T>
69 bool has_bad_bankers_rounding(const T&)
70 {
71    return false;
72 }
73
74 bool is_bankers_rounding_error(const std::string& s, const char* expect)
75 {
76    // This check isn't foolproof: that would require *much* more sophisticated code!!!
77    std::string::size_type l = std::strlen(expect);
78    if (l != s.size())
79       return false;
80    std::string::size_type len = s.find('e');
81    if (len == std::string::npos)
82       len = l - 1;
83    else
84       --len;
85    if (s.compare(0, len, expect, len))
86       return false;
87    if (s[len] != expect[len] + 1)
88       return false;
89    return true;
90 }
91
92 void print_flags(std::ios_base::fmtflags f)
93 {
94    std::cout << "Formatting flags were: ";
95    if (f & std::ios_base::scientific)
96       std::cout << "scientific ";
97    if (f & std::ios_base::fixed)
98       std::cout << "fixed ";
99    if (f & std::ios_base::showpoint)
100       std::cout << "showpoint ";
101    if (f & std::ios_base::showpos)
102       std::cout << "showpos ";
103    std::cout << std::endl;
104 }
105
106 template <class T>
107 void test()
108 {
109    typedef T                                mp_t;
110    boost::array<std::ios_base::fmtflags, 9> f =
111        {{std::ios_base::fmtflags(0), std::ios_base::showpoint, std::ios_base::showpos, std::ios_base::scientific, std::ios_base::scientific | std::ios_base::showpos,
112          std::ios_base::scientific | std::ios_base::showpoint, std::ios_base::fixed, std::ios_base::fixed | std::ios_base::showpoint,
113          std::ios_base::fixed | std::ios_base::showpos}};
114
115    boost::array<boost::array<const char*, 13 * 9>, 40> string_data = {{
116 #include "libs/multiprecision/test/string_data.ipp"
117    }};
118
119    double num   = 123456789.0;
120    double denom = 1;
121    double val   = num;
122    for (unsigned j = 0; j < 40; ++j)
123    {
124       unsigned col = 0;
125       for (unsigned prec = 1; prec < 14; ++prec)
126       {
127          for (unsigned i = 0; i < f.size(); ++i, ++col)
128          {
129             std::stringstream ss;
130             ss.precision(prec);
131             ss.flags(f[i]);
132             ss << mp_t(val);
133             const char* expect = string_data[j][col];
134             if (ss.str() != expect)
135             {
136                if (has_bad_bankers_rounding(mp_t()) && is_bankers_rounding_error(ss.str(), expect))
137                {
138                   std::cout << "Ignoring bankers-rounding error with GMP mp_f.\n";
139                }
140                else
141                {
142                   std::cout << std::setprecision(20) << "Testing value " << val << std::endl;
143                   print_flags(f[i]);
144                   std::cout << "Precision is: " << prec << std::endl;
145                   std::cout << "Got:      " << ss.str() << std::endl;
146                   std::cout << "Expected: " << expect << std::endl;
147                   ++boost::detail::test_errors();
148                   mp_t(val).str(prec, f[i]); // for debugging
149                }
150             }
151          }
152       }
153       num = -num;
154       if (j & 1)
155          denom *= 8;
156       val = num / denom;
157    }
158
159    boost::array<const char*, 13 * 9> zeros =
160        {{"0", "0.", "+0", "0.0e+00", "+0.0e+00", "0.0e+00", "0.0", "0.0", "+0.0", "0", "0.0", "+0", "0.00e+00", "+0.00e+00", "0.00e+00", "0.00", "0.00", "+0.00", "0", "0.00", "+0", "0.000e+00", "+0.000e+00", "0.000e+00", "0.000", "0.000", "+0.000", "0", "0.000", "+0", "0.0000e+00", "+0.0000e+00", "0.0000e+00", "0.0000", "0.0000", "+0.0000", "0", "0.0000", "+0", "0.00000e+00", "+0.00000e+00", "0.00000e+00", "0.00000", "0.00000", "+0.00000", "0", "0.00000", "+0", "0.000000e+00", "+0.000000e+00", "0.000000e+00", "0.000000", "0.000000", "+0.000000", "0", "0.000000", "+0", "0.0000000e+00", "+0.0000000e+00", "0.0000000e+00", "0.0000000", "0.0000000", "+0.0000000", "0", "0.0000000", "+0", "0.00000000e+00", "+0.00000000e+00", "0.00000000e+00", "0.00000000", "0.00000000", "+0.00000000", "0", "0.00000000", "+0", "0.000000000e+00", "+0.000000000e+00", "0.000000000e+00", "0.000000000", "0.000000000", "+0.000000000", "0", "0.000000000", "+0", "0.0000000000e+00", "+0.0000000000e+00", "0.0000000000e+00", "0.0000000000", "0.0000000000", "+0.0000000000", "0", "0.0000000000", "+0", "0.00000000000e+00", "+0.00000000000e+00", "0.00000000000e+00", "0.00000000000", "0.00000000000", "+0.00000000000", "0", "0.00000000000", "+0", "0.000000000000e+00", "+0.000000000000e+00", "0.000000000000e+00", "0.000000000000", "0.000000000000", "+0.000000000000", "0", "0.000000000000", "+0", "0.0000000000000e+00", "+0.0000000000000e+00", "0.0000000000000e+00", "0.0000000000000", "0.0000000000000", "+0.0000000000000"}};
161
162    unsigned col = 0;
163    val          = 0;
164    for (unsigned prec = 1; prec < 14; ++prec)
165    {
166       for (unsigned i = 0; i < f.size(); ++i, ++col)
167       {
168          std::stringstream ss;
169          ss.precision(prec);
170          ss.flags(f[i]);
171          ss << mp_t(val);
172          const char* expect = zeros[col];
173          if (ss.str() != expect)
174          {
175             std::cout << std::setprecision(20) << "Testing value " << val << std::endl;
176             print_flags(f[i]);
177             std::cout << "Precision is: " << prec << std::endl;
178             std::cout << "Got:      " << ss.str() << std::endl;
179             std::cout << "Expected: " << expect << std::endl;
180             ++boost::detail::test_errors();
181             mp_t(val).str(prec, f[i]); // for debugging
182          }
183       }
184    }
185
186    if (std::numeric_limits<mp_t>::has_infinity)
187    {
188       T val = std::numeric_limits<T>::infinity();
189       BOOST_CHECK_EQUAL(val.str(), "inf");
190       BOOST_CHECK_EQUAL(val.str(0, std::ios_base::showpos), "+inf");
191       val = -val;
192       BOOST_CHECK_EQUAL(val.str(), "-inf");
193       BOOST_CHECK_EQUAL(val.str(0, std::ios_base::showpos), "-inf");
194
195       val = static_cast<T>("inf");
196       BOOST_CHECK_EQUAL(val, std::numeric_limits<T>::infinity());
197       val = static_cast<T>("+inf");
198       BOOST_CHECK_EQUAL(val, std::numeric_limits<T>::infinity());
199       val = static_cast<T>("-inf");
200       BOOST_CHECK_EQUAL(val, -std::numeric_limits<T>::infinity());
201    }
202    if (std::numeric_limits<mp_t>::has_quiet_NaN)
203    {
204       T val = std::numeric_limits<T>::quiet_NaN();
205       BOOST_CHECK_EQUAL(val.str(), "nan");
206       val = static_cast<T>("nan");
207       BOOST_CHECK((boost::math::isnan)(val));
208    }
209    //
210    // Bug cases:
211    //
212    // https://github.com/boostorg/multiprecision/issues/113
213    //
214    {
215       T val("99.9809");
216       BOOST_CHECK_EQUAL(val.str(3, std::ios_base::fixed), "99.981");
217    }
218 }
219
220 template <class T>
221 T generate_random()
222 {
223    typedef typename T::backend_type::exponent_type e_type;
224    static boost::random::mt19937                   gen;
225    T                                               val      = gen();
226    T                                               prev_val = -1;
227    while (val != prev_val)
228    {
229       val *= (gen.max)();
230       prev_val = val;
231       val += gen();
232    }
233    e_type e;
234    val = frexp(val, &e);
235
236    static boost::random::uniform_int_distribution<e_type> ui(0, std::numeric_limits<T>::max_exponent - 10);
237    return ldexp(val, ui(gen));
238 }
239
240 template <class T>
241 struct max_digits10_proxy
242 {
243    static const unsigned value = std::numeric_limits<T>::digits10 + 5;
244 };
245 #ifdef TEST_CPP_DEC_FLOAT
246 template <unsigned D, boost::multiprecision::expression_template_option ET>
247 struct max_digits10_proxy<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<D>, ET> >
248 {
249    static const unsigned value = std::numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<D>, ET> >::max_digits10;
250 };
251 #endif
252 #ifdef TEST_MPF_50
253 template <unsigned D, boost::multiprecision::expression_template_option ET>
254 struct max_digits10_proxy<boost::multiprecision::number<boost::multiprecision::gmp_float<D>, ET> >
255 {
256    static const unsigned value = std::numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<D>, ET> >::max_digits10;
257 };
258 #endif
259 #ifdef TEST_MPFR_50
260 template <unsigned D, boost::multiprecision::expression_template_option ET>
261 struct max_digits10_proxy<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<D>, ET> >
262 {
263    static const unsigned value = std::numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<D>, ET> >::max_digits10;
264 };
265 #endif
266
267 template <class T>
268 void do_round_trip(const T& val, std::ios_base::fmtflags f)
269 {
270    std::stringstream ss;
271 #ifndef BOOST_NO_CXX11_NUMERIC_LIMITS
272    ss << std::setprecision(std::numeric_limits<T>::max_digits10);
273 #else
274    ss << std::setprecision(max_digits10_proxy<T>::value);
275 #endif
276    ss.flags(f);
277    ss << val;
278    T new_val = static_cast<T>(ss.str());
279    BOOST_CHECK_EQUAL(new_val, val);
280    new_val = static_cast<T>(val.str(0, f));
281    BOOST_CHECK_EQUAL(new_val, val);
282 }
283
284 template <class T>
285 void do_round_trip(const T& val)
286 {
287    do_round_trip(val, std::ios_base::fmtflags(0));
288    do_round_trip(val, std::ios_base::fmtflags(std::ios_base::scientific));
289    if ((fabs(val) > 1) && (fabs(val) < 1e100))
290       do_round_trip(val, std::ios_base::fmtflags(std::ios_base::fixed));
291 }
292
293 template <class T>
294 void test_round_trip()
295 {
296    for (unsigned i = 0; i < 1000; ++i)
297    {
298       T val = generate_random<T>();
299       do_round_trip(val);
300       do_round_trip(T(-val));
301       do_round_trip(T(1 / val));
302       do_round_trip(T(-1 / val));
303    }
304 }
305
306 #ifdef TEST_FLOAT128
307 void test_hexadecimal_floating_point()
308 {
309    using boost::multiprecision::float128;
310
311    float128 x = 0x1p+0Q;
312
313    std::string s = x.str(0, std::ios_base::fmtflags(std::ios_base::fixed | std::ios_base::scientific));
314    BOOST_CHECK_EQUAL(s, "0x1p+0");
315
316    s = x.str(0, std::ios_base::fmtflags(std::ios_base::floatfield));
317    BOOST_CHECK_EQUAL(s, "0x1p+0");
318
319    x = -1;
320    s = x.str(0, std::ios_base::fmtflags(std::ios_base::floatfield));
321    BOOST_CHECK_EQUAL(s, "-0x1p+0");
322
323    // hexadecimal representation of pi; test a round trip:
324    float128 pi1 = 0x1.921fb54442d18469898cc51701b8p+1Q;
325    s            = pi1.str(0, std::ios_base::fmtflags(std::ios_base::floatfield));
326    float128 pi2(s);
327    BOOST_CHECK_EQUAL(pi1, pi2);
328 }
329 #endif
330
331 int main()
332 {
333 #ifdef TEST_MPFR_50
334    test<boost::multiprecision::mpfr_float_50>();
335    test<boost::multiprecision::mpfr_float_100>();
336
337    test_round_trip<boost::multiprecision::mpfr_float_50>();
338    test_round_trip<boost::multiprecision::mpfr_float_100>();
339 #endif
340 #ifdef TEST_MPFI_50
341    test<boost::multiprecision::mpfr_float_50>();
342    test<boost::multiprecision::mpfr_float_100>();
343
344    test_round_trip<boost::multiprecision::mpfr_float_50>();
345    test_round_trip<boost::multiprecision::mpfr_float_100>();
346 #endif
347 #ifdef TEST_CPP_DEC_FLOAT
348    test<boost::multiprecision::cpp_dec_float_50>();
349    test<boost::multiprecision::cpp_dec_float_100>();
350
351    // cpp_dec_float has extra guard digits that messes this up:
352    test_round_trip<boost::multiprecision::cpp_dec_float_50>();
353    test_round_trip<boost::multiprecision::cpp_dec_float_100>();
354 #endif
355 #ifdef TEST_MPF_50
356    test<boost::multiprecision::mpf_float_50>();
357    test<boost::multiprecision::mpf_float_100>();
358    /*
359    // I can't get this to work with mpf_t - mpf_str appears
360    // not to actually print enough decimal digits:
361    test_round_trip<boost::multiprecision::mpf_float_50>();
362    test_round_trip<boost::multiprecision::mpf_float_100>();
363    */
364 #endif
365 #ifdef TEST_FLOAT128
366    test<boost::multiprecision::float128>();
367    test_hexadecimal_floating_point();
368 #ifndef BOOST_INTEL
369    test_round_trip<boost::multiprecision::float128>();
370 #endif
371 #endif
372    return boost::report_errors();
373 }