Problem with large values in GMP...

sisyphus1 at optusnet.com.au sisyphus1 at optusnet.com.au
Sun Mar 20 00:50:10 UTC 2016


-----Original Message----- 
From: "Torbjörn" Granlund
Sent: Saturday, March 19, 2016 10:21 PM
To: wraithx at morpheus.net
Cc: gmp-discuss at gmplib.org
Subject: Re: Problem with large values in GMP...

> wraithx at morpheus.net writes:
>
>   So, would something similar happen during normal mpz_mul operations?
>   Here is the behavior I was originally trying to understand:
>
>   mpz_mul(num3, num1, num2)
>   num bits in num1 = 4006516961
>   num bits in num2 =  288450334
>   num bits in num3 = 4294967295
>
>   mpz_mul(num3, num1, num2)
>   num bits in num1 = 4006516961
>   num bits in num2 =  288450365
>   num bits in num3 =         30
>
> That's not supposed to happen.  We try to detect spill.
>
> Please report this to gmp-bugs.

I can't reproduce this problem on my Windows 7 (64) box, using gmp-6.1.0.
I get:

mpz_mul(num3, num1, num2)
bits in num1 = 4006516961
bits in num2 =  288450334
bits in num3 = 4294967295

mpz_mul(num3, num1, num2)
bits in num1 = 4006516961
bits in num2 =  288450365
bits in num3 = 4294967326

That's with 64-bit gcc-4.9.2.

I couldn't locate the original script that the OP used. Are we sure it 
specified:

printf("num bits in num3 = %llu\n", mpz_sizeinbase(num3, 2));
and not
printf("num bits in num3 = %u\n", mpz_sizeinbase(num3, 2));

(The latter version will report "30").

Below my sig is the actual program I ran.

For me, when I switch to 32-bit gcc-4.9.2, the program crashes with:

GNU MP: Cannot allocate memory (size=146817032)

And that happens for both values of num2.

Cheers,
Rob

/**********************************/
#include <stdio.h>
#include <gmp.h>

int main(void) {
mpz_t num1, num2, num3;

mpz_init(num3);
mpz_init_set_ui(num1, 1);
mpz_init_set_ui(num2, 1);

mpz_mul_2exp(num1, num1, 4006516961);
mpz_mul_2exp(num2, num2, 288450365);

mpz_sub_ui(num1, num1, 1);
mpz_sub_ui(num2, num2, 1);
mpz_mul(num3, num1, num2);

printf("mpz_mul(num3, num1, num2)\n");
printf("num bits in num1 = %llu\n", mpz_sizeinbase(num1, 2));
printf("num bits in num2 =  %llu\n", mpz_sizeinbase(num2, 2));
printf("num bits in num3 = %llu\n", mpz_sizeinbase(num3, 2));

return 0;
}

/**********************************/



More information about the gmp-discuss mailing list