File: test_float_io.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (468 lines) | stat: -rw-r--r-- 16,787 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
///////////////////////////////////////////////////////////////
//  Copyright 2011 - 2025 John Maddock.
//  Copyright Christopher Kormanyos 2002 - 2025.
//  Distributed under the Boost
//  Software License, Version 1.0. (See accompanying file
//  LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
//

#ifdef _MSC_VER
#define _SCL_SECURE_NO_WARNINGS
#endif

#include <test.hpp>

#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int.hpp>

#if !defined(TEST_MPF_50) && !defined(TEST_CPP_DEC_FLOAT) && !defined(TEST_CPP_BIN_FLOAT) && !defined(TEST_MPFR_50) && !defined(TEST_MPFI_50) && !defined(TEST_FLOAT128) && !defined(TEST_CPP_DOUBLE_FLOAT)
#define TEST_MPF_50
#define TEST_CPP_DEC_FLOAT
#define TEST_CPP_BIN_FLOAT
#define TEST_MPFR_50
#define TEST_MPFI_50
#define TEST_FLOAT128
#define TEST_CPP_DOUBLE_FLOAT

#ifdef _MSC_VER
#pragma message("CAUTION!!: No backend type specified so testing everything.... this will take some time!!")
#endif
#ifdef __GNUC__
#pragma warning "CAUTION!!: No backend type specified so testing everything.... this will take some time!!"
#endif

#endif

#include <test_traits.hpp> // Note: include this AFTER the test-backends are defined

#if defined(TEST_MPF_50)
#include <boost/multiprecision/gmp.hpp>
#endif
#if defined(TEST_MPFR_50)
#include <boost/multiprecision/mpfr.hpp>
#endif
#if defined(TEST_MPFI_50)
#include <boost/multiprecision/mpfi.hpp>
#endif
#ifdef TEST_CPP_DEC_FLOAT
#include <boost/multiprecision/cpp_dec_float.hpp>
#endif
#ifdef TEST_CPP_BIN_FLOAT
#include <boost/multiprecision/cpp_bin_float.hpp>
#endif
#ifdef TEST_FLOAT128
#include <boost/multiprecision/float128.hpp>
#endif
#ifdef TEST_CPP_DOUBLE_FLOAT
#include <boost/multiprecision/cpp_double_fp.hpp>
#endif

#include <array>
#include <iostream>
#include <iomanip>

#ifdef BOOST_MSVC
#pragma warning(disable : 4127)
#endif

#if defined(TEST_CPP_DOUBLE_FLOAT)
template <class FloatingPointType, boost::multiprecision::expression_template_option ET>
bool has_bad_bankers_rounding(const boost::multiprecision::number<boost::multiprecision::backends::cpp_double_fp_backend<FloatingPointType>, ET>&)
{
   return true;
}
#endif
#if defined(TEST_MPF_50)
template <unsigned N, boost::multiprecision::expression_template_option ET>
bool has_bad_bankers_rounding(const boost::multiprecision::number<boost::multiprecision::gmp_float<N>, ET>&)
{
   return true;
}
#endif
#if defined(TEST_FLOAT128) && defined(BOOST_INTEL)
bool has_bad_bankers_rounding(const boost::multiprecision::float128&)
{
   return true;
}
#endif
template <class T>
bool has_bad_bankers_rounding(const T&)
{
   return false;
}

bool is_bankers_rounding_error(const std::string& s, const char* expect)
{
   // This check isn't foolproof: that would require *much* more sophisticated code!!!
   std::string::size_type l = std::strlen(expect);
   if (l != s.size())
      return false;
   std::string::size_type len = s.find('e');
   if (len == std::string::npos)
      len = l - 1;
   else
      --len;
   if (s.compare(0, len, expect, len))
      return false;
   if (s[len] != expect[len] + 1)
      return false;
   return true;
}

// LCOV_EXCL_START These lines are not expected to get hit in tests.
void print_flags(std::ios_base::fmtflags f)
{
   std::cout << "Formatting flags were: ";
   if (f & std::ios_base::scientific)
      std::cout << "scientific ";
   if (f & std::ios_base::fixed)
      std::cout << "fixed ";
   if (f & std::ios_base::showpoint)
      std::cout << "showpoint ";
   if (f & std::ios_base::showpos)
      std::cout << "showpos ";
   std::cout << std::endl;
}
// LCOV_EXCL_STOP These lines are not expected to get hit in tests.

template <class T>
void test()
{
   typedef T mp_t;

   std::cout << "Testing type of test: " << typeid(mp_t).name() << std::endl;

   std::array<std::ios_base::fmtflags, 9> f =
       {{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,
         std::ios_base::scientific | std::ios_base::showpoint, std::ios_base::fixed, std::ios_base::fixed | std::ios_base::showpoint,
         std::ios_base::fixed | std::ios_base::showpos}};

   std::array<std::array<const char*, 13 * 9>, 40> string_data = {{
#include "string_data.ipp"
   }};

   double num   = 123456789.0;
   double denom = 1;
   double val   = num;
   for (unsigned j = 0; j < 40; ++j)
   {
      unsigned col = 0;
      for (unsigned prec = 1; prec < 14; ++prec)
      {
         for (unsigned i = 0; i < f.size(); ++i, ++col)
         {
            std::stringstream ss;
            ss.precision(prec);
            ss.flags(f[i]);
            ss << mp_t(val);
            const char* expect = string_data[j][col];
            if (ss.str() != expect)
            {
               if (has_bad_bankers_rounding(mp_t()) && is_bankers_rounding_error(ss.str(), expect))
               {
                  std::cout << "Ignoring bankers-rounding error with " << typeid(T).name() << ".\n";
               }
               else
               {
                  // LCOV_EXCL_START These lines are not expected to get hit in tests.
                  std::cout << std::setprecision(20) << "Testing value " << val << std::endl;
                  print_flags(f[i]);
                  std::cout << "Precision is: " << prec << std::endl;
                  std::cout << "Got:      " << ss.str() << std::endl;
                  std::cout << "Expected: " << expect << std::endl;
                  ++boost::detail::test_errors();
                  mp_t(val).str(prec, f[i]); // for debugging
                  // LCOV_EXCL_STOP These lines are not expected to get hit in tests.
               }
            }
         }
      }
      num = -num;
      if (j & 1)
         denom *= 8;
      val = num / denom;
   }

   std::array<const char*, 13 * 9> zeros =
       {{"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"}};

   unsigned col = 0;
   val          = 0;
   for (unsigned prec = 1; prec < 14; ++prec)
   {
      for (unsigned i = 0; i < f.size(); ++i, ++col)
      {
         std::stringstream ss;
         ss.precision(prec);
         ss.flags(f[i]);
         ss << mp_t(val);
         const char* expect = zeros[col];
         if (ss.str() != expect)
         {
            // LCOV_EXCL_START These lines are not expected to get hit in tests.
            std::cout << std::setprecision(20) << "Testing value " << val << std::endl;
            print_flags(f[i]);
            std::cout << "Precision is: " << prec << std::endl;
            std::cout << "Got:      " << ss.str() << std::endl;
            std::cout << "Expected: " << expect << std::endl;
            ++boost::detail::test_errors();
            mp_t(val).str(prec, f[i]); // for debugging
            // LCOV_EXCL_STOP These lines are not expected to get hit in tests.
         }
      }
   }

   if (std::numeric_limits<mp_t>::has_infinity)
   {
      T val = std::numeric_limits<T>::infinity();
      BOOST_CHECK_EQUAL(val.str(), "inf");
      BOOST_CHECK_EQUAL(val.str(0, std::ios_base::showpos), "+inf");
      val = -val;
      BOOST_CHECK_EQUAL(val.str(), "-inf");
      BOOST_CHECK_EQUAL(val.str(0, std::ios_base::showpos), "-inf");

      val = static_cast<T>("inf");
      BOOST_CHECK_EQUAL(val, std::numeric_limits<T>::infinity());
      val = static_cast<T>("+inf");
      BOOST_CHECK_EQUAL(val, std::numeric_limits<T>::infinity());
      val = static_cast<T>("-inf");
      BOOST_CHECK_EQUAL(val, -std::numeric_limits<T>::infinity());
   }
   if (std::numeric_limits<mp_t>::has_quiet_NaN)
   {
      T val = std::numeric_limits<T>::quiet_NaN();
      BOOST_CHECK_EQUAL(val.str(), "nan");
      val = static_cast<T>("nan");
      BOOST_CHECK((boost::math::isnan)(val));
   }
   //
   // Bug cases:
   //
   // https://github.com/boostorg/multiprecision/issues/113
   //
   {
      T val("99.9809");
      BOOST_CHECK_EQUAL(val.str(3, std::ios_base::fixed), "99.981");
   }
}

template <class T>
T generate_random()
{
   typedef typename T::backend_type::exponent_type e_type;
   static boost::random::mt19937                   gen;
   T                                               val      = gen();
   T                                               prev_val = -1;
   while (val != prev_val)
   {
      val *= (gen.max)();
      prev_val = val;
      val += gen();
   }
   e_type e;
   val = frexp(val, &e);

   constexpr auto exp_range =
     (::has_poor_exp_range_or_precision_support<T>::value)
       ? static_cast<int>
         (
            static_cast<float>
            (
                 static_cast<float>(std::numeric_limits<T>::max_exponent10)
               - static_cast<float>(static_cast<float>(std::numeric_limits<T>::max_digits10) * 1.1F)
            )
            / 0.301F
         )
       : std::numeric_limits<T>::max_exponent - 10
       ;

   static boost::random::uniform_int_distribution<e_type> ui(0, exp_range);

   return ldexp(val, ui(gen));
}

template <class T>
struct max_digits10_proxy
{
   static const unsigned value = std::numeric_limits<T>::digits10 + 5;
};
#ifdef TEST_CPP_DEC_FLOAT
template <unsigned D, boost::multiprecision::expression_template_option ET>
struct max_digits10_proxy<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<D>, ET> >
{
   static const unsigned value = std::numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<D>, ET> >::max_digits10;
};
#endif
#ifdef TEST_MPF_50
template <unsigned D, boost::multiprecision::expression_template_option ET>
struct max_digits10_proxy<boost::multiprecision::number<boost::multiprecision::gmp_float<D>, ET> >
{
   static const unsigned value = std::numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<D>, ET> >::max_digits10;
};
#endif
#ifdef TEST_MPFR_50
template <unsigned D, boost::multiprecision::expression_template_option ET>
struct max_digits10_proxy<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<D>, ET> >
{
   static const unsigned value = std::numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<D>, ET> >::max_digits10;
};
#endif

template <class T>
void do_round_trip(const T& val, std::ios_base::fmtflags f)
{
   std::stringstream ss;
   ss << std::setprecision(std::numeric_limits<T>::max_digits10);
   ss.flags(f);
   ss << val;
   T new_val = static_cast<T>(ss.str());
   //BOOST_CHECK_EQUAL(new_val, val);
   BOOST_CHECK_CLOSE_FRACTION(new_val, val, std::numeric_limits<T>::epsilon() * 100000);
   new_val = static_cast<T>(val.str(f & std::ios_base::fixed ? std::numeric_limits<T>::max_digits10 : 0, f));
   //BOOST_CHECK_EQUAL(new_val, val);
   BOOST_CHECK_CLOSE_FRACTION(new_val, val, std::numeric_limits<T>::epsilon() * 100000);
}

template <class T>
void do_round_trip(const T& val)
{
   do_round_trip(val, std::ios_base::fmtflags(0));
   do_round_trip(val, std::ios_base::fmtflags(std::ios_base::scientific));
   if ((fabs(val) > 1) && (fabs(val) < 1e100))
      do_round_trip(val, std::ios_base::fmtflags(std::ios_base::fixed));
}

template <class T>
void test_round_trip()
{
   std::cout << "Testing type of test_round_trip: " << typeid(T).name() << std::endl;

   for (unsigned i = 0; i < 1000; ++i)
   {
      T val = generate_random<T>();
      do_round_trip(val);
      do_round_trip(T(-val));
      do_round_trip(T(1 / val));
      do_round_trip(T(-1 / val));
   }
}

template<typename T>
void test_to_string()
{
   std::cout << "Testing type of test_to_string: " << typeid(T).name() << std::endl;

   using std::to_string;
   T x0{"23.43"};
   BOOST_CHECK_EQUAL(to_string(x0), "23.430000");

   T x1{"1e-9"};
   BOOST_CHECK_EQUAL(to_string(x1), "0.000000");

   T x2{"123456789"};
   BOOST_CHECK_EQUAL(to_string(x2), "123456789.000000");
}

#ifdef TEST_FLOAT128
void test_hexadecimal_floating_point()
{
   using boost::multiprecision::float128;

   float128 x = 0x1p+0Q;

   std::string s = x.str(0, std::ios_base::fmtflags(std::ios_base::fixed | std::ios_base::scientific));
   BOOST_CHECK_EQUAL(s, "0x1p+0");

   s = x.str(0, std::ios_base::fmtflags(std::ios_base::floatfield));
   BOOST_CHECK_EQUAL(s, "0x1p+0");

   x = -1;
   s = x.str(0, std::ios_base::fmtflags(std::ios_base::floatfield));
   BOOST_CHECK_EQUAL(s, "-0x1p+0");

   // hexadecimal representation of pi; test a round trip:
   float128 pi1 = 0x1.921fb54442d18469898cc51701b8p+1Q;
   s            = pi1.str(0, std::ios_base::fmtflags(std::ios_base::floatfield));
   float128 pi2(s);
   BOOST_CHECK_EQUAL(pi1, pi2);
}
#endif

int main()
{
#ifdef TEST_MPFR_50
   test<boost::multiprecision::mpfr_float_50>();
   test<boost::multiprecision::mpfr_float_100>();

   test_round_trip<boost::multiprecision::mpfr_float_50>();
   test_round_trip<boost::multiprecision::mpfr_float_100>();

   test_to_string<boost::multiprecision::mpfr_float_50>();
   test_to_string<boost::multiprecision::mpfr_float_100>();

#endif
#ifdef TEST_MPFI_50
   test<boost::multiprecision::mpfr_float_50>();
   test<boost::multiprecision::mpfr_float_100>();

   test_round_trip<boost::multiprecision::mpfr_float_50>();
   test_round_trip<boost::multiprecision::mpfr_float_100>();
#endif
#ifdef TEST_CPP_DEC_FLOAT
   test<boost::multiprecision::cpp_dec_float_50>();
   test<boost::multiprecision::cpp_dec_float_100>();

   // cpp_dec_float has extra guard digits that messes this up:
   test_round_trip<boost::multiprecision::cpp_dec_float_50>();
   test_round_trip<boost::multiprecision::cpp_dec_float_100>();

   test_to_string<boost::multiprecision::cpp_dec_float_50>();
   test_to_string<boost::multiprecision::cpp_dec_float_100>();

#endif
#ifdef TEST_CPP_BIN_FLOAT
   test<boost::multiprecision::cpp_bin_float_50>();
   test<boost::multiprecision::cpp_bin_float_100>();

   test_round_trip<boost::multiprecision::cpp_bin_float_50>();
   test_round_trip<boost::multiprecision::cpp_bin_float_100>();

   test_to_string<boost::multiprecision::cpp_bin_float_50>();
   test_to_string<boost::multiprecision::cpp_bin_float_100>();

#endif
#ifdef TEST_MPF_50
   test<boost::multiprecision::mpf_float_50>();
   test<boost::multiprecision::mpf_float_100>();
   /*
   // I can't get this to work with mpf_t - mpf_str appears
   // not to actually print enough decimal digits:
   test_round_trip<boost::multiprecision::mpf_float_50>();
   test_round_trip<boost::multiprecision::mpf_float_100>();
   */
#endif
#ifdef TEST_FLOAT128
   test<boost::multiprecision::float128>();
   test_hexadecimal_floating_point();
   test_to_string<boost::multiprecision::float128>();
#ifndef BOOST_INTEL
   test_round_trip<boost::multiprecision::float128>();
#endif
#endif
#ifdef TEST_CPP_DOUBLE_FLOAT
   test<boost::multiprecision::cpp_double_double>();
   test_to_string<boost::multiprecision::cpp_double_double>();
   test_round_trip<boost::multiprecision::cpp_double_double>();

   test<boost::multiprecision::cpp_double_long_double>();
   test_to_string<boost::multiprecision::cpp_double_long_double>();
   test_round_trip<boost::multiprecision::cpp_double_long_double>();

   #if defined(BOOST_MP_CPP_DOUBLE_FP_HAS_FLOAT128)
   test<boost::multiprecision::cpp_double_float128>();
   test_to_string<boost::multiprecision::cpp_double_float128>();
   test_round_trip<boost::multiprecision::cpp_double_float128>();
   #endif
#endif
   return boost::report_errors();
}