GMP is a library that will allow you to perform calculations on numbers that extend past the reach of what your standard data sizes can hold. From their website:
GMP is a free library for arbitrary precision arithmetic, operating on signed integers, rational numbers, and floating-point numbers. There is no practical limit to the precision except the ones implied by the available memory in the machine GMP runs on. GMP has a rich set of functions, and the functions have a regular interface.
This means you can embed large math into your programs and will only be limited by the amount of memory on your running system.
In today’s article, we’ll go through a few simple examples on how to use this library.
Getting setup
Get gmp installed locally on your system either by downloading the latest release from their site, or just using your package manager.
Building
For any program that will require the gmp library, we’ll need to add the -lgmp switch:
Now, we’re ready to go.
Factorial
As an example implementation, we’ll write a program to calculate the n’th factorial for us. First of all, we’ll implement this using traditional data types offered to us through C, and then we’ll swap this out to get greater numbers.
We build this application not needing the gmp library:
We can then start to test it out.
The wheels start to fall off once we want to look at numbers higher than !19.
We overflowed our integer to where it wrapped into negative numbers. 19 is our limit for traditional data types.
Make the numbers bigger!
Now we can introduce gmp to help us break out of these constraints.
We’ll rewrite the factorial function above to operate on gmp types, and we’ll also convert the body of our program into a input capture function that will parse text into a gmp type for us.
Let’s deal with the input first:
First, you’ll notice that we’re not returning anything here. The mpz_t type is typed as an array, and as such can’t be used as a return. So, we supply it as an output parameter. This pattern will reoccur through these examples.
This function also assumes that res has already had mpz_init run on it, so it’s not magically allocating resources on your behalf.
mpz_set_ui sets the initial state of an mpz_t with a value from an integer (the real world!). mpz_set_str is really doing most of the work for us here, parsing out a string that its given into a mpz_t. The base needs to be supplied.
Now the factorial function will need to change as you’d expect:
Again, res is an output parameter with no return value. We do have a “work” variable here, so we set it up and destroy it all within the context of our function so we don’t have a memory leak.
mpz_cmp takes care of the looping for us. We’ve substituted a for loop here for a while loop to accommodate.
Full program!
Now we can use these functions in a program of our own. Here’s a full listing of an application that will give you the factorial of an arbitrary length integer.
We write the result number here with mpz_out_str. This can redirected to any stream of your choice.
Building this program is just adding the lgmp switch.
And, now you can calculate factorials as high as you like: