]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/multiprecision/test/test_float_io.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / multiprecision / test / test_float_io.cpp
CommitLineData
7c673cae
FG
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
92f5a8d4 9#define _SCL_SECURE_NO_WARNINGS
7c673cae
FG
10#endif
11
92f5a8d4
TL
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
7c673cae
FG
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
92f5a8d4 52#pragma warning(disable : 4127)
7c673cae
FG
53#endif
54
55#if defined(TEST_MPF_50)
56template <unsigned N, boost::multiprecision::expression_template_option ET>
57bool has_bad_bankers_rounding(const boost::multiprecision::number<boost::multiprecision::gmp_float<N>, ET>&)
92f5a8d4
TL
58{
59 return true;
60}
7c673cae
FG
61#endif
62#if defined(TEST_FLOAT128) && defined(BOOST_INTEL)
63bool has_bad_bankers_rounding(const boost::multiprecision::float128&)
92f5a8d4
TL
64{
65 return true;
66}
7c673cae
FG
67#endif
68template <class T>
92f5a8d4
TL
69bool has_bad_bankers_rounding(const T&)
70{
71 return false;
72}
7c673cae
FG
73
74bool 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);
92f5a8d4 78 if (l != s.size())
7c673cae
FG
79 return false;
80 std::string::size_type len = s.find('e');
92f5a8d4 81 if (len == std::string::npos)
7c673cae
FG
82 len = l - 1;
83 else
84 --len;
92f5a8d4 85 if (s.compare(0, len, expect, len))
7c673cae 86 return false;
92f5a8d4 87 if (s[len] != expect[len] + 1)
7c673cae
FG
88 return false;
89 return true;
90}
91
92void print_flags(std::ios_base::fmtflags f)
93{
94 std::cout << "Formatting flags were: ";
92f5a8d4 95 if (f & std::ios_base::scientific)
7c673cae 96 std::cout << "scientific ";
92f5a8d4 97 if (f & std::ios_base::fixed)
7c673cae 98 std::cout << "fixed ";
92f5a8d4 99 if (f & std::ios_base::showpoint)
7c673cae 100 std::cout << "showpoint ";
92f5a8d4 101 if (f & std::ios_base::showpos)
7c673cae
FG
102 std::cout << "showpos ";
103 std::cout << std::endl;
104}
105
7c673cae
FG
106template <class T>
107void test()
108{
92f5a8d4
TL
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}};
7c673cae
FG
114
115 boost::array<boost::array<const char*, 13 * 9>, 40> string_data = {{
116#include "libs/multiprecision/test/string_data.ipp"
117 }};
118
92f5a8d4 119 double num = 123456789.0;
7c673cae 120 double denom = 1;
92f5a8d4
TL
121 double val = num;
122 for (unsigned j = 0; j < 40; ++j)
7c673cae
FG
123 {
124 unsigned col = 0;
92f5a8d4 125 for (unsigned prec = 1; prec < 14; ++prec)
7c673cae 126 {
92f5a8d4 127 for (unsigned i = 0; i < f.size(); ++i, ++col)
7c673cae
FG
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];
92f5a8d4 134 if (ss.str() != expect)
7c673cae 135 {
92f5a8d4 136 if (has_bad_bankers_rounding(mp_t()) && is_bankers_rounding_error(ss.str(), expect))
7c673cae
FG
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;
92f5a8d4 154 if (j & 1)
7c673cae
FG
155 denom *= 8;
156 val = num / denom;
157 }
158
92f5a8d4
TL
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"}};
7c673cae
FG
161
162 unsigned col = 0;
92f5a8d4
TL
163 val = 0;
164 for (unsigned prec = 1; prec < 14; ++prec)
7c673cae 165 {
92f5a8d4 166 for (unsigned i = 0; i < f.size(); ++i, ++col)
7c673cae
FG
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];
92f5a8d4 173 if (ss.str() != expect)
7c673cae
FG
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
92f5a8d4 186 if (std::numeric_limits<mp_t>::has_infinity)
7c673cae
FG
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 }
92f5a8d4 202 if (std::numeric_limits<mp_t>::has_quiet_NaN)
7c673cae
FG
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 }
92f5a8d4
TL
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 }
7c673cae
FG
218}
219
220template <class T>
221T generate_random()
222{
223 typedef typename T::backend_type::exponent_type e_type;
92f5a8d4
TL
224 static boost::random::mt19937 gen;
225 T val = gen();
226 T prev_val = -1;
227 while (val != prev_val)
7c673cae
FG
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
240template <class T>
241struct max_digits10_proxy
242{
243 static const unsigned value = std::numeric_limits<T>::digits10 + 5;
244};
245#ifdef TEST_CPP_DEC_FLOAT
246template <unsigned D, boost::multiprecision::expression_template_option ET>
247struct 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
253template <unsigned D, boost::multiprecision::expression_template_option ET>
254struct 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
260template <unsigned D, boost::multiprecision::expression_template_option ET>
261struct 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
267template <class T>
268void do_round_trip(const T& val, std::ios_base::fmtflags f)
269{
270 std::stringstream ss;
7c673cae 271 ss << std::setprecision(std::numeric_limits<T>::max_digits10);
7c673cae
FG
272 ss.flags(f);
273 ss << val;
274 T new_val = static_cast<T>(ss.str());
275 BOOST_CHECK_EQUAL(new_val, val);
1e59de90 276 new_val = static_cast<T>(val.str(f & std::ios_base::fixed ? std::numeric_limits<T>::max_digits10 : 0, f));
7c673cae
FG
277 BOOST_CHECK_EQUAL(new_val, val);
278}
279
280template <class T>
281void do_round_trip(const T& val)
282{
283 do_round_trip(val, std::ios_base::fmtflags(0));
284 do_round_trip(val, std::ios_base::fmtflags(std::ios_base::scientific));
92f5a8d4 285 if ((fabs(val) > 1) && (fabs(val) < 1e100))
7c673cae
FG
286 do_round_trip(val, std::ios_base::fmtflags(std::ios_base::fixed));
287}
288
289template <class T>
290void test_round_trip()
291{
92f5a8d4 292 for (unsigned i = 0; i < 1000; ++i)
7c673cae
FG
293 {
294 T val = generate_random<T>();
295 do_round_trip(val);
296 do_round_trip(T(-val));
92f5a8d4
TL
297 do_round_trip(T(1 / val));
298 do_round_trip(T(-1 / val));
7c673cae
FG
299 }
300}
301
1e59de90
TL
302template<typename T>
303void test_to_string()
304{
305 using std::to_string;
306 T x0{"23.43"};
307 BOOST_CHECK_EQUAL(to_string(x0), "23.430000");
308
309 T x1{"1e-9"};
310 BOOST_CHECK_EQUAL(to_string(x1), "0.000000");
311
312 T x2{"123456789"};
313 BOOST_CHECK_EQUAL(to_string(x2), "123456789.000000");
314}
315
92f5a8d4
TL
316#ifdef TEST_FLOAT128
317void test_hexadecimal_floating_point()
318{
319 using boost::multiprecision::float128;
320
321 float128 x = 0x1p+0Q;
322
323 std::string s = x.str(0, std::ios_base::fmtflags(std::ios_base::fixed | std::ios_base::scientific));
324 BOOST_CHECK_EQUAL(s, "0x1p+0");
325
326 s = x.str(0, std::ios_base::fmtflags(std::ios_base::floatfield));
327 BOOST_CHECK_EQUAL(s, "0x1p+0");
328
329 x = -1;
330 s = x.str(0, std::ios_base::fmtflags(std::ios_base::floatfield));
331 BOOST_CHECK_EQUAL(s, "-0x1p+0");
332
333 // hexadecimal representation of pi; test a round trip:
334 float128 pi1 = 0x1.921fb54442d18469898cc51701b8p+1Q;
335 s = pi1.str(0, std::ios_base::fmtflags(std::ios_base::floatfield));
336 float128 pi2(s);
337 BOOST_CHECK_EQUAL(pi1, pi2);
338}
339#endif
340
7c673cae
FG
341int main()
342{
343#ifdef TEST_MPFR_50
344 test<boost::multiprecision::mpfr_float_50>();
345 test<boost::multiprecision::mpfr_float_100>();
346
347 test_round_trip<boost::multiprecision::mpfr_float_50>();
348 test_round_trip<boost::multiprecision::mpfr_float_100>();
1e59de90
TL
349
350 test_to_string<boost::multiprecision::mpfr_float_50>();
351 test_to_string<boost::multiprecision::mpfr_float_100>();
352
7c673cae
FG
353#endif
354#ifdef TEST_MPFI_50
355 test<boost::multiprecision::mpfr_float_50>();
356 test<boost::multiprecision::mpfr_float_100>();
357
358 test_round_trip<boost::multiprecision::mpfr_float_50>();
359 test_round_trip<boost::multiprecision::mpfr_float_100>();
360#endif
361#ifdef TEST_CPP_DEC_FLOAT
362 test<boost::multiprecision::cpp_dec_float_50>();
363 test<boost::multiprecision::cpp_dec_float_100>();
92f5a8d4 364
7c673cae
FG
365 // cpp_dec_float has extra guard digits that messes this up:
366 test_round_trip<boost::multiprecision::cpp_dec_float_50>();
367 test_round_trip<boost::multiprecision::cpp_dec_float_100>();
1e59de90
TL
368
369 test_to_string<boost::multiprecision::cpp_dec_float_50>();
370 test_to_string<boost::multiprecision::cpp_dec_float_100>();
371
7c673cae
FG
372#endif
373#ifdef TEST_MPF_50
374 test<boost::multiprecision::mpf_float_50>();
375 test<boost::multiprecision::mpf_float_100>();
376 /*
377 // I can't get this to work with mpf_t - mpf_str appears
378 // not to actually print enough decimal digits:
379 test_round_trip<boost::multiprecision::mpf_float_50>();
380 test_round_trip<boost::multiprecision::mpf_float_100>();
381 */
382#endif
383#ifdef TEST_FLOAT128
384 test<boost::multiprecision::float128>();
92f5a8d4 385 test_hexadecimal_floating_point();
1e59de90 386 test_to_string<boost::multiprecision::float128>();
7c673cae
FG
387#ifndef BOOST_INTEL
388 test_round_trip<boost::multiprecision::float128>();
389#endif
390#endif
391 return boost::report_errors();
392}