How to use GMP.
juneco@quartz.ocn.ne.jp
juneco@quartz.ocn.ne.jp
Tue, 6 May 2003 12:26:23 +0900
Tomonori Kouya here.
Your program is not standard, I think. If you get correct results only,
how about the following list ?
#include <stdio.h>
#include <gmp.h>
#include <stdarg.h>
//#include <obstack.h>
main()
{
mpz_t bbbbb, aaaaa;
mpz_init_set_str(bbbbb,"2147483647", 10);
mpz_init(aaaaa);
mpz_mul_ui(aaaaa, bbbbb, 3);
gmp_printf("bbbbb=%Zd\n",bbbbb);
gmp_printf("aaaaa=%Zd\n",aaaaa);
mpz_clear(bbbbb);
mpz_clear(aaaaa);
}
Results on Pentium 4 PC with Windows XP and GMP 4.1.2:
$ ./a
bbbbb=2147483647
aaaaa=6442450941
Hashida <jhashida@fls.fujitsu.com>さん:
> 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*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);
> 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*3 is 6442450944 in decimal system.
>
> Please teach me how to recognize 6442450944 from the above result.
> Simply the way of the usage of "printf" may be bad, but I don't know.
>
> Thanks.
>
> Hashida
>