test_convert_from_cpp_dec_float.cpp 5.6 KB

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