test_rat_float_interconv.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // Copyright John Maddock 2013.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifdef _MSC_VER
  7. #define _SCL_SECURE_NO_WARNINGS
  8. #endif
  9. #if defined(TEST1) || defined(TEST2) || defined(TEST3) || defined(TEST4)
  10. #include <boost/multiprecision/cpp_bin_float.hpp>
  11. #include <boost/multiprecision/cpp_int.hpp>
  12. #else
  13. #include <boost/multiprecision/mpfr.hpp>
  14. #endif
  15. #include <boost/math/special_functions/next.hpp>
  16. #include <boost/random/mersenne_twister.hpp>
  17. #include <boost/random/uniform_int.hpp>
  18. #include <boost/chrono.hpp>
  19. #include "test.hpp"
  20. #include <boost/array.hpp>
  21. #include <iostream>
  22. #include <iomanip>
  23. #ifdef BOOST_MSVC
  24. #pragma warning(disable : 4127)
  25. #endif
  26. template <class Clock>
  27. struct stopwatch
  28. {
  29. typedef typename Clock::duration duration;
  30. stopwatch()
  31. {
  32. m_start = Clock::now();
  33. }
  34. duration elapsed()
  35. {
  36. return Clock::now() - m_start;
  37. }
  38. void reset()
  39. {
  40. m_start = Clock::now();
  41. }
  42. private:
  43. typename Clock::time_point m_start;
  44. };
  45. template <class T>
  46. struct exponent_type
  47. {
  48. typedef int type;
  49. };
  50. template <class T, boost::multiprecision::expression_template_option ET>
  51. struct exponent_type<boost::multiprecision::number<T, ET> >
  52. {
  53. typedef typename T::exponent_type type;
  54. };
  55. template <class T>
  56. T generate_random_float()
  57. {
  58. BOOST_MATH_STD_USING
  59. typedef typename exponent_type<T>::type e_type;
  60. static boost::random::mt19937 gen;
  61. T val = gen();
  62. T prev_val = -1;
  63. while (val != prev_val)
  64. {
  65. val *= (gen.max)();
  66. prev_val = val;
  67. val += gen();
  68. }
  69. e_type e;
  70. val = frexp(val, &e);
  71. static const int max_exponent_value = (std::min)(static_cast<int>(std::numeric_limits<T>::max_exponent - std::numeric_limits<T>::digits - 20), 2000);
  72. static boost::random::uniform_int_distribution<e_type> ui(0, max_exponent_value);
  73. return ldexp(val, ui(gen));
  74. }
  75. template <class Float, class Rat>
  76. void do_round_trip(const Float& val)
  77. {
  78. #ifndef BOOST_MP_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
  79. BOOST_MATH_STD_USING
  80. Rat rat(val);
  81. Float new_f(rat);
  82. BOOST_CHECK_EQUAL(val, new_f);
  83. //
  84. // Try adding or subtracting an insignificant amount
  85. // (0.25ulp) from rat and check that it rounds to the same value:
  86. //
  87. typename exponent_type<Float>::type e;
  88. Float t = frexp(val, &e);
  89. (void)t; // warning suppression
  90. e -= std::numeric_limits<Float>::digits + 2;
  91. BOOST_ASSERT(val == (val + ldexp(Float(1), e)));
  92. Rat delta, rounded;
  93. typedef typename boost::multiprecision::component_type<Rat>::type i_type;
  94. i_type i(1);
  95. i <<= (e < 0 ? -e : e);
  96. if (e > 0)
  97. delta.assign(i);
  98. else
  99. delta = Rat(i_type(1), i);
  100. rounded = rat + delta;
  101. new_f = static_cast<Float>(rounded);
  102. BOOST_CHECK_EQUAL(val, new_f);
  103. rounded = rat - delta;
  104. new_f = static_cast<Float>(rounded);
  105. BOOST_CHECK_EQUAL(val, new_f);
  106. delta /= 2;
  107. rounded = rat + delta;
  108. new_f = static_cast<Float>(rounded);
  109. BOOST_CHECK_EQUAL(val, new_f);
  110. rounded = rat - delta;
  111. new_f = static_cast<Float>(rounded);
  112. BOOST_CHECK_EQUAL(val, new_f);
  113. delta /= 2;
  114. rounded = rat + delta;
  115. new_f = static_cast<Float>(rounded);
  116. BOOST_CHECK_EQUAL(val, new_f);
  117. rounded = rat - delta;
  118. new_f = static_cast<Float>(rounded);
  119. BOOST_CHECK_EQUAL(val, new_f);
  120. #endif
  121. }
  122. template <class Float, class Rat>
  123. void test_round_trip()
  124. {
  125. #ifndef BOOST_MP_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
  126. std::cout << "Testing types " << typeid(Float).name() << " <<==>> " << typeid(Rat).name() << std::endl;
  127. std::cout << "digits = " << std::numeric_limits<Float>::digits << std::endl;
  128. std::cout << "digits10 = " << std::numeric_limits<Float>::digits10 << std::endl;
  129. #ifndef BOOST_NO_CXX11_NUMERIC_LIMITS
  130. std::cout << "max_digits10 = " << std::numeric_limits<Float>::max_digits10 << std::endl;
  131. #endif
  132. stopwatch<boost::chrono::high_resolution_clock> w;
  133. int count = 0;
  134. #ifndef CI_SUPPRESS_KNOWN_ISSUES
  135. while (boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count() < 200)
  136. #else
  137. while (boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count() < 50)
  138. #endif
  139. {
  140. Float val = generate_random_float<Float>();
  141. do_round_trip<Float, Rat>(val);
  142. do_round_trip<Float, Rat>(Float(-val));
  143. do_round_trip<Float, Rat>(Float(1 / val));
  144. do_round_trip<Float, Rat>(Float(-1 / val));
  145. count += 4;
  146. if (boost::detail::test_errors() > 100)
  147. break;
  148. }
  149. std::cout << "Execution time = " << boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count() << "s" << std::endl;
  150. std::cout << "Total values tested: " << count << std::endl;
  151. #endif
  152. }
  153. template <class Int>
  154. Int generate_random_int()
  155. {
  156. static boost::random::mt19937 gen;
  157. static boost::random::uniform_int_distribution<boost::random::mt19937::result_type> d(1, 20);
  158. int lim;
  159. Int cppi(0);
  160. lim = d(gen);
  161. for (int i = 0; i < lim; ++i)
  162. {
  163. cppi *= (gen.max)();
  164. cppi += gen();
  165. }
  166. return cppi;
  167. }
  168. template <class Float, class Rat>
  169. void test_random_rationals()
  170. {
  171. #ifndef BOOST_MP_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
  172. std::cout << "Testing types " << typeid(Float).name() << " <<==>> " << typeid(Rat).name() << std::endl;
  173. std::cout << "digits = " << std::numeric_limits<Float>::digits << std::endl;
  174. std::cout << "digits10 = " << std::numeric_limits<Float>::digits10 << std::endl;
  175. #ifndef BOOST_NO_CXX11_NUMERIC_LIMITS
  176. std::cout << "max_digits10 = " << std::numeric_limits<Float>::max_digits10 << std::endl;
  177. #endif
  178. typedef typename boost::multiprecision::component_type<Rat>::type i_type;
  179. stopwatch<boost::chrono::high_resolution_clock> w;
  180. int count = 0;
  181. #ifndef CI_SUPPRESS_KNOWN_ISSUES
  182. while (boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count() < 200)
  183. #else
  184. while (boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count() < 50)
  185. #endif
  186. {
  187. Rat rat(generate_random_int<i_type>(), generate_random_int<i_type>());
  188. Float f(rat);
  189. Rat new_rat(f); // rounded value
  190. int c = new_rat.compare(rat);
  191. if (c < 0)
  192. {
  193. // If f was rounded down, next float up must be above the original value:
  194. f = boost::math::float_next(f);
  195. new_rat.assign(f);
  196. BOOST_CHECK(new_rat >= rat);
  197. }
  198. else if (c > 0)
  199. {
  200. // If f was rounded up, next float down must be below the original value:
  201. f = boost::math::float_prior(f);
  202. new_rat.assign(f);
  203. BOOST_CHECK(new_rat <= rat);
  204. }
  205. else
  206. {
  207. // Values were equal... nothing to test.
  208. }
  209. if (boost::detail::test_errors() > 100)
  210. break;
  211. }
  212. std::cout << "Execution time = " << boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count() << "s" << std::endl;
  213. std::cout << "Total values tested: " << count << std::endl;
  214. #endif
  215. }
  216. #if defined(TEST2)
  217. void double_spot_tests()
  218. {
  219. boost::multiprecision::cpp_rational rat = 1;
  220. boost::multiprecision::cpp_rational twiddle(boost::multiprecision::cpp_int(1), boost::multiprecision::cpp_int(boost::multiprecision::cpp_int(1) << 54));
  221. rat += boost::multiprecision::cpp_rational(boost::multiprecision::cpp_int(1), boost::multiprecision::cpp_int(boost::multiprecision::cpp_int(1) << 50));
  222. double d = rat.convert_to<double>();
  223. rat += twiddle;
  224. BOOST_CHECK_EQUAL(d, rat.convert_to<double>());
  225. rat += twiddle;
  226. // tie: round to even rounds down
  227. BOOST_CHECK_EQUAL(d, rat.convert_to<double>());
  228. rat += twiddle;
  229. BOOST_CHECK_NE(d, rat.convert_to<double>());
  230. rat -= twiddle;
  231. BOOST_CHECK_EQUAL(d, rat.convert_to<double>());
  232. rat += boost::multiprecision::cpp_rational(boost::multiprecision::cpp_int(1), boost::multiprecision::cpp_int(boost::multiprecision::cpp_int(1) << 52));
  233. // tie, but last bit is now a 1 so we round up:
  234. BOOST_CHECK_NE(d, rat.convert_to<double>());
  235. }
  236. #endif
  237. int main()
  238. {
  239. using namespace boost::multiprecision;
  240. #if defined(TEST1) && !defined(BOOST_MSVC)
  241. test_round_trip<number<cpp_bin_float<113, digit_base_2, void, boost::int16_t> >, cpp_rational>();
  242. #elif defined(TEST2)
  243. double_spot_tests();
  244. test_round_trip<double, cpp_rational>();
  245. #elif defined(TEST3) && !defined(BOOST_MSVC)
  246. test_random_rationals<number<cpp_bin_float<113, digit_base_2, void, boost::int16_t> >, cpp_rational>();
  247. #elif defined(TEST4)
  248. test_random_rationals<double, cpp_rational>();
  249. #elif defined(TEST5)
  250. // This does not work: gmp does not correctly round integer to float or
  251. // rational to float conversions:
  252. test_round_trip<double, mpq_rational>();
  253. #elif defined(TEST6)
  254. test_round_trip<mpfr_float_100, mpq_rational>();
  255. #elif defined(TEST7)
  256. test_random_rationals<mpfr_float_100, mpq_rational>();
  257. #elif defined(TEST8)
  258. test_random_rationals<double, mpq_rational>();
  259. #endif
  260. return boost::report_errors();
  261. }