Discussion:
Multiprecision -- cubing a number gives unexpected result
(too old to reply)
Craig Henderson
2017-01-19 17:39:59 UTC
Permalink
Hello,
I am new to Boost.Multiprecision and have come across a result that I
cannot understand.

I have the following code to calculate the sum of three cubes,
using bigint_t = boost::multiprecision::mpz_int;
bigint_t a = 2220422932;
bigint_t b = 2218888517; b = -b;
bigint_t c = 283059965; c = -c;
std::cout << '(' << a << ")^3+(" << b << ")^3+(" << c << ")^3 = ";
bigint_t a1 = a * a * a;
bigint_t b1 = b * b * b;
bigint_t c1 = c * c * c;
std::cout << '(' << a1 << ")+(" << b1 << ")+(" << c1 << ") = ";
std::cout << bigint_t(a1+b1+c1) << '\n';

and it produces the correct result
(2220422932)^3+(-2218888517)^3+(-283059965)^3 =
(10947302325566084787191541568)+(-10924622727902378924946084413)+(-22679597663705862245457125)
= 30

However, if I use this implementation, with a,b and c assigned the result
of the multiplication,
a = a * a * a;
b = b * b * b;
c = c * c * c;
std::cout << '(' << a << ")+(" << b << ")+(" << c << ") = ";
std::cout << bigint_t(a+b+c) << '\n';
... I get a very strange result:
(24307641127223864542936438774018437376)+(24240519923499804093545671548118385521)+(6419686120902663137493915211500625)
= 48554580736844571299619604237348323522

What is going on? Is this an invalid use of the multiplication that I have
missed?
I get the same result with cpp_int and gmp;
using bigint_t = boost::multiprecision::cpp_int;
using bigint_t = boost::multiprecision::mpz_int;

I'm using g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609, Boost 1.58

Many thanks
Craig
John Maddock
2017-01-19 19:23:56 UTC
Permalink
It's this bug: https://svn.boost.org/trac/boost/ticket/12408

Fixed in boost-1.62.

HTH, John.
Post by Craig Henderson
Hello,
I am new to Boost.Multiprecision and have come across a result that I
cannot understand.
I have the following code to calculate the sum of three cubes,
using bigint_t = boost::multiprecision::mpz_int;
bigint_t a = 2220422932;
bigint_t b = 2218888517; b = -b;
bigint_t c = 283059965; c = -c;
std::cout << '(' << a << ")^3+(" << b << ")^3+(" << c << ")^3 = ";
bigint_t a1 = a * a * a;
bigint_t b1 = b * b * b;
bigint_t c1 = c * c * c;
std::cout << '(' << a1 << ")+(" << b1 << ")+(" << c1 << ") = ";
std::cout << bigint_t(a1+b1+c1) << '\n';
and it produces the correct result
(2220422932)^3+(-2218888517)^3+(-283059965)^3 =
(10947302325566084787191541568)+(-10924622727902378924946084413)+(-22679597663705862245457125)
= 30
However, if I use this implementation, with a,b and c assigned the
result of the multiplication,
a = a * a * a;
b = b * b * b;
c = c * c * c;
std::cout << '(' << a << ")+(" << b << ")+(" << c << ") = ";
std::cout << bigint_t(a+b+c) << '\n';
(24307641127223864542936438774018437376)+(24240519923499804093545671548118385521)+(6419686120902663137493915211500625)
= 48554580736844571299619604237348323522
What is going on? Is this an invalid use of the multiplication that I
have missed?
I get the same result with cpp_int and gmp;
using bigint_t = boost::multiprecision::cpp_int;
using bigint_t = boost::multiprecision::mpz_int;
I'm using g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609, Boost 1.58
Many thanks
Craig
_______________________________________________
Boost-users mailing list
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Loading...