How to use GMP.

Hashida jhashida@fls.fujitsu.com
Tue, 06 May 2003 12:15:18 +0900


--------------AF68E40F2DF07F1EC7EC853D
Content-Type: text/plain; charset=iso-2022-jp
Content-Transfer-Encoding: 7bit

Hi

I am a beginner for GMP and not good at C.
I can't understand how to use GMP very well.
For example, in arithmetic of (231-1)*3
I wrote the following C program.

#include <stdio.h>
#include <gmp.h>
#include <stdarg.h>
#include <obstack.h>

main()
{
        MP_INT *bbbbb=(MP_INT *)malloc(50);
        MP_INT aaaaa;

        bbbbb->_mp_d=(mp_limb_t *)malloc(10);

        mpz_init_set_ui(bbbbb,2147483647);         //2147483647==(231-1)

        printf("bbbbb->_mp_alloc=%d\n",bbbbb->_mp_alloc);
        printf("bbbbb->_mp_size=%d\n",bbbbb->_mp_size);
        printf("*(bbbbb->_mp_d)=%d\n",*(bbbbb->_mp_d));
        mpz_init(&aaaaa);
        mpz_mul_ui(&aaaaa, bbbbb, 3);
        printf("aaaaa._mp_alloc=%d\n",aaaaa._mp_alloc);
        printf("aaaaa._mp_size=%d\n",aaaaa._mp_size);
        printf("*(aaaaa._mp_d)=%d\n",*(aaaaa._mp_d));

}

The result of the execution of this program is as follows.

bbbbb->_mp_alloc=1
bbbbb->_mp_size=1
*(bbbbb->_mp_d)=2147483647
aaaaa._mp_alloc=2
aaaaa._mp_size=2
*(aaaaa._mp_d)=2147483645

(231-1)*3 is 6442450941 in decimal system.

Please teach me how to recognize 6442450941 from the above result.
Simply the way of the usage of "printf" may be bad, but I don't know.

Thanks.

Hashida

--------------AF68E40F2DF07F1EC7EC853D
Content-Type: text/html; charset=iso-2022-jp
Content-Transfer-Encoding: 7bit

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
Hi
<p>I am a beginner for GMP and not good at C.
<br>I can't understand how to use GMP very well.
<br>For example, in arithmetic of (2<sup>31</sup>-1)*3
<br>I wrote the following C program.
<p>#include &lt;stdio.h>
<br>#include &lt;gmp.h>
<br>#include &lt;stdarg.h>
<br>#include &lt;obstack.h>
<p>main()
<br>{
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MP_INT *bbbbb=(MP_INT *)malloc(50);
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MP_INT aaaaa;
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bbbbb->_mp_d=(mp_limb_t *)malloc(10);
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mpz_init_set_ui(bbbbb,2147483647);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
//2147483647==(2<sup>31</sup>-1)
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("bbbbb->_mp_alloc=%d\n",bbbbb->_mp_alloc);
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("bbbbb->_mp_size=%d\n",bbbbb->_mp_size);
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("*(bbbbb->_mp_d)=%d\n",*(bbbbb->_mp_d));
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mpz_init(&amp;aaaaa);
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mpz_mul_ui(&amp;aaaaa, bbbbb,
3);
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("aaaaa._mp_alloc=%d\n",aaaaa._mp_alloc);
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("aaaaa._mp_size=%d\n",aaaaa._mp_size);
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("*(aaaaa._mp_d)=%d\n",*(aaaaa._mp_d));
<p>}
<p>The result of the execution of this program is as follows.
<p>bbbbb->_mp_alloc=1
<br>bbbbb->_mp_size=1
<br>*(bbbbb->_mp_d)=2147483647
<br>aaaaa._mp_alloc=2
<br>aaaaa._mp_size=2
<br>*(aaaaa._mp_d)=2147483645
<p>(2<sup>31</sup>-1)*3 is 6442450941 in decimal system.
<p>Please teach me how to recognize 6442450941 from the above result.
<br>Simply the way of the usage of "printf" may be bad, but I don't know.
<p>Thanks.
<p>Hashida</html>

--------------AF68E40F2DF07F1EC7EC853D--