Ticket #12580: sub_from_pow2.cpp

File sub_from_pow2.cpp, 940 bytes (added by Michael Shatz, 6 years ago)

demonstrates incorrect rounding in corner case of subtraction

Line 
1#include <iostream>
2#include <boost/multiprecision/cpp_bin_float.hpp>
3
4typedef boost::multiprecision::number<boost::multiprecision::cpp_bin_float<128, boost::multiprecision::backends::digit_base_2> > boost_float128_t;
5typedef boost::multiprecision::number<boost::multiprecision::cpp_bin_float<256, boost::multiprecision::backends::digit_base_2> > boost_float256_t;
6
7int main(int , char** )
8{
9 boost_float128_t a = 1;
10 boost_float128_t b = ldexp(boost_float128_t(0.99), -128);
11 boost_float128_t c = a - b;
12 boost_float128_t d = boost_float128_t(boost_float256_t(a) - boost_float256_t(b));
13
14 std::cout
15 << std::setprecision(40)
16 << std::scientific
17 << "a = " << a << "\n"
18 << "b = " << b << "\n"
19 << "c = " << c << " (a - b)\n"
20 << "d = " << d << " (a - b) calculated with 256-bit precision.\n"
21 ;
22
23 if (c == d)
24 std::cout << "o.k\n";
25 else
26 std::cout << "fail.\n";
27 return 0;
28}