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
|
///////////////////////////////////////////////////////////////
// Copyright 2012 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_
//
// Compare arithmetic results using fixed_int to GMP results.
//
#ifdef _MSC_VER
# define _SCL_SECURE_NO_WARNINGS
#endif
#include <boost/multiprecision/gmp.hpp>
#include <boost/multiprecision/fixed_int.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int.hpp>
#include "test.hpp"
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 == 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;
}
int main()
{
using namespace boost::multiprecision;
typedef number<fixed_int<1024, true> > packed_type;
unsigned last_error_count = 0;
for(int i = 0; i < 1000; ++i)
{
mpz_int a = generate_random<mpz_int>(1000);
mpz_int b = generate_random<mpz_int>(512);
mpz_int c = generate_random<mpz_int>(256);
mpz_int d = generate_random<mpz_int>(32);
int si = d.convert_to<int>();
packed_type a1 = a.str();
packed_type b1 = b.str();
packed_type c1 = c.str();
packed_type d1 = d.str();
BOOST_CHECK_EQUAL(a.str(), a1.str());
BOOST_CHECK_EQUAL(b.str(), b1.str());
BOOST_CHECK_EQUAL(c.str(), c1.str());
BOOST_CHECK_EQUAL(d.str(), d1.str());
BOOST_CHECK_EQUAL(mpz_int(a+b).str(), packed_type(a1 + b1).str());
BOOST_CHECK_EQUAL(mpz_int(a-b).str(), packed_type(a1 - b1).str());
BOOST_CHECK_EQUAL(mpz_int(mpz_int(-a)+b).str(), packed_type(packed_type(-a1) + b1).str());
BOOST_CHECK_EQUAL(mpz_int(mpz_int(-a)-b).str(), packed_type(packed_type(-a1) - b1).str());
BOOST_CHECK_EQUAL(mpz_int(c * d).str(), packed_type(c1 * d1).str());
BOOST_CHECK_EQUAL(mpz_int(c * -d).str(), packed_type(c1 * -d1).str());
BOOST_CHECK_EQUAL(mpz_int(-c * d).str(), packed_type(-c1 * d1).str());
BOOST_CHECK_EQUAL(mpz_int(b * c).str(), packed_type(b1 * c1).str());
BOOST_CHECK_EQUAL(mpz_int(a / b).str(), packed_type(a1 / b1).str());
BOOST_CHECK_EQUAL(mpz_int(a / -b).str(), packed_type(a1 / -b1).str());
BOOST_CHECK_EQUAL(mpz_int(-a / b).str(), packed_type(-a1 / b1).str());
BOOST_CHECK_EQUAL(mpz_int(a / d).str(), packed_type(a1 / d1).str());
BOOST_CHECK_EQUAL(mpz_int(a % b).str(), packed_type(a1 % b1).str());
BOOST_CHECK_EQUAL(mpz_int(a % -b).str(), packed_type(a1 % -b1).str());
BOOST_CHECK_EQUAL(mpz_int(-a % b).str(), packed_type(-a1 % b1).str());
BOOST_CHECK_EQUAL(mpz_int(a % d).str(), packed_type(a1 % d1).str());
// bitwise ops:
BOOST_CHECK_EQUAL(mpz_int(a|b).str(), packed_type(a1 | b1).str());
BOOST_CHECK_EQUAL(mpz_int(a&b).str(), packed_type(a1 & b1).str());
BOOST_CHECK_EQUAL(mpz_int(a^b).str(), packed_type(a1 ^ b1).str());
// Now check operations involving integers:
BOOST_CHECK_EQUAL(mpz_int(a + si).str(), packed_type(a1 + si).str());
BOOST_CHECK_EQUAL(mpz_int(a + -si).str(), packed_type(a1 + -si).str());
BOOST_CHECK_EQUAL(mpz_int(-a + si).str(), packed_type(-a1 + si).str());
BOOST_CHECK_EQUAL(mpz_int(si + a).str(), packed_type(si + a1).str());
BOOST_CHECK_EQUAL(mpz_int(a - si).str(), packed_type(a1 - si).str());
BOOST_CHECK_EQUAL(mpz_int(a - -si).str(), packed_type(a1 - -si).str());
BOOST_CHECK_EQUAL(mpz_int(-a - si).str(), packed_type(-a1 - si).str());
BOOST_CHECK_EQUAL(mpz_int(si - a).str(), packed_type(si - a1).str());
BOOST_CHECK_EQUAL(mpz_int(b * si).str(), packed_type(b1 * si).str());
BOOST_CHECK_EQUAL(mpz_int(b * -si).str(), packed_type(b1 * -si).str());
BOOST_CHECK_EQUAL(mpz_int(-b * si).str(), packed_type(-b1 * si).str());
BOOST_CHECK_EQUAL(mpz_int(si * b).str(), packed_type(si * b1).str());
BOOST_CHECK_EQUAL(mpz_int(a / si).str(), packed_type(a1 / si).str());
BOOST_CHECK_EQUAL(mpz_int(a / -si).str(), packed_type(a1 / -si).str());
BOOST_CHECK_EQUAL(mpz_int(-a / si).str(), packed_type(-a1 / si).str());
BOOST_CHECK_EQUAL(mpz_int(a % si).str(), packed_type(a1 % si).str());
BOOST_CHECK_EQUAL(mpz_int(a % -si).str(), packed_type(a1 % -si).str());
BOOST_CHECK_EQUAL(mpz_int(-a % si).str(), packed_type(-a1 % si).str());
BOOST_CHECK_EQUAL(mpz_int(a|si).str(), packed_type(a1 | si).str());
BOOST_CHECK_EQUAL(mpz_int(a&si).str(), packed_type(a1 & si).str());
BOOST_CHECK_EQUAL(mpz_int(a^si).str(), packed_type(a1 ^ si).str());
BOOST_CHECK_EQUAL(mpz_int(si|a).str(), packed_type(si|a1).str());
BOOST_CHECK_EQUAL(mpz_int(si&a).str(), packed_type(si&a1).str());
BOOST_CHECK_EQUAL(mpz_int(si^a).str(), packed_type(si^a1).str());
BOOST_CHECK_EQUAL(mpz_int(gcd(a, b)).str(), packed_type(gcd(a1, b1)).str());
BOOST_CHECK_EQUAL(mpz_int(lcm(c, d)).str(), packed_type(lcm(c1, d1)).str());
if(last_error_count != boost::detail::test_errors())
{
last_error_count = boost::detail::test_errors();
std::cout << std::hex << std::showbase;
std::cout << "a = " << a << std::endl;
std::cout << "a1 = " << a1 << std::endl;
std::cout << "b = " << b << std::endl;
std::cout << "b1 = " << b1 << std::endl;
std::cout << "c = " << c << std::endl;
std::cout << "c1 = " << c1 << std::endl;
std::cout << "d = " << d << std::endl;
std::cout << "d1 = " << d1 << std::endl;
std::cout << "a + b = " << a+b << std::endl;
std::cout << "a1 + b1 = " << a1+b1 << std::endl;
std::cout << std::dec;
std::cout << "a - b = " << a-b << std::endl;
std::cout << "a1 - b1 = " << a1-b1 << std::endl;
std::cout << "-a + b = " << mpz_int(-a)+b << std::endl;
std::cout << "-a1 + b1 = " << packed_type(-a1)+b1 << std::endl;
std::cout << "-a - b = " << mpz_int(-a)-b << std::endl;
std::cout << "-a1 - b1 = " << packed_type(-a1)-b1 << std::endl;
std::cout << "c*d = " << c*d << std::endl;
std::cout << "c1*d1 = " << c1*d1 << std::endl;
std::cout << "b*c = " << b*c << std::endl;
std::cout << "b1*c1 = " << b1*c1 << std::endl;
std::cout << "a/b = " << a/b << std::endl;
std::cout << "a1/b1 = " << a1/b1 << std::endl;
std::cout << "a/d = " << a/d << std::endl;
std::cout << "a1/d1 = " << a1/d1 << std::endl;
std::cout << "a%b = " << a%b << std::endl;
std::cout << "a1%b1 = " << a1%b1 << std::endl;
std::cout << "a%d = " << a%d << std::endl;
std::cout << "a1%d1 = " << a1%d1 << std::endl;
}
}
return boost::report_errors();
}
|