Monthly Archives: March 2012

GMP on Mac and Windows

There is some task requires handling very large numbers. Say 417812367683563 multiplies 354325123456546 equals what? We need way more digits than the normal double precision integers can do. GMP is a nice library to deal with this problem. It is well documented. But it refuses to make on my Mac OS X 10.5.8 due to XCode issue. After a few search, I found a pre-compiled GMP here.  A minimal test:

#include <gmp.h>

mpz_t n;
mpz_init_set_str (n, “417812367683563”, 0);
gmp_printf (“n %Zd\n”, n);

mpz_t m;
mpz_init_set_str (m, “354325123456546”, 0);
gmp_printf (“m %Zd\n”, m);

mpz_t mn;
mpz_init(mn);
mpz_mul(mn, m, n);
gmp_printf (“m * n %Zd\n”, mn);

link flag is -lgmp

This might be working! I know the answer is 148041418761150250269708953398

On the Windows side, I found MPIR. Need YASM to build it. Run configure and make inside /win dir. It seems to be fine.

Leave a comment

Filed under C++