test_convert_from_gmp_rational.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 "test.hpp"
  12. #include <boost/multiprecision/gmp.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_int(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 T>
  62. T generate_random(unsigned bits_wanted)
  63. {
  64. typedef typename component_type<T>::type int_type;
  65. T val(generate_random_int<int_type>(bits_wanted), generate_random_int<int_type>(bits_wanted));
  66. return val;
  67. }
  68. template <class From, class To>
  69. void test_convert_neg_val(From from, const boost::mpl::true_&)
  70. {
  71. from = -from;
  72. typename component_type<From>::type answer = numerator(from) / denominator(from);
  73. To t3(from);
  74. To t4 = from.template convert_to<To>();
  75. BOOST_CHECK_EQUAL(answer.str(), t3.str());
  76. BOOST_CHECK_EQUAL(answer.str(), t4.str());
  77. }
  78. template <class From, class To>
  79. void test_convert_neg_val(From const&, const boost::mpl::false_&)
  80. {
  81. }
  82. template <class From, class To>
  83. void test_convert_imp(boost::mpl::int_<number_kind_rational> const&, boost::mpl::int_<number_kind_integer> const&)
  84. {
  85. int bits_wanted = (std::min)((std::min)(std::numeric_limits<From>::digits, std::numeric_limits<To>::digits), 2000);
  86. for (unsigned i = 0; i < 100; ++i)
  87. {
  88. From from = generate_random<From>(bits_wanted);
  89. typename component_type<From>::type answer = numerator(from) / denominator(from);
  90. To t1(from);
  91. To t2 = from.template convert_to<To>();
  92. BOOST_CHECK_EQUAL(answer.str(), t1.str());
  93. BOOST_CHECK_EQUAL(answer.str(), t2.str());
  94. test_convert_neg_val<From, To>(from, boost::mpl::bool_ < std::numeric_limits<From>::is_signed && std::numeric_limits<To>::is_signed > ());
  95. }
  96. }
  97. template <class From, class To>
  98. void test_convert_neg_float_val(From from, To const& tol, const boost::mpl::true_&)
  99. {
  100. from = -from;
  101. To answer = To(numerator(from)) / To(denominator(from));
  102. To t3(from);
  103. To t4 = from.template convert_to<To>();
  104. BOOST_CHECK_CLOSE_FRACTION(answer, t3, tol);
  105. BOOST_CHECK_CLOSE_FRACTION(answer, t4, tol);
  106. }
  107. template <class From, class To>
  108. void test_convert_neg_float_val(From const&, To const&, const boost::mpl::false_&)
  109. {
  110. }
  111. template <class From, class To>
  112. void test_convert_imp(boost::mpl::int_<number_kind_rational> const&, boost::mpl::int_<number_kind_floating_point> const&)
  113. {
  114. int bits_wanted = (std::min)((std::min)(std::numeric_limits<From>::digits, std::numeric_limits<To>::digits), 2000);
  115. for (unsigned i = 0; i < 100; ++i)
  116. {
  117. From from = generate_random<From>(bits_wanted);
  118. To answer = To(numerator(from)) / To(denominator(from));
  119. To t1(from);
  120. To t2 = from.template convert_to<To>();
  121. To tol = std::numeric_limits<To>::is_specialized ? std::numeric_limits<To>::epsilon() : ldexp(To(1), 1 - bits_wanted);
  122. tol *= 2;
  123. BOOST_CHECK_CLOSE_FRACTION(answer, t1, tol);
  124. BOOST_CHECK_CLOSE_FRACTION(answer, t2, tol);
  125. test_convert_neg_float_val<From, To>(from, tol, boost::mpl::bool_ < std::numeric_limits<From>::is_signed && std::numeric_limits<To>::is_signed > ());
  126. }
  127. }
  128. template <class From, class To>
  129. void test_convert_neg_rat_val(From from, const boost::mpl::true_&)
  130. {
  131. from = -from;
  132. To t3(from);
  133. To t4 = from.template convert_to<To>();
  134. BOOST_CHECK_EQUAL(from.str(), t3.str());
  135. BOOST_CHECK_EQUAL(from.str(), t4.str());
  136. }
  137. template <class From, class To>
  138. void test_convert_neg_rat_val(From const&, const boost::mpl::false_&)
  139. {
  140. }
  141. template <class From, class To>
  142. void test_convert_imp(boost::mpl::int_<number_kind_rational> const&, boost::mpl::int_<number_kind_rational> const&)
  143. {
  144. int bits_wanted = (std::min)((std::min)(std::numeric_limits<From>::digits, std::numeric_limits<To>::digits), 2000);
  145. for (unsigned i = 0; i < 100; ++i)
  146. {
  147. From from = generate_random<From>(bits_wanted);
  148. To t1(from);
  149. To t2 = from.template convert_to<To>();
  150. BOOST_CHECK_EQUAL(from.str(), t1.str());
  151. BOOST_CHECK_EQUAL(from.str(), t2.str());
  152. test_convert_neg_rat_val<From, To>(from, boost::mpl::bool_ < std::numeric_limits<From>::is_signed && std::numeric_limits<To>::is_signed > ());
  153. }
  154. }
  155. template <class From, class To>
  156. void test_convert()
  157. {
  158. test_convert_imp<From, To>(typename number_category<From>::type(), typename number_category<To>::type());
  159. }
  160. int main()
  161. {
  162. test_convert<mpq_rational, cpp_int>();
  163. test_convert<mpq_rational, int128_t>();
  164. test_convert<mpq_rational, uint128_t>();
  165. test_convert<mpq_rational, cpp_rational>();
  166. test_convert<mpq_rational, cpp_bin_float_50>();
  167. test_convert<mpq_rational, cpp_dec_float_50>();
  168. test_convert<mpq_rational, mpz_int>();
  169. test_convert<mpq_rational, mpf_float_50>();
  170. #if defined(HAS_MPFR)
  171. test_convert<mpq_rational, mpfr_float_50>();
  172. #endif
  173. #if defined(HAS_MPFI)
  174. test_convert<mpq_rational, mpfi_float_50>();
  175. #endif
  176. #ifdef HAS_TOMMATH
  177. test_convert<mpq_rational, tom_int>();
  178. test_convert<mpq_rational, tom_rational>();
  179. #endif
  180. return boost::report_errors();
  181. }
  182. #else
  183. int main() { return 0; }
  184. #endif