issue_13301.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright 2016 John Maddock. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #include <boost/multiprecision/cpp_bin_float.hpp>
  6. int main()
  7. {
  8. typedef boost::multiprecision::number<boost::multiprecision::cpp_bin_float<8, boost::multiprecision::backends::digit_base_2> > quarter_float;
  9. quarter_float qf(256);
  10. unsigned int ui = qf.convert_to<unsigned int>();
  11. if (ui != 256)
  12. return 1;
  13. boost::uintmax_t m(1), n;
  14. m <<= std::numeric_limits<boost::uintmax_t>::digits - 1;
  15. qf = m;
  16. n = qf.convert_to<boost::uintmax_t>();
  17. if (m != n)
  18. return 2;
  19. qf *= 2;
  20. n = qf.convert_to<boost::uintmax_t>();
  21. m = (std::numeric_limits<boost::uintmax_t>::max)();
  22. if (m != n)
  23. return 3;
  24. qf = 256;
  25. int si = qf.convert_to<int>();
  26. if (si != 256)
  27. return 4;
  28. boost::intmax_t sm(1), sn;
  29. sm <<= std::numeric_limits<boost::intmax_t>::digits - 1;
  30. qf = sm;
  31. sn = qf.convert_to<boost::intmax_t>();
  32. if (sm != sn)
  33. return 5;
  34. qf *= 2;
  35. sn = qf.convert_to<boost::intmax_t>();
  36. sm = (std::numeric_limits<boost::intmax_t>::max)();
  37. if (sm != sn)
  38. return 6;
  39. // Again with negative numbers:
  40. qf = -256;
  41. si = qf.convert_to<int>();
  42. if (si != -256)
  43. return 7;
  44. sm = 1;
  45. sm <<= std::numeric_limits<boost::intmax_t>::digits - 1;
  46. sm = -sm;
  47. qf = sm;
  48. sn = qf.convert_to<boost::intmax_t>();
  49. if (sm != sn)
  50. return 8;
  51. qf *= 2;
  52. sn = qf.convert_to<boost::intmax_t>();
  53. sm = (std::numeric_limits<boost::intmax_t>::min)();
  54. if (sm != sn)
  55. return 9;
  56. // Now try conversion to cpp_int:
  57. qf = 256;
  58. boost::multiprecision::cpp_int i = qf.convert_to<boost::multiprecision::cpp_int>(), j;
  59. if (i != 256)
  60. return 10;
  61. qf = ldexp(qf, 126);
  62. i = qf.convert_to<boost::multiprecision::cpp_int>();
  63. j = 256;
  64. j <<= 126;
  65. if (i != j)
  66. return 11;
  67. return 0;
  68. }