test_convert_from_mpz_int.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. #ifdef _MSC_VER
  6. #define _SCL_SECURE_NO_WARNINGS
  7. #endif
  8. #if defined(HAS_GMP)
  9. #include <boost/multiprecision/cpp_int.hpp>
  10. #include <boost/random/mersenne_twister.hpp>
  11. #include <boost/multiprecision/gmp.hpp>
  12. #include "test.hpp"
  13. #if defined(HAS_MPFR)
  14. #include <boost/multiprecision/mpfr.hpp>
  15. #endif
  16. #if defined(HAS_MPFI)
  17. #include <boost/multiprecision/mpfi.hpp>
  18. #endif
  19. #ifdef HAS_TOMMATH
  20. #include <boost/multiprecision/tommath.hpp>
  21. #endif
  22. #ifdef HAS_FLOAT128
  23. #include <boost/multiprecision/float128.hpp>
  24. #endif
  25. #include <boost/multiprecision/cpp_bin_float.hpp>
  26. #include <boost/multiprecision/cpp_dec_float.hpp>
  27. using namespace boost::multiprecision;
  28. #ifdef BOOST_MSVC
  29. #pragma warning(disable : 4127)
  30. #endif
  31. template <class T>
  32. T generate_random(unsigned bits_wanted)
  33. {
  34. static boost::random::mt19937 gen;
  35. typedef boost::random::mt19937::result_type random_type;
  36. T max_val;
  37. unsigned digits;
  38. if (std::numeric_limits<T>::is_bounded && (bits_wanted == (unsigned)std::numeric_limits<T>::digits))
  39. {
  40. max_val = (std::numeric_limits<T>::max)();
  41. digits = std::numeric_limits<T>::digits;
  42. }
  43. else
  44. {
  45. max_val = T(1) << bits_wanted;
  46. digits = bits_wanted;
  47. }
  48. unsigned bits_per_r_val = std::numeric_limits<random_type>::digits - 1;
  49. while ((random_type(1) << bits_per_r_val) > (gen.max)())
  50. --bits_per_r_val;
  51. unsigned terms_needed = digits / bits_per_r_val + 1;
  52. T val = 0;
  53. for (unsigned i = 0; i < terms_needed; ++i)
  54. {
  55. val *= (gen.max)();
  56. val += gen();
  57. }
  58. val %= max_val;
  59. return val;
  60. }
  61. template <class From, class To>
  62. void test_convert_neg_int(From from, const boost::mpl::true_&)
  63. {
  64. from = -from;
  65. To t3(from);
  66. To t4 = from.template convert_to<To>();
  67. BOOST_CHECK_EQUAL(from.str(), t3.str());
  68. BOOST_CHECK_EQUAL(from.str(), t4.str());
  69. }
  70. template <class From, class To>
  71. void test_convert_neg_int(From const&, const boost::mpl::false_&)
  72. {
  73. }
  74. template <class From, class To>
  75. void test_convert_imp(boost::mpl::int_<number_kind_integer> const&, boost::mpl::int_<number_kind_integer> const&)
  76. {
  77. int bits_wanted = (std::min)((std::min)(std::numeric_limits<From>::digits, std::numeric_limits<To>::digits), 2000);
  78. for (unsigned i = 0; i < 100; ++i)
  79. {
  80. From from = generate_random<From>(bits_wanted);
  81. To t1(from);
  82. To t2 = from.template convert_to<To>();
  83. BOOST_CHECK_EQUAL(from.str(), t1.str());
  84. BOOST_CHECK_EQUAL(from.str(), t2.str());
  85. test_convert_neg_int<From, To>(from, boost::mpl::bool_ < std::numeric_limits<From>::is_signed && std::numeric_limits<To>::is_signed > ());
  86. }
  87. }
  88. template <class From, class To>
  89. void test_convert_neg_rat(From from, const boost::mpl::true_&)
  90. {
  91. from = -from;
  92. To t3(from);
  93. To t4 = from.template convert_to<To>();
  94. BOOST_CHECK_EQUAL(from.str(), numerator(t3).str());
  95. BOOST_CHECK_EQUAL(from.str(), numerator(t4).str());
  96. }
  97. template <class From, class To>
  98. void test_convert_neg_rat(From const&, const boost::mpl::false_&)
  99. {
  100. }
  101. template <class From, class To>
  102. void test_convert_imp(boost::mpl::int_<number_kind_integer> const&, boost::mpl::int_<number_kind_rational> const&)
  103. {
  104. int bits_wanted = (std::min)((std::min)(std::numeric_limits<From>::digits, std::numeric_limits<To>::digits), 2000);
  105. for (unsigned i = 0; i < 100; ++i)
  106. {
  107. From from = generate_random<From>(bits_wanted);
  108. To t1(from);
  109. To t2 = from.template convert_to<To>();
  110. BOOST_CHECK_EQUAL(from.str(), numerator(t1).str());
  111. BOOST_CHECK_EQUAL(from.str(), numerator(t2).str());
  112. test_convert_neg_rat<From, To>(from, boost::mpl::bool_ < std::numeric_limits<From>::is_signed && std::numeric_limits<To>::is_signed > ());
  113. }
  114. }
  115. template <class From, class To>
  116. void test_convert_neg_float(From from, const boost::mpl::true_&)
  117. {
  118. from = -from;
  119. To t3(from);
  120. To t4 = from.template convert_to<To>();
  121. To check(from.str() + ".0");
  122. BOOST_CHECK_EQUAL(t3, check);
  123. BOOST_CHECK_EQUAL(t4, check);
  124. }
  125. template <class From, class To>
  126. void test_convert_neg_float(From const&, const boost::mpl::false_&)
  127. {
  128. }
  129. template <class From, class To>
  130. void test_convert_imp(boost::mpl::int_<number_kind_integer> const&, boost::mpl::int_<number_kind_floating_point> const&)
  131. {
  132. int bits_wanted = (std::min)((std::min)(std::numeric_limits<From>::digits, std::numeric_limits<To>::digits), 2000);
  133. for (unsigned i = 0; i < 100; ++i)
  134. {
  135. From from = generate_random<From>(bits_wanted);
  136. To t1(from);
  137. To t2 = from.template convert_to<To>();
  138. To check(from.str() + ".0");
  139. BOOST_CHECK_EQUAL(t1, check);
  140. BOOST_CHECK_EQUAL(t2, check);
  141. test_convert_neg_float<From, To>(from, boost::mpl::bool_ < std::numeric_limits<From>::is_signed && std::numeric_limits<To>::is_signed > ());
  142. }
  143. }
  144. template <class From, class To>
  145. void test_convert()
  146. {
  147. test_convert_imp<From, To>(typename number_category<From>::type(), typename number_category<To>::type());
  148. }
  149. int main()
  150. {
  151. test_convert<mpz_int, cpp_int>();
  152. test_convert<mpz_int, int128_t>();
  153. test_convert<mpz_int, uint128_t>();
  154. test_convert<mpz_int, cpp_rational>();
  155. test_convert<mpz_int, cpp_dec_float_50>();
  156. test_convert<mpz_int, cpp_bin_float_50>();
  157. test_convert<mpz_int, mpq_rational>();
  158. test_convert<mpz_int, mpf_float_50>();
  159. #if defined(HAS_MPFR)
  160. test_convert<mpz_int, mpfr_float_50>();
  161. #endif
  162. #if defined(HAS_MPFI)
  163. test_convert<mpz_int, mpfi_float_50>();
  164. #endif
  165. #ifdef HAS_TOMMATH
  166. test_convert<mpz_int, tom_int>();
  167. test_convert<mpz_int, tom_rational>();
  168. #endif
  169. #ifdef HAS_FLOAT128
  170. test_convert<mpz_int, float128>();
  171. #endif
  172. return boost::report_errors();
  173. }
  174. #else
  175. int main() { return 0; }
  176. #endif