Really Ha-yuge Integers

Unlike real numbers, integers are stored in binary as-is: The bits are read and values set. The only limit on an integer’s value is on the number of bits in the binary chunk — the bit width — which sets the value’s range, positive and negative or just positive.

For most computer systems, the largest integer value is based on a word size of 64 bits. In C, this size is data type long or long long. The values range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. The unsigned value is large enough that E notation is required: 264-1

Until the 128-bit era dawns for PCs and Linux boxes, the unsigned long long is as huge as you’re going to see for straight integer values in C — or on any computer system. Here’s a demo:

2022_10_01-Lesson-a.c

#include <stdio.h>

int main()
{
    unsigned long long i = 0;

    while(i++)
        printf("%llu\n",i);

    return(0);
}

The program outputs values from 1 through 18,446,744,073,709,551,614. As you can imagine, it takes quite a while to run, mostly because screen output is slow. I gave up running this code after three days. My guess is it would take over a week for the program to finish. When output is suppressed, however, the program runs in 0.003 seconds! That’s how long it takes the computer to count from one through 264-1.

Just imagine how much longer it would take such a program to run if the upper limit of an integer were the full 128 bytes! Or maybe 256 bytes? Or an infinite number of bytes?

Well, you don’t need to imagine. That’s because the GMP library exists. It extends the C language’s capabilities to deal with integers of nearly infinite size.

In this blog, I normally don’t cover C programming beyond the standard library. Yes, I’ve covered the math library, which must be linked for Linux systems. But this is the first time I’m exploring adding a new library to your compiler: the GMP Library.

The full name is the GNU Multiple Precision Arithmetic Library. It may be pre-installed on your computer, though probably not. You must use the Linux package manager to obtain and install the library. This step adds the required header files and libraries so that you can play with ha-yuge integer values.

In Linux, install the libgmp-dev package. The command I use in Ubuntu is:

sudo apt-get install libgmp-dev

On the Mac, I use the Homebrew program to obtain the package. The command is brew install gmp

To confirm that the package is installed, run the following code:

2022_10_01-Lesson-b.c

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

int main()
{
    printf("GMP version: %d\n",__GNU_MP_RELEASE);
    return(0);
}

The program’s output reflects the GMP library version number, which can vary depending on the package. For the Mac, it shows:

GMP version: 60201

In Ubuntu, I see:

GMP version: 60102

Upon success (meaning the compiler sees the gmp.h header file), you can confirm that the GMP library is installed on your system. Compiling code that uses the library’s functions requires that you use the -lgmp switch when compiling. This option is added last at the command prompt. In an IDE, you must configure the compiler to add the -lgmp switch or otherwise link the GMP library as part of the build process.

I continue exploration of the GMP library and its monstrous integer values with next week’s Lesson.

Leave a Reply