test_cpp_bin_float_io.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. #include <boost/multiprecision/cpp_bin_float.hpp>
  10. #include <boost/random/mersenne_twister.hpp>
  11. #include <boost/random/uniform_int.hpp>
  12. #include <boost/chrono.hpp>
  13. #include "test.hpp"
  14. #include <boost/array.hpp>
  15. #include <iostream>
  16. #include <iomanip>
  17. #ifdef BOOST_MSVC
  18. #pragma warning(disable : 4127)
  19. #endif
  20. template <class Clock>
  21. struct stopwatch
  22. {
  23. typedef typename Clock::duration duration;
  24. stopwatch()
  25. {
  26. m_start = Clock::now();
  27. }
  28. duration elapsed()
  29. {
  30. return Clock::now() - m_start;
  31. }
  32. void reset()
  33. {
  34. m_start = Clock::now();
  35. }
  36. private:
  37. typename Clock::time_point m_start;
  38. };
  39. void print_flags(std::ios_base::fmtflags f)
  40. {
  41. std::cout << "Formatting flags were: ";
  42. if (f & std::ios_base::scientific)
  43. std::cout << "scientific ";
  44. if (f & std::ios_base::fixed)
  45. std::cout << "fixed ";
  46. if (f & std::ios_base::showpoint)
  47. std::cout << "showpoint ";
  48. if (f & std::ios_base::showpos)
  49. std::cout << "showpos ";
  50. std::cout << std::endl;
  51. }
  52. template <class T>
  53. void test()
  54. {
  55. typedef T mp_t;
  56. boost::array<std::ios_base::fmtflags, 9> f =
  57. {{std::ios_base::fmtflags(0), std::ios_base::showpoint, std::ios_base::showpos, std::ios_base::scientific, std::ios_base::scientific | std::ios_base::showpos,
  58. std::ios_base::scientific | std::ios_base::showpoint, std::ios_base::fixed, std::ios_base::fixed | std::ios_base::showpoint,
  59. std::ios_base::fixed | std::ios_base::showpos}};
  60. boost::array<boost::array<const char*, 13 * 9>, 40> string_data = {{
  61. #include "libs/multiprecision/test/string_data.ipp"
  62. }};
  63. double num = 123456789.0;
  64. double denom = 1;
  65. double val = num;
  66. for (unsigned j = 0; j < 40; ++j)
  67. {
  68. unsigned col = 0;
  69. for (unsigned prec = 1; prec < 14; ++prec)
  70. {
  71. for (unsigned i = 0; i < f.size(); ++i, ++col)
  72. {
  73. std::stringstream ss;
  74. ss.precision(prec);
  75. ss.flags(f[i]);
  76. ss << mp_t(val);
  77. const char* expect = string_data[j][col];
  78. if (ss.str() != expect)
  79. {
  80. std::cout << std::setprecision(20) << "Testing value " << val << std::endl;
  81. print_flags(f[i]);
  82. std::cout << "Precision is: " << prec << std::endl;
  83. std::cout << "Got: " << ss.str() << std::endl;
  84. std::cout << "Expected: " << expect << std::endl;
  85. ++boost::detail::test_errors();
  86. mp_t(val).str(prec, f[i]); // for debugging
  87. }
  88. }
  89. }
  90. num = -num;
  91. if (j & 1)
  92. denom *= 8;
  93. val = num / denom;
  94. }
  95. boost::array<const char*, 13 * 9> zeros =
  96. {{"0", "0.", "+0", "0.0e+00", "+0.0e+00", "0.0e+00", "0.0", "0.0", "+0.0", "0", "0.0", "+0", "0.00e+00", "+0.00e+00", "0.00e+00", "0.00", "0.00", "+0.00", "0", "0.00", "+0", "0.000e+00", "+0.000e+00", "0.000e+00", "0.000", "0.000", "+0.000", "0", "0.000", "+0", "0.0000e+00", "+0.0000e+00", "0.0000e+00", "0.0000", "0.0000", "+0.0000", "0", "0.0000", "+0", "0.00000e+00", "+0.00000e+00", "0.00000e+00", "0.00000", "0.00000", "+0.00000", "0", "0.00000", "+0", "0.000000e+00", "+0.000000e+00", "0.000000e+00", "0.000000", "0.000000", "+0.000000", "0", "0.000000", "+0", "0.0000000e+00", "+0.0000000e+00", "0.0000000e+00", "0.0000000", "0.0000000", "+0.0000000", "0", "0.0000000", "+0", "0.00000000e+00", "+0.00000000e+00", "0.00000000e+00", "0.00000000", "0.00000000", "+0.00000000", "0", "0.00000000", "+0", "0.000000000e+00", "+0.000000000e+00", "0.000000000e+00", "0.000000000", "0.000000000", "+0.000000000", "0", "0.000000000", "+0", "0.0000000000e+00", "+0.0000000000e+00", "0.0000000000e+00", "0.0000000000", "0.0000000000", "+0.0000000000", "0", "0.0000000000", "+0", "0.00000000000e+00", "+0.00000000000e+00", "0.00000000000e+00", "0.00000000000", "0.00000000000", "+0.00000000000", "0", "0.00000000000", "+0", "0.000000000000e+00", "+0.000000000000e+00", "0.000000000000e+00", "0.000000000000", "0.000000000000", "+0.000000000000", "0", "0.000000000000", "+0", "0.0000000000000e+00", "+0.0000000000000e+00", "0.0000000000000e+00", "0.0000000000000", "0.0000000000000", "+0.0000000000000"}};
  97. unsigned col = 0;
  98. val = 0;
  99. for (unsigned prec = 1; prec < 14; ++prec)
  100. {
  101. for (unsigned i = 0; i < f.size(); ++i, ++col)
  102. {
  103. std::stringstream ss;
  104. ss.precision(prec);
  105. ss.flags(f[i]);
  106. ss << mp_t(val);
  107. const char* expect = zeros[col];
  108. if (ss.str() != expect)
  109. {
  110. std::cout << std::setprecision(20) << "Testing value " << val << std::endl;
  111. print_flags(f[i]);
  112. std::cout << "Precision is: " << prec << std::endl;
  113. std::cout << "Got: " << ss.str() << std::endl;
  114. std::cout << "Expected: " << expect << std::endl;
  115. ++boost::detail::test_errors();
  116. mp_t(val).str(prec, f[i]); // for debugging
  117. }
  118. }
  119. }
  120. if (std::numeric_limits<mp_t>::has_infinity)
  121. {
  122. T val = std::numeric_limits<T>::infinity();
  123. BOOST_CHECK_EQUAL(val.str(), "inf");
  124. BOOST_CHECK_EQUAL(val.str(0, std::ios_base::showpos), "+inf");
  125. val = -val;
  126. BOOST_CHECK_EQUAL(val.str(), "-inf");
  127. BOOST_CHECK_EQUAL(val.str(0, std::ios_base::showpos), "-inf");
  128. val = static_cast<T>("inf");
  129. BOOST_CHECK_EQUAL(val, std::numeric_limits<T>::infinity());
  130. val = static_cast<T>("+inf");
  131. BOOST_CHECK_EQUAL(val, std::numeric_limits<T>::infinity());
  132. val = static_cast<T>("-inf");
  133. BOOST_CHECK_EQUAL(val, -std::numeric_limits<T>::infinity());
  134. }
  135. if (std::numeric_limits<mp_t>::has_quiet_NaN)
  136. {
  137. T val = std::numeric_limits<T>::quiet_NaN();
  138. BOOST_CHECK_EQUAL(val.str(), "nan");
  139. val = static_cast<T>("nan");
  140. BOOST_CHECK((boost::math::isnan)(val));
  141. }
  142. //
  143. // Min and max values:
  144. //
  145. T t((std::numeric_limits<T>::max)().str(std::numeric_limits<T>::max_digits10, std::ios_base::scientific));
  146. BOOST_CHECK_EQUAL(t, (std::numeric_limits<T>::max)());
  147. t = T((std::numeric_limits<T>::min)().str(std::numeric_limits<T>::max_digits10, std::ios_base::scientific));
  148. BOOST_CHECK_EQUAL(t, (std::numeric_limits<T>::min)());
  149. t = T((std::numeric_limits<T>::lowest)().str(std::numeric_limits<T>::max_digits10, std::ios_base::scientific));
  150. BOOST_CHECK_EQUAL(t, (std::numeric_limits<T>::lowest)());
  151. }
  152. template <class T>
  153. T generate_random()
  154. {
  155. typedef typename T::backend_type::exponent_type e_type;
  156. static boost::random::mt19937 gen;
  157. T val = gen();
  158. T prev_val = -1;
  159. while (val != prev_val)
  160. {
  161. val *= (gen.max)();
  162. prev_val = val;
  163. val += gen();
  164. }
  165. e_type e;
  166. val = frexp(val, &e);
  167. static boost::random::uniform_int_distribution<e_type> ui(0, std::numeric_limits<T>::max_exponent);
  168. return ldexp(val, ui(gen));
  169. }
  170. template <class T>
  171. void do_round_trip(const T& val, std::ios_base::fmtflags f)
  172. {
  173. std::stringstream ss;
  174. #ifndef BOOST_NO_CXX11_NUMERIC_LIMITS
  175. ss << std::setprecision(std::numeric_limits<T>::max_digits10);
  176. #else
  177. ss << std::setprecision(std::numeric_limits<T>::digits10 + 3);
  178. #endif
  179. ss.flags(f);
  180. ss << val;
  181. T new_val = static_cast<T>(ss.str());
  182. BOOST_CHECK_EQUAL(new_val, val);
  183. new_val = static_cast<T>(val.str(0, f));
  184. BOOST_CHECK_EQUAL(new_val, val);
  185. }
  186. template <class T>
  187. void do_round_trip(const T& val)
  188. {
  189. do_round_trip(val, std::ios_base::fmtflags(0));
  190. do_round_trip(val, std::ios_base::fmtflags(std::ios_base::scientific));
  191. if ((fabs(val) > 1) && (fabs(val) < 1e100))
  192. do_round_trip(val, std::ios_base::fmtflags(std::ios_base::fixed));
  193. static int error_count = 0;
  194. if (error_count != boost::detail::test_errors())
  195. {
  196. error_count = boost::detail::test_errors();
  197. std::cout << "Errors occured while testing value....";
  198. if (val.backend().sign())
  199. std::cout << "-";
  200. std::cout << boost::multiprecision::cpp_int(val.backend().bits()) << "e" << val.backend().exponent() << std::endl;
  201. }
  202. }
  203. template <class T>
  204. void test_round_trip()
  205. {
  206. std::cout << "Testing type " << typeid(T).name() << std::endl;
  207. std::cout << "digits = " << std::numeric_limits<T>::digits << std::endl;
  208. std::cout << "digits10 = " << std::numeric_limits<T>::digits10 << std::endl;
  209. std::cout << "max_digits10 = " << std::numeric_limits<T>::max_digits10 << std::endl;
  210. stopwatch<boost::chrono::high_resolution_clock> w;
  211. #ifndef CI_SUPPRESS_KNOWN_ISSUES
  212. while (boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count() < 200)
  213. #else
  214. while (boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count() < 50)
  215. #endif
  216. {
  217. T val = generate_random<T>();
  218. do_round_trip(val);
  219. do_round_trip(T(-val));
  220. do_round_trip(T(1 / val));
  221. do_round_trip(T(-1 / val));
  222. if (boost::detail::test_errors() > 200)
  223. break; // escape if there are too many errors.
  224. }
  225. std::cout << "Execution time = " << boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count() << "s" << std::endl;
  226. }
  227. #if !defined(TEST1) && !defined(TEST2)
  228. #define TEST1
  229. #define TEST2
  230. #endif
  231. int main()
  232. {
  233. using namespace boost::multiprecision;
  234. #ifdef TEST1
  235. test<number<cpp_bin_float<113, digit_base_2> > >();
  236. test_round_trip<number<cpp_bin_float<113, digit_base_2> > >();
  237. #endif
  238. #ifdef TEST2
  239. test<number<cpp_bin_float<53, digit_base_2> > >();
  240. test_round_trip<number<cpp_bin_float<53, digit_base_2> > >();
  241. #endif
  242. return boost::report_errors();
  243. }