test_convert_from_mpf_float.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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/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. From tol = std::numeric_limits<From>::epsilon();
  85. BOOST_CHECK_CLOSE_FRACTION(From(t3), from, tol);
  86. BOOST_CHECK_CLOSE_FRACTION(From(t4), from, tol);
  87. }
  88. template <class From, class To>
  89. void test_convert_rat_int(From const&, const boost::mpl::false_&)
  90. {
  91. }
  92. template <class From, class To>
  93. void test_convert_imp(boost::mpl::int_<number_kind_floating_point> const&, boost::mpl::int_<number_kind_rational> const&)
  94. {
  95. for (unsigned i = 0; i < 100; ++i)
  96. {
  97. From from = generate_random<From>();
  98. To t1(from);
  99. To t2 = from.template convert_to<To>();
  100. From tol = std::numeric_limits<From>::epsilon();
  101. BOOST_CHECK_CLOSE_FRACTION(From(t1), from, tol);
  102. BOOST_CHECK_CLOSE_FRACTION(From(t2), from, tol);
  103. test_convert_neg_rat<From, To>(from, boost::mpl::bool_ < std::numeric_limits<From>::is_signed && std::numeric_limits<To>::is_signed > ());
  104. }
  105. }
  106. template <class From, class To>
  107. void test_convert_neg_float(From from, const boost::mpl::true_&)
  108. {
  109. from = -from;
  110. To t3(from);
  111. To t4 = from.template convert_to<To>();
  112. To answer(from.str());
  113. To tol = (std::max)(std::numeric_limits<To>::epsilon(), To(std::numeric_limits<From>::epsilon())) * 2;
  114. BOOST_CHECK_CLOSE_FRACTION(t3, answer, tol);
  115. BOOST_CHECK_CLOSE_FRACTION(t4, answer, tol);
  116. }
  117. template <class From, class To>
  118. void test_convert_neg_float(From const&, const boost::mpl::false_&)
  119. {
  120. }
  121. template <class From, class To>
  122. void test_convert_imp(boost::mpl::int_<number_kind_floating_point> const&, boost::mpl::int_<number_kind_floating_point> const&)
  123. {
  124. for (unsigned i = 0; i < 100; ++i)
  125. {
  126. From from = generate_random<From>();
  127. To t1(from);
  128. To t2 = from.template convert_to<To>();
  129. To answer(from.str());
  130. To tol = (std::max)(std::numeric_limits<To>::epsilon(), To(std::numeric_limits<From>::epsilon())) * 2;
  131. BOOST_CHECK_CLOSE_FRACTION(t1, answer, tol);
  132. BOOST_CHECK_CLOSE_FRACTION(t2, answer, tol);
  133. test_convert_neg_float<From, To>(from, boost::mpl::bool_ < std::numeric_limits<From>::is_signed && std::numeric_limits<To>::is_signed > ());
  134. }
  135. }
  136. template <class From, class To>
  137. void test_convert()
  138. {
  139. test_convert_imp<From, To>(typename number_category<From>::type(), typename number_category<To>::type());
  140. }
  141. int main()
  142. {
  143. test_convert<mpf_float_50, cpp_int>();
  144. test_convert<mpf_float_50, int128_t>();
  145. test_convert<mpf_float_50, uint128_t>();
  146. test_convert<mpf_float_50, cpp_rational>();
  147. test_convert<mpf_float_50, cpp_dec_float_50>();
  148. test_convert<mpf_float_50, mpz_int>();
  149. test_convert<mpf_float_50, mpq_rational>();
  150. #if defined(HAS_MPFR)
  151. test_convert<mpf_float_50, mpfr_float_50>();
  152. #endif
  153. #if defined(HAS_MPFI)
  154. test_convert<mpf_float_50, mpfi_float_50>();
  155. #endif
  156. #ifdef HAS_TOMMATH
  157. test_convert<mpf_float_50, tom_int>();
  158. test_convert<mpf_float_50, tom_rational>();
  159. #endif
  160. #ifdef HAS_FLOAT128
  161. test_convert<mpf_float_50, float128>();
  162. #endif
  163. return boost::report_errors();
  164. }
  165. #else
  166. int main() { return 0; }
  167. #endif