File: test_convert_cpp_int_2_float.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 (109 lines) | stat: -rw-r--r-- 3,192 bytes parent folder | download | duplicates (11)
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
///////////////////////////////////////////////////////////////
//  Copyright 2021 John Maddock. 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
//
// Simple test that cpp_int -> double conversion has less than 0.5ulp error
// and rounds to even in case of ties.
// See https://github.com/boostorg/multiprecision/issues/360.
//
#ifdef _MSC_VER
#define _SCL_SECURE_NO_WARNINGS
#endif

#include <boost/multiprecision/cpp_int.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int.hpp>
#include "test.hpp"

using namespace boost::multiprecision;

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

template <class T>
T generate_random(unsigned bits_wanted)
{
   static boost::random::mt19937               gen;
   typedef boost::random::mt19937::result_type random_type;

   T        max_val;
   unsigned digits;
   if (std::numeric_limits<T>::is_bounded && (bits_wanted == (unsigned)std::numeric_limits<T>::digits))
   {
      max_val = (std::numeric_limits<T>::max)();
      digits  = std::numeric_limits<T>::digits;
   }
   else
   {
      max_val = T(1) << bits_wanted;
      digits  = bits_wanted;
   }

   unsigned bits_per_r_val = std::numeric_limits<random_type>::digits - 1;
   while ((random_type(1) << bits_per_r_val) > (gen.max)())
      --bits_per_r_val;

   unsigned terms_needed = digits / bits_per_r_val + 1;

   T val = 0;
   for (unsigned i = 0; i < terms_needed; ++i)
   {
      val *= (gen.max)();
      val += gen();
   }
   val %= max_val;
   return val;
}

template <class From, class To>
void test_convert()
{
   boost::random::mt19937 gen;
   boost::random::uniform_int_distribution<> d(20, (std::min)(200, std::numeric_limits<To>::max_exponent - 2));

   for (unsigned i = 0; i < 10000; ++i)
   {
      int  bits = d(gen);
      From from = generate_random<From>(bits);
      To   t1(from);
      From b(t1);
      std::size_t m = msb(from);
      if (m >= std::numeric_limits<To>::digits)
      {
         // For error <= 1ulp
         // Note msb(from) returns one less than the number of bits in from:
         From max_error = (From(1) << (m - std::numeric_limits<To>::digits));
         BOOST_TEST_GE(max_error, abs(b - from));
         if (max_error < abs(b - from))
            // debugging help:
            std::cout << from << std::endl
                      << b << std::endl
                      << abs(b - from) << std::endl;
         if (max_error == abs(b - from))
         {
            // Check we rounded to even in case of tie:
            BOOST_TEST_GT(lsb(b), (1 + msb(from) - std::numeric_limits<To>::digits));
            if (lsb(b) <= (1 + msb(from) - std::numeric_limits<To>::digits))
            {
               // debugging help:
               std::cout << from << std::endl
                         << b << std::endl
                         << abs(b - from) << std::endl;
            }
         }
      }
      else
      {
         BOOST_TEST_EQ(b, from);
      }
   }
}

int main()
{
   test_convert<cpp_int, float>();
   test_convert<cpp_int, double>();
   return boost::report_errors();
}