test_float_io.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. // Copyright John Maddock 2011.
  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(TEST_MPF_50) && !defined(TEST_CPP_DEC_FLOAT) && !defined(TEST_MPFR_50) && !defined(TEST_MPFI_50) && !defined(TEST_FLOAT128)
  10. #define TEST_MPF_50
  11. #define TEST_CPP_DEC_FLOAT
  12. #define TEST_MPFR_50
  13. #define TEST_MPFI_50
  14. #define TEST_FLOAT128
  15. #ifdef _MSC_VER
  16. #pragma message("CAUTION!!: No backend type specified so testing everything.... this will take some time!!")
  17. #endif
  18. #ifdef __GNUC__
  19. #pragma warning "CAUTION!!: No backend type specified so testing everything.... this will take some time!!"
  20. #endif
  21. #endif
  22. #if defined(TEST_MPF_50)
  23. #include <boost/multiprecision/gmp.hpp>
  24. #endif
  25. #if defined(TEST_MPFR_50)
  26. #include <boost/multiprecision/mpfr.hpp>
  27. #endif
  28. #if defined(TEST_MPFI_50)
  29. #include <boost/multiprecision/mpfi.hpp>
  30. #endif
  31. #ifdef TEST_CPP_DEC_FLOAT
  32. #include <boost/multiprecision/cpp_dec_float.hpp>
  33. #endif
  34. #ifdef TEST_FLOAT128
  35. #include <boost/multiprecision/float128.hpp>
  36. #endif
  37. #include <boost/random/mersenne_twister.hpp>
  38. #include <boost/random/uniform_int.hpp>
  39. #include "test.hpp"
  40. #include <boost/array.hpp>
  41. #include <iostream>
  42. #include <iomanip>
  43. #ifdef BOOST_MSVC
  44. #pragma warning(disable : 4127)
  45. #endif
  46. #if defined(TEST_MPF_50)
  47. template <unsigned N, boost::multiprecision::expression_template_option ET>
  48. bool has_bad_bankers_rounding(const boost::multiprecision::number<boost::multiprecision::gmp_float<N>, ET>&)
  49. {
  50. return true;
  51. }
  52. #endif
  53. #if defined(TEST_FLOAT128) && defined(BOOST_INTEL)
  54. bool has_bad_bankers_rounding(const boost::multiprecision::float128&)
  55. {
  56. return true;
  57. }
  58. #endif
  59. template <class T>
  60. bool has_bad_bankers_rounding(const T&)
  61. {
  62. return false;
  63. }
  64. bool is_bankers_rounding_error(const std::string& s, const char* expect)
  65. {
  66. // This check isn't foolproof: that would require *much* more sophisticated code!!!
  67. std::string::size_type l = std::strlen(expect);
  68. if (l != s.size())
  69. return false;
  70. std::string::size_type len = s.find('e');
  71. if (len == std::string::npos)
  72. len = l - 1;
  73. else
  74. --len;
  75. if (s.compare(0, len, expect, len))
  76. return false;
  77. if (s[len] != expect[len] + 1)
  78. return false;
  79. return true;
  80. }
  81. void print_flags(std::ios_base::fmtflags f)
  82. {
  83. std::cout << "Formatting flags were: ";
  84. if (f & std::ios_base::scientific)
  85. std::cout << "scientific ";
  86. if (f & std::ios_base::fixed)
  87. std::cout << "fixed ";
  88. if (f & std::ios_base::showpoint)
  89. std::cout << "showpoint ";
  90. if (f & std::ios_base::showpos)
  91. std::cout << "showpos ";
  92. std::cout << std::endl;
  93. }
  94. template <class T>
  95. void test()
  96. {
  97. typedef T mp_t;
  98. boost::array<std::ios_base::fmtflags, 9> f =
  99. {{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,
  100. std::ios_base::scientific | std::ios_base::showpoint, std::ios_base::fixed, std::ios_base::fixed | std::ios_base::showpoint,
  101. std::ios_base::fixed | std::ios_base::showpos}};
  102. boost::array<boost::array<const char*, 13 * 9>, 40> string_data = {{
  103. #include "libs/multiprecision/test/string_data.ipp"
  104. }};
  105. double num = 123456789.0;
  106. double denom = 1;
  107. double val = num;
  108. for (unsigned j = 0; j < 40; ++j)
  109. {
  110. unsigned col = 0;
  111. for (unsigned prec = 1; prec < 14; ++prec)
  112. {
  113. for (unsigned i = 0; i < f.size(); ++i, ++col)
  114. {
  115. std::stringstream ss;
  116. ss.precision(prec);
  117. ss.flags(f[i]);
  118. ss << mp_t(val);
  119. const char* expect = string_data[j][col];
  120. if (ss.str() != expect)
  121. {
  122. if (has_bad_bankers_rounding(mp_t()) && is_bankers_rounding_error(ss.str(), expect))
  123. {
  124. std::cout << "Ignoring bankers-rounding error with GMP mp_f.\n";
  125. }
  126. else
  127. {
  128. std::cout << std::setprecision(20) << "Testing value " << val << std::endl;
  129. print_flags(f[i]);
  130. std::cout << "Precision is: " << prec << std::endl;
  131. std::cout << "Got: " << ss.str() << std::endl;
  132. std::cout << "Expected: " << expect << std::endl;
  133. ++boost::detail::test_errors();
  134. mp_t(val).str(prec, f[i]); // for debugging
  135. }
  136. }
  137. }
  138. }
  139. num = -num;
  140. if (j & 1)
  141. denom *= 8;
  142. val = num / denom;
  143. }
  144. boost::array<const char*, 13 * 9> zeros =
  145. {{"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"}};
  146. unsigned col = 0;
  147. val = 0;
  148. for (unsigned prec = 1; prec < 14; ++prec)
  149. {
  150. for (unsigned i = 0; i < f.size(); ++i, ++col)
  151. {
  152. std::stringstream ss;
  153. ss.precision(prec);
  154. ss.flags(f[i]);
  155. ss << mp_t(val);
  156. const char* expect = zeros[col];
  157. if (ss.str() != expect)
  158. {
  159. std::cout << std::setprecision(20) << "Testing value " << val << std::endl;
  160. print_flags(f[i]);
  161. std::cout << "Precision is: " << prec << std::endl;
  162. std::cout << "Got: " << ss.str() << std::endl;
  163. std::cout << "Expected: " << expect << std::endl;
  164. ++boost::detail::test_errors();
  165. mp_t(val).str(prec, f[i]); // for debugging
  166. }
  167. }
  168. }
  169. if (std::numeric_limits<mp_t>::has_infinity)
  170. {
  171. T val = std::numeric_limits<T>::infinity();
  172. BOOST_CHECK_EQUAL(val.str(), "inf");
  173. BOOST_CHECK_EQUAL(val.str(0, std::ios_base::showpos), "+inf");
  174. val = -val;
  175. BOOST_CHECK_EQUAL(val.str(), "-inf");
  176. BOOST_CHECK_EQUAL(val.str(0, std::ios_base::showpos), "-inf");
  177. val = static_cast<T>("inf");
  178. BOOST_CHECK_EQUAL(val, std::numeric_limits<T>::infinity());
  179. val = static_cast<T>("+inf");
  180. BOOST_CHECK_EQUAL(val, std::numeric_limits<T>::infinity());
  181. val = static_cast<T>("-inf");
  182. BOOST_CHECK_EQUAL(val, -std::numeric_limits<T>::infinity());
  183. }
  184. if (std::numeric_limits<mp_t>::has_quiet_NaN)
  185. {
  186. T val = std::numeric_limits<T>::quiet_NaN();
  187. BOOST_CHECK_EQUAL(val.str(), "nan");
  188. val = static_cast<T>("nan");
  189. BOOST_CHECK((boost::math::isnan)(val));
  190. }
  191. //
  192. // Bug cases:
  193. //
  194. // https://github.com/boostorg/multiprecision/issues/113
  195. //
  196. {
  197. T val("99.9809");
  198. BOOST_CHECK_EQUAL(val.str(3, std::ios_base::fixed), "99.981");
  199. }
  200. }
  201. template <class T>
  202. T generate_random()
  203. {
  204. typedef typename T::backend_type::exponent_type e_type;
  205. static boost::random::mt19937 gen;
  206. T val = gen();
  207. T prev_val = -1;
  208. while (val != prev_val)
  209. {
  210. val *= (gen.max)();
  211. prev_val = val;
  212. val += gen();
  213. }
  214. e_type e;
  215. val = frexp(val, &e);
  216. static boost::random::uniform_int_distribution<e_type> ui(0, std::numeric_limits<T>::max_exponent - 10);
  217. return ldexp(val, ui(gen));
  218. }
  219. template <class T>
  220. struct max_digits10_proxy
  221. {
  222. static const unsigned value = std::numeric_limits<T>::digits10 + 5;
  223. };
  224. #ifdef TEST_CPP_DEC_FLOAT
  225. template <unsigned D, boost::multiprecision::expression_template_option ET>
  226. struct max_digits10_proxy<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<D>, ET> >
  227. {
  228. static const unsigned value = std::numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<D>, ET> >::max_digits10;
  229. };
  230. #endif
  231. #ifdef TEST_MPF_50
  232. template <unsigned D, boost::multiprecision::expression_template_option ET>
  233. struct max_digits10_proxy<boost::multiprecision::number<boost::multiprecision::gmp_float<D>, ET> >
  234. {
  235. static const unsigned value = std::numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<D>, ET> >::max_digits10;
  236. };
  237. #endif
  238. #ifdef TEST_MPFR_50
  239. template <unsigned D, boost::multiprecision::expression_template_option ET>
  240. struct max_digits10_proxy<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<D>, ET> >
  241. {
  242. static const unsigned value = std::numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<D>, ET> >::max_digits10;
  243. };
  244. #endif
  245. template <class T>
  246. void do_round_trip(const T& val, std::ios_base::fmtflags f)
  247. {
  248. std::stringstream ss;
  249. #ifndef BOOST_NO_CXX11_NUMERIC_LIMITS
  250. ss << std::setprecision(std::numeric_limits<T>::max_digits10);
  251. #else
  252. ss << std::setprecision(max_digits10_proxy<T>::value);
  253. #endif
  254. ss.flags(f);
  255. ss << val;
  256. T new_val = static_cast<T>(ss.str());
  257. BOOST_CHECK_EQUAL(new_val, val);
  258. new_val = static_cast<T>(val.str(0, f));
  259. BOOST_CHECK_EQUAL(new_val, val);
  260. }
  261. template <class T>
  262. void do_round_trip(const T& val)
  263. {
  264. do_round_trip(val, std::ios_base::fmtflags(0));
  265. do_round_trip(val, std::ios_base::fmtflags(std::ios_base::scientific));
  266. if ((fabs(val) > 1) && (fabs(val) < 1e100))
  267. do_round_trip(val, std::ios_base::fmtflags(std::ios_base::fixed));
  268. }
  269. template <class T>
  270. void test_round_trip()
  271. {
  272. for (unsigned i = 0; i < 1000; ++i)
  273. {
  274. T val = generate_random<T>();
  275. do_round_trip(val);
  276. do_round_trip(T(-val));
  277. do_round_trip(T(1 / val));
  278. do_round_trip(T(-1 / val));
  279. }
  280. }
  281. #ifdef TEST_FLOAT128
  282. void test_hexadecimal_floating_point()
  283. {
  284. using boost::multiprecision::float128;
  285. float128 x = 0x1p+0Q;
  286. std::string s = x.str(0, std::ios_base::fmtflags(std::ios_base::fixed | std::ios_base::scientific));
  287. BOOST_CHECK_EQUAL(s, "0x1p+0");
  288. s = x.str(0, std::ios_base::fmtflags(std::ios_base::floatfield));
  289. BOOST_CHECK_EQUAL(s, "0x1p+0");
  290. x = -1;
  291. s = x.str(0, std::ios_base::fmtflags(std::ios_base::floatfield));
  292. BOOST_CHECK_EQUAL(s, "-0x1p+0");
  293. // hexadecimal representation of pi; test a round trip:
  294. float128 pi1 = 0x1.921fb54442d18469898cc51701b8p+1Q;
  295. s = pi1.str(0, std::ios_base::fmtflags(std::ios_base::floatfield));
  296. float128 pi2(s);
  297. BOOST_CHECK_EQUAL(pi1, pi2);
  298. }
  299. #endif
  300. int main()
  301. {
  302. #ifdef TEST_MPFR_50
  303. test<boost::multiprecision::mpfr_float_50>();
  304. test<boost::multiprecision::mpfr_float_100>();
  305. test_round_trip<boost::multiprecision::mpfr_float_50>();
  306. test_round_trip<boost::multiprecision::mpfr_float_100>();
  307. #endif
  308. #ifdef TEST_MPFI_50
  309. test<boost::multiprecision::mpfr_float_50>();
  310. test<boost::multiprecision::mpfr_float_100>();
  311. test_round_trip<boost::multiprecision::mpfr_float_50>();
  312. test_round_trip<boost::multiprecision::mpfr_float_100>();
  313. #endif
  314. #ifdef TEST_CPP_DEC_FLOAT
  315. test<boost::multiprecision::cpp_dec_float_50>();
  316. test<boost::multiprecision::cpp_dec_float_100>();
  317. // cpp_dec_float has extra guard digits that messes this up:
  318. test_round_trip<boost::multiprecision::cpp_dec_float_50>();
  319. test_round_trip<boost::multiprecision::cpp_dec_float_100>();
  320. #endif
  321. #ifdef TEST_MPF_50
  322. test<boost::multiprecision::mpf_float_50>();
  323. test<boost::multiprecision::mpf_float_100>();
  324. /*
  325. // I can't get this to work with mpf_t - mpf_str appears
  326. // not to actually print enough decimal digits:
  327. test_round_trip<boost::multiprecision::mpf_float_50>();
  328. test_round_trip<boost::multiprecision::mpf_float_100>();
  329. */
  330. #endif
  331. #ifdef TEST_FLOAT128
  332. test<boost::multiprecision::float128>();
  333. test_hexadecimal_floating_point();
  334. #ifndef BOOST_INTEL
  335. test_round_trip<boost::multiprecision::float128>();
  336. #endif
  337. #endif
  338. return boost::report_errors();
  339. }