Skip to Content
Course content

109: GMP for Big Integers

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I was working on a project recently that involved some basic cryptography, and I ran into a wall that every PHP developer hits eventually: the integer ceiling. I tried to calculate a value that I knew would be huge, and PHP did something that looked correct at first glance, but was actually a disaster for my data integrity.

The Moment the Numbers Break

Let's look at what happened. I wanted to calculate 2 to the power of 64. On a 64-bit system, the maximum integer is $2^{63}-1$. I figured, "Eh, it's just one digit over, how bad can it be?"

<?php
$val = 2 ** 64; 
var_dump($val);
?>

I expected a long string of digits. Instead, I got this: float(1.8446744073709552E+19). Notice that float? PHP silently converted my integer to a floating-point number because it exceeded PHP_INT_MAX. Now, if you're building a calculator for a middle school math class, that's fine. If you're dealing with encryption keys or precise financial IDs, that scientific notation is a death sentence. We've lost precision.

Bringing in the Big Guns

I can't use standard integers, and I certainly can't use floats. This is where the GMP (GNU Multiple Precision) extension comes in. It's designed specifically for "arbitrary-precision arithmetic," which is a fancy way of saying "numbers as big as your RAM can hold."

First, I tried to just pass my big number into a GMP function. I wanted to see that $2^{64}$ value in its full, un-truncated glory.

<?php
// I'll use a string because if I put the raw number, 
// PHP converts it to a float before GMP even sees it.
$bigNum = gmp_init("18446744073709551616");
echo gmp_strval($bigNum); 
?>

That worked. gmp_init() takes a string (or an int) and turns it into a GMP number object. I also had to use gmp_strval() to print it, because you can't just echo a GMP object directly—it'll just tell you it's an object of the GMP class.

Wait, Where's the Plus Sign?

Here is the part that tripped me up. I tried to add 1 to my big number using the standard + operator, expecting GMP to be "smart" about it.

<?php
$bigNum = gmp_init("18446744073709551616");
$result = $bigNum + 1; 
var_dump($result);
?>

It didn't work. PHP doesn't overload the arithmetic operators for GMP objects. It tries to cast the GMP object back to a native PHP type to perform the addition, which brings us right back to the float precision problem I was trying to avoid in the first place.

To do math with GMP, you have to use the dedicated functions. It feels a bit like writing Lisp for a second, but it's the only way to ensure the precision stays intact:

<?php
$bigNum = gmp_init("18446744073709551616");
$one = gmp_init(1);

// Instead of $bigNum + 1
$result = gmp_add($bigNum, $one);

echo gmp_strval($result); // 18446744073709551617
?>

I've found that the most common GMP functions you'll actually use are gmp_add(), gmp_sub(), gmp_mul(), gmp_div(), and gmp_pow(). Just remember: if you're using GMP, you're leaving the world of standard operators behind. Every single operation must go through a function call.




📋 Practical Task

Building a 100th Factorial Calculator

Your task is to write a script that calculates the factorial of 100 (100!). A factorial is the product of an integer and all the integers below it (e.g., 5! = 5 * 4 * 3 * 2 * 1).

Since 100! is a massive number that far exceeds the capacity of a 64-bit integer, you must use the GMP extension. Create a loop that iterates from 1 to 100, multiplying the values using gmp_mul(), and finally output the full resulting number as a string using gmp_strval().

Rating
0 0

There are no comments for now.

to be the first to leave a comment.