Problem with large values in GMP...

wraithx at morpheus.net wraithx at morpheus.net
Thu Mar 17 02:19:16 UTC 2016


Hello all,

While trying to track down one problem I am experiencing with GMP on 64-bit 
Windows, I found another similar problem.  I don't know if the two are related, 
but I was hoping that solving the first might either solve or lead to a solution 
for the second.  The problem that I can reproduce with a simple test case is the 
following.  When I calculate with mpz_ui_pow_ui, my GMP program will run fine for:
3^2694881279
But it will crash for:
3^2694881280
With the error "gmp: overflow in mpz type"

On the other hand, when I calculate:
n = 3^1347440640
And then square it with mpz_pow_ui(n, 2), it is able to successfully calculate 
the value:
3^2694881280

I can see that this number has very close to 2^32 bits (4271285773 to be exact), 
and so somehow that may contribute to the problem.  But, I thought that on 
64-bit Windows, I should be able to create and use numbers that have more that 
2^32 bits.  I guess I should start here.  Is this a valid first assumption?

If so, is there a reason why the mpz_ui_pow_ui calculation above would fail 
while the mpz_pow_ui calculation succeeds?  I have reproduced this problem on 
two Win64 computers.  Here are the details of one of them:

Windows 7 Pro SP1 x64
Intel Xeon E3-1241 v3
16GB ram

Msys build environment
MinGW64 4.9.3 compiler

I tried 3 different versions of GMP to try to make sure it wasn't specific to 
one version.  I ran "./configure", then "make", and then "make check".  All of 
those ran successfully to completion.  The different GMP's picked different 
compiler options, like so:

GMP 5.1.3 (-mtune=corei7 -march=corei7)

GMP 6.0.0 (-mtune=corei7 -march=corei7)

GMP 6.1.0 (-mtune=haswell -march=haswell)

I am attaching a zip file containing a test program and my config.log from the 
6.1.0 build.  I will also type out the test program at the end of this email, in 
case attachments aren't allowed.  Can anyone help me find out what might be 
going wrong with these calculations?  Is there any additional information I can 
provide to help track down this problem?  Thank you for any help you can provide.

-David C.

exponent_test.c
-----------------------------
#include <stdio.h>
#include <gmp.h>

void calculate_3exp(mpz_t n, unsigned int exponent) {
   printf("\nCalculating 3^%u\n", exponent);
   mpz_ui_pow_ui(n, 3, exponent);
   printf("Finished calculating 3^%u\n", exponent);
   printf("Number of bits in 3^%u = %llu\n", exponent, mpz_sizeinbase(n, 2));
}

int main(int argc, char* argv[]) {
   mpz_t num1, num2, num3;
   unsigned int exp = 0;

   mpz_init(num1);
   mpz_init(num2);
   mpz_init(num3);

   exp = 1347440640;
   calculate_3exp(num1, exp);

   printf("\nSquaring result...\n");
   mpz_pow_ui(num2, num1, 2);
   printf("Number of bits in 3^%u = %llu\n", 2*exp, mpz_sizeinbase(num2, 2));

   exp = 2694881279UL;
   calculate_3exp(num3, exp);

   exp = 2694881280UL;
   calculate_3exp(num3, exp);

   mpz_clear(num1);
   mpz_clear(num2);
   mpz_clear(num3);

   return 0;
}
-----------------------------

Here is the result on my two Win64 computers:

Calculating 3^1347440640
Finished calculating 3^1347440640
Number of bits in 3^1347440640 = 2135642887

Squaring result...
Number of bits in 3^2694881280 = 4271285773

Calculating 3^2694881279
Finished calculating 3^2694881279
Number of bits in 3^2694881279 = 4271285772

Calculating 3^2694881280
gmp: overflow in mpz type



More information about the gmp-discuss mailing list