test_fixed_int.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. ///////////////////////////////////////////////////////////////
  2. // Copyright 2012 John Maddock. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
  5. //
  6. // Compare arithmetic results using fixed_int to GMP results.
  7. //
  8. #ifdef _MSC_VER
  9. #define _SCL_SECURE_NO_WARNINGS
  10. #endif
  11. #include <boost/multiprecision/gmp.hpp>
  12. #include <boost/multiprecision/fixed_int.hpp>
  13. #include <boost/random/mersenne_twister.hpp>
  14. #include <boost/random/uniform_int.hpp>
  15. #include "test.hpp"
  16. template <class T>
  17. T generate_random(unsigned bits_wanted)
  18. {
  19. static boost::random::mt19937 gen;
  20. typedef boost::random::mt19937::result_type random_type;
  21. T max_val;
  22. unsigned digits;
  23. if (std::numeric_limits<T>::is_bounded && (bits_wanted == std::numeric_limits<T>::digits))
  24. {
  25. max_val = (std::numeric_limits<T>::max)();
  26. digits = std::numeric_limits<T>::digits;
  27. }
  28. else
  29. {
  30. max_val = T(1) << bits_wanted;
  31. digits = bits_wanted;
  32. }
  33. unsigned bits_per_r_val = std::numeric_limits<random_type>::digits - 1;
  34. while ((random_type(1) << bits_per_r_val) > (gen.max)())
  35. --bits_per_r_val;
  36. unsigned terms_needed = digits / bits_per_r_val + 1;
  37. T val = 0;
  38. for (unsigned i = 0; i < terms_needed; ++i)
  39. {
  40. val *= (gen.max)();
  41. val += gen();
  42. }
  43. val %= max_val;
  44. return val;
  45. }
  46. int main()
  47. {
  48. using namespace boost::multiprecision;
  49. typedef number<fixed_int<1024, true> > packed_type;
  50. unsigned last_error_count = 0;
  51. for (int i = 0; i < 1000; ++i)
  52. {
  53. mpz_int a = generate_random<mpz_int>(1000);
  54. mpz_int b = generate_random<mpz_int>(512);
  55. mpz_int c = generate_random<mpz_int>(256);
  56. mpz_int d = generate_random<mpz_int>(32);
  57. int si = d.convert_to<int>();
  58. packed_type a1 = a.str();
  59. packed_type b1 = b.str();
  60. packed_type c1 = c.str();
  61. packed_type d1 = d.str();
  62. BOOST_CHECK_EQUAL(a.str(), a1.str());
  63. BOOST_CHECK_EQUAL(b.str(), b1.str());
  64. BOOST_CHECK_EQUAL(c.str(), c1.str());
  65. BOOST_CHECK_EQUAL(d.str(), d1.str());
  66. BOOST_CHECK_EQUAL(mpz_int(a + b).str(), packed_type(a1 + b1).str());
  67. BOOST_CHECK_EQUAL(mpz_int(a - b).str(), packed_type(a1 - b1).str());
  68. BOOST_CHECK_EQUAL(mpz_int(mpz_int(-a) + b).str(), packed_type(packed_type(-a1) + b1).str());
  69. BOOST_CHECK_EQUAL(mpz_int(mpz_int(-a) - b).str(), packed_type(packed_type(-a1) - b1).str());
  70. BOOST_CHECK_EQUAL(mpz_int(c * d).str(), packed_type(c1 * d1).str());
  71. BOOST_CHECK_EQUAL(mpz_int(c * -d).str(), packed_type(c1 * -d1).str());
  72. BOOST_CHECK_EQUAL(mpz_int(-c * d).str(), packed_type(-c1 * d1).str());
  73. BOOST_CHECK_EQUAL(mpz_int(b * c).str(), packed_type(b1 * c1).str());
  74. BOOST_CHECK_EQUAL(mpz_int(a / b).str(), packed_type(a1 / b1).str());
  75. BOOST_CHECK_EQUAL(mpz_int(a / -b).str(), packed_type(a1 / -b1).str());
  76. BOOST_CHECK_EQUAL(mpz_int(-a / b).str(), packed_type(-a1 / b1).str());
  77. BOOST_CHECK_EQUAL(mpz_int(a / d).str(), packed_type(a1 / d1).str());
  78. BOOST_CHECK_EQUAL(mpz_int(a % b).str(), packed_type(a1 % b1).str());
  79. BOOST_CHECK_EQUAL(mpz_int(a % -b).str(), packed_type(a1 % -b1).str());
  80. BOOST_CHECK_EQUAL(mpz_int(-a % b).str(), packed_type(-a1 % b1).str());
  81. BOOST_CHECK_EQUAL(mpz_int(a % d).str(), packed_type(a1 % d1).str());
  82. // bitwise ops:
  83. BOOST_CHECK_EQUAL(mpz_int(a | b).str(), packed_type(a1 | b1).str());
  84. BOOST_CHECK_EQUAL(mpz_int(a & b).str(), packed_type(a1 & b1).str());
  85. BOOST_CHECK_EQUAL(mpz_int(a ^ b).str(), packed_type(a1 ^ b1).str());
  86. // Now check operations involving integers:
  87. BOOST_CHECK_EQUAL(mpz_int(a + si).str(), packed_type(a1 + si).str());
  88. BOOST_CHECK_EQUAL(mpz_int(a + -si).str(), packed_type(a1 + -si).str());
  89. BOOST_CHECK_EQUAL(mpz_int(-a + si).str(), packed_type(-a1 + si).str());
  90. BOOST_CHECK_EQUAL(mpz_int(si + a).str(), packed_type(si + a1).str());
  91. BOOST_CHECK_EQUAL(mpz_int(a - si).str(), packed_type(a1 - si).str());
  92. BOOST_CHECK_EQUAL(mpz_int(a - -si).str(), packed_type(a1 - -si).str());
  93. BOOST_CHECK_EQUAL(mpz_int(-a - si).str(), packed_type(-a1 - si).str());
  94. BOOST_CHECK_EQUAL(mpz_int(si - a).str(), packed_type(si - a1).str());
  95. BOOST_CHECK_EQUAL(mpz_int(b * si).str(), packed_type(b1 * si).str());
  96. BOOST_CHECK_EQUAL(mpz_int(b * -si).str(), packed_type(b1 * -si).str());
  97. BOOST_CHECK_EQUAL(mpz_int(-b * si).str(), packed_type(-b1 * si).str());
  98. BOOST_CHECK_EQUAL(mpz_int(si * b).str(), packed_type(si * b1).str());
  99. BOOST_CHECK_EQUAL(mpz_int(a / si).str(), packed_type(a1 / si).str());
  100. BOOST_CHECK_EQUAL(mpz_int(a / -si).str(), packed_type(a1 / -si).str());
  101. BOOST_CHECK_EQUAL(mpz_int(-a / si).str(), packed_type(-a1 / si).str());
  102. BOOST_CHECK_EQUAL(mpz_int(a % si).str(), packed_type(a1 % si).str());
  103. BOOST_CHECK_EQUAL(mpz_int(a % -si).str(), packed_type(a1 % -si).str());
  104. BOOST_CHECK_EQUAL(mpz_int(-a % si).str(), packed_type(-a1 % si).str());
  105. BOOST_CHECK_EQUAL(mpz_int(a | si).str(), packed_type(a1 | si).str());
  106. BOOST_CHECK_EQUAL(mpz_int(a & si).str(), packed_type(a1 & si).str());
  107. BOOST_CHECK_EQUAL(mpz_int(a ^ si).str(), packed_type(a1 ^ si).str());
  108. BOOST_CHECK_EQUAL(mpz_int(si | a).str(), packed_type(si | a1).str());
  109. BOOST_CHECK_EQUAL(mpz_int(si & a).str(), packed_type(si & a1).str());
  110. BOOST_CHECK_EQUAL(mpz_int(si ^ a).str(), packed_type(si ^ a1).str());
  111. BOOST_CHECK_EQUAL(mpz_int(gcd(a, b)).str(), packed_type(gcd(a1, b1)).str());
  112. BOOST_CHECK_EQUAL(mpz_int(lcm(c, d)).str(), packed_type(lcm(c1, d1)).str());
  113. if (last_error_count != boost::detail::test_errors())
  114. {
  115. last_error_count = boost::detail::test_errors();
  116. std::cout << std::hex << std::showbase;
  117. std::cout << "a = " << a << std::endl;
  118. std::cout << "a1 = " << a1 << std::endl;
  119. std::cout << "b = " << b << std::endl;
  120. std::cout << "b1 = " << b1 << std::endl;
  121. std::cout << "c = " << c << std::endl;
  122. std::cout << "c1 = " << c1 << std::endl;
  123. std::cout << "d = " << d << std::endl;
  124. std::cout << "d1 = " << d1 << std::endl;
  125. std::cout << "a + b = " << a + b << std::endl;
  126. std::cout << "a1 + b1 = " << a1 + b1 << std::endl;
  127. std::cout << std::dec;
  128. std::cout << "a - b = " << a - b << std::endl;
  129. std::cout << "a1 - b1 = " << a1 - b1 << std::endl;
  130. std::cout << "-a + b = " << mpz_int(-a) + b << std::endl;
  131. std::cout << "-a1 + b1 = " << packed_type(-a1) + b1 << std::endl;
  132. std::cout << "-a - b = " << mpz_int(-a) - b << std::endl;
  133. std::cout << "-a1 - b1 = " << packed_type(-a1) - b1 << std::endl;
  134. std::cout << "c*d = " << c * d << std::endl;
  135. std::cout << "c1*d1 = " << c1 * d1 << std::endl;
  136. std::cout << "b*c = " << b * c << std::endl;
  137. std::cout << "b1*c1 = " << b1 * c1 << std::endl;
  138. std::cout << "a/b = " << a / b << std::endl;
  139. std::cout << "a1/b1 = " << a1 / b1 << std::endl;
  140. std::cout << "a/d = " << a / d << std::endl;
  141. std::cout << "a1/d1 = " << a1 / d1 << std::endl;
  142. std::cout << "a%b = " << a % b << std::endl;
  143. std::cout << "a1%b1 = " << a1 % b1 << std::endl;
  144. std::cout << "a%d = " << a % d << std::endl;
  145. std::cout << "a1%d1 = " << a1 % d1 << std::endl;
  146. }
  147. }
  148. return boost::report_errors();
  149. }