File: test_cpp_bin_float_conv.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 (283 lines) | stat: -rw-r--r-- 10,266 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
///////////////////////////////////////////////////////////////
//  Copyright 2012 - 2025 John Maddock.
//  Copyright 2025 Christopher Kormanyos.
//  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/detail/lightweight_test.hpp>

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

#include <array>
#include <cstdint>

template <class T>
T generate_random()
{
   typedef int                   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);

   static boost::random::uniform_int_distribution<e_type> ui(-20, 20);
   return ldexp(val, ui(gen));
}

template <class T>
void check_round(const T& val, bool check_extended = false)
{
   double d1    = val.template convert_to<double>();
   double d2    = boost::math::nextafter(d1, d1 < val ? DBL_MAX : -DBL_MAX);
   T      diff1 = abs(d1 - val);
   T      diff2 = abs(d2 - val);
   if (diff2 < diff1)
   {
      // Some debugging code here...
      // LCOV_EXCL_START These lines are not expected to get hit in tests.
      std::cout << val.str() << std::endl;
      std::cout << std::setprecision(18);
      std::cout << d1 << std::endl;
      std::cout << d2 << std::endl;
      std::cout << diff1 << std::endl;
      std::cout << diff2 << std::endl;
      d1 = val.template convert_to<double>();
      // LCOV_EXCL_STOP These lines are not expected to get hit in tests.
   }

   using std::signbit;

   BOOST_CHECK(diff2 >= diff1);
   // Note: Ask John if boost::multiprecision::signbit() should return bool?
   BOOST_CHECK_EQUAL((boost::math::signbit(val) != 0), signbit(d1));

   float f1 = val.template convert_to<float>();
   float f2 = boost::math::nextafter(f1, f1 < val ? FLT_MAX : -FLT_MAX);
   BOOST_CHECK(((abs(f1 - val) <= abs(f2 - val))));
   BOOST_CHECK_EQUAL((boost::math::signbit(val) != 0), signbit(f1));

#if !defined(BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS)

   if (check_extended)
   {
      //
      // We should check long double as well:
      //
      long double l1 = val.template convert_to<long double>();
      long double l2 = boost::math::nextafter(d1, d1 < val ? LDBL_MAX : -LDBL_MAX);
      diff1          = abs(l1 - val);
      diff2          = abs(l2 - val);
      if (diff2 < diff1)
      {
         // Some debugging code here...
         // LCOV_EXCL_START These lines are not expected to get hit in tests.
         std::cout << val.str() << std::endl;
         std::cout << std::setprecision(18);
         std::cout << l1 << std::endl;
         std::cout << l2 << std::endl;
         std::cout << diff1 << std::endl;
         std::cout << diff2 << std::endl;
         l1 = val.template convert_to<long double>();
         // LCOV_EXCL_STOP These lines are not expected to get hit in tests.
      }
      BOOST_CHECK(diff2 >= diff1);
      BOOST_CHECK_EQUAL((boost::math::signbit(val) != 0), signbit(l1));
   }

#endif
}

int main()
{
   using namespace boost::multiprecision;

   cpp_int          i(20);
   cpp_bin_float_50 f50(i);
   BOOST_CHECK_EQUAL(f50, 20);
   f50 = i.convert_to<cpp_bin_float_50>();
   BOOST_CHECK_EQUAL(f50, 20);

   int1024_t         i1024(45);
   cpp_bin_float_100 f100(i1024);
   BOOST_CHECK_EQUAL(f100, 45);
   f100 = i1024.convert_to<cpp_bin_float_100>();
   BOOST_CHECK_EQUAL(f100, 45);

   uint1024_t        ui1024(55);
   cpp_bin_float_100 f100b(ui1024);
   BOOST_CHECK_EQUAL(f100b, 55);
   f100b = ui1024.convert_to<cpp_bin_float_100>();
   BOOST_CHECK_EQUAL(f100b, 55);

   typedef number<cpp_int_backend<32, 32>, et_off> i32_t;
   i32_t                                           i32(67);
   cpp_bin_float_100                               f100c(i32);
   BOOST_CHECK_EQUAL(f100c, 67);
   f100c = i32.convert_to<cpp_bin_float_100>();
   BOOST_CHECK_EQUAL(f100c, 67);

   typedef number<cpp_int_backend<32, 32, unsigned_magnitude>, et_off> ui32_t;
   ui32_t                                                              ui32(98);
   cpp_bin_float_100                                                   f100d(ui32);
   BOOST_CHECK_EQUAL(f100d, 98);
   f100d = ui32.convert_to<cpp_bin_float_100>();
   BOOST_CHECK_EQUAL(f100d, 98);

   typedef number<cpp_int_backend<64, 64>, et_off> i64_t;
   i64_t                                           i64(67);
   cpp_bin_float_100                               f100e(i64);
   BOOST_CHECK_EQUAL(f100e, 67);
   f100e = i64.convert_to<cpp_bin_float_100>();
   BOOST_CHECK_EQUAL(f100e, 67);

   typedef number<cpp_int_backend<64, 64, unsigned_magnitude>, et_off> ui64_t;
   ui64_t                                                              ui64(98);
   cpp_bin_float_100                                                   f100f(ui64);
   BOOST_CHECK_EQUAL(f100f, 98);
   f100f = ui64.convert_to<cpp_bin_float_100>();
   BOOST_CHECK_EQUAL(f100f, 98);

   typedef number<cpp_int_backend<128, 128>, et_off> i128_t;
   i128_t                                            i128(67);
   cpp_bin_float_100                                 f100g(i128);
   BOOST_CHECK_EQUAL(f100g, 67);
   f100g = i128.convert_to<cpp_bin_float_100>();
   BOOST_CHECK_EQUAL(f100g, 67);

   typedef number<cpp_int_backend<128, 128, unsigned_magnitude>, et_off> ui128_t;
   ui128_t                                                               ui128(98);
   cpp_bin_float_100                                                     f100h(ui128);
   BOOST_CHECK_EQUAL(f100h, 98);
   f100h = ui128.convert_to<cpp_bin_float_100>();
   BOOST_CHECK_EQUAL(f100h, 98);

   cpp_bin_float_quad q = (std::numeric_limits<float>::min)();
   BOOST_CHECK_EQUAL(q.convert_to<float>(), (std::numeric_limits<float>::min)());
   q = (std::numeric_limits<float>::max)();
   BOOST_CHECK_EQUAL(q.convert_to<float>(), (std::numeric_limits<float>::max)());
   q = (std::numeric_limits<float>::denorm_min)();
   BOOST_CHECK_EQUAL(q.convert_to<float>(), (std::numeric_limits<float>::denorm_min)());
   // See https://svn.boost.org/trac/boost/ticket/12512:
   boost::multiprecision::number<boost::multiprecision::backends::cpp_bin_float<128, boost::multiprecision::backends::digit_base_2, void, std::int64_t> > ext_float("1e-646456978");
   BOOST_CHECK_EQUAL(ext_float.convert_to<float>(), 0);

   q = -(std::numeric_limits<float>::min)();
   BOOST_CHECK_EQUAL(q.convert_to<float>(), -(std::numeric_limits<float>::min)());
   q = -(std::numeric_limits<float>::max)();
   BOOST_CHECK_EQUAL(q.convert_to<float>(), -(std::numeric_limits<float>::max)());
   q = -(std::numeric_limits<float>::denorm_min)();
   BOOST_CHECK_EQUAL(q.convert_to<float>(), -(std::numeric_limits<float>::denorm_min)());
   // See https://svn.boost.org/trac/boost/ticket/12512:
   ext_float = boost::multiprecision::number<boost::multiprecision::backends::cpp_bin_float<128, boost::multiprecision::backends::digit_base_2, void, std::int64_t> >("-1e-646456978");
   BOOST_CHECK_EQUAL(ext_float.convert_to<float>(), 0);
   ext_float = (std::numeric_limits<double>::max)();
   ext_float *= 16;
   if (std::numeric_limits<double>::has_infinity)
   {
      BOOST_CHECK_EQUAL(ext_float.convert_to<double>(), std::numeric_limits<double>::infinity());
   }
   else
   {
      BOOST_CHECK_EQUAL(ext_float.convert_to<double>(), (std::numeric_limits<double>::max)());
   }
   ext_float = -ext_float;
   if (std::numeric_limits<double>::has_infinity)
   {
      BOOST_CHECK_EQUAL(ext_float.convert_to<double>(), -std::numeric_limits<double>::infinity());
   }
   else
   {
      BOOST_CHECK_EQUAL(ext_float.convert_to<double>(), -(std::numeric_limits<double>::max)());
   }
   //
   // Check for double rounding when the result would be a denorm.
   // See https://svn.boost.org/trac/boost/ticket/12527
   //
   cpp_bin_float_50 r1 = ldexp(cpp_bin_float_50(0x8000000000000bffull), -63 - 1023);
   check_round(r1);
   r1 = -r1;
   check_round(r1);

   r1 = ldexp(cpp_bin_float_50(0x8000017f), -31 - 127);
   check_round(r1);
   r1 = -r1;
   check_round(r1);
   //
   // Check conversion to signed zero works OK:
   //
   r1 = -ldexp(cpp_bin_float_50(1), -3000);
   check_round(r1);
   r1 = 0;
   r1 = -r1;
   BOOST_CHECK(boost::math::signbit(r1));
   check_round(r1);
   //
   // Check boundary as the exponent drops below what a double can cope with
   // but we will be rounding up:
   //
   r1 = 3;
   r1 = ldexp(r1, std::numeric_limits<double>::min_exponent);
   do
   {
      check_round(r1);
      check_round(boost::math::float_next(r1));
      check_round(boost::math::float_prior(r1));
      r1 = ldexp(r1, -1);
   } while (ilogb(r1) > std::numeric_limits<double>::min_exponent - 5 - std::numeric_limits<double>::digits);
   r1 = -3;
   r1 = ldexp(r1, std::numeric_limits<double>::min_exponent);
   do
   {
      check_round(r1);
      check_round(boost::math::float_next(r1));
      check_round(boost::math::float_prior(r1));
      r1 = ldexp(r1, -1);
   } while (ilogb(r1) > std::numeric_limits<double>::min_exponent - 5 - std::numeric_limits<double>::digits);
   //
   // Again when not rounding up:
   //
   r1 = 1;
   r1 = ldexp(r1, std::numeric_limits<double>::min_exponent);
   do
   {
      check_round(r1);
      check_round(boost::math::float_next(r1));
      check_round(boost::math::float_prior(r1));
      r1 = ldexp(r1, -1);
   } while (ilogb(r1) > std::numeric_limits<double>::min_exponent - 5 - std::numeric_limits<double>::digits);
   r1 = -1;
   r1 = ldexp(r1, std::numeric_limits<double>::min_exponent);
   do
   {
      check_round(r1);
      check_round(boost::math::float_next(r1));
      check_round(boost::math::float_prior(r1));
      r1 = ldexp(r1, -1);
   } while (ilogb(r1) > std::numeric_limits<double>::min_exponent - 5 - std::numeric_limits<double>::digits);
   //
   // Test random conversions:
   //
   for (unsigned j = 0; j < 10000; ++j)
   {
      check_round(generate_random<cpp_bin_float_50>(), true);
   }

   return boost::report_errors();
}