test_convert_from_cpp_rational.cpp 6.7 KB

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