test_convert_from_mpfr_float.cpp 5.4 KB

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