High-precision numbers computation with PHP

Crunch high-precision large & small numbers in PHP using its official bcmath extension.

Edouard Courty
4 min readApr 18, 2023
Photo by Volkan Olmez on Unsplash

Have you ever needed to work with extremely large numbers in PHP, only to find that your code was slow, computing wrong results, or even crashed due to memory constraints?

While PHP offers some built-in functions and libraries for working with large numbers, the language’s interpreter and underlying hardware can pose significant limitations on performance and scalability.

In fact, when dealing with numbers that are too large for PHP’s built-in integer type, traditional techniques for computation simply won’t work.

That’s where specialized libraries and algorithms come in. In this article, we’ll explore some of the challenges of working with large numbers in PHP and introduce some alternative techniques for high-speed computation.

We’ll go through the usage of the bcmath PHP extension, used to work with big numbers.

What is BCMATH?

bcmath is a PHP extension that provides arbitrary precision arithmetic functions for working with large numbers. It should be used when you need to perform mathematical operations on numbers that exceed the range of PHP's built-in data types (i.e. int and float).

In PHP, int can typically store numbers up to a maximum value of 2^63 - 1 on 64-bit systems (or 2^31 - 1 on 32-bit systems), while float can represent numbers with up to 14 decimal places. If you need to work with numbers that exceed these limits, you will run into issues with accuracy and precision.

bcmath provides a way to perform calculations with arbitrary precision, meaning that you can specify the number of decimal places or significant figures you need, and the library will handle the calculations accordingly. This makes it useful for a variety of applications, such as financial calculations, cryptography, and scientific computing.

However, bcmath is slower and less memory-efficient than the built-in arithmetic operators for int and float, so it should only be used when absolutely necessary. In general, you should stick to PHP's built-in data types whenever possible and only use bcmath when you need the extra precision it provides.

Installation

If you’ve never used bcmath before, there is a chance you don’t have it installed on your machine.

Using Docker

If you’re using Docker & PHP’s official images, simply add the following line in your Dockerfile to enable the bcmath extension.

RUN docker-php-ext-install bcmath

Without Docker

If you’re running PHP in your local system, simply install it by running the following command

sudo apt install php<PHP version>-bcmath # Example: php8.1-bcmath

Usage

Here is how simple calculations would be done with the base mathematical operators:

$addition = 35 + 10;
$substraction = 10 - 5;
$multiplication = 35 * 2;
$division = 25 / 5;
$power = 3 ** 3;

Now, let’s do the same calculations using bcmath.

$addition = bcadd('35', '10');
$substraction = bcsub('10', '5');
$multiplication = bcmul('35', '2');
$division = bcdiv('25', '5');
$power = bcpow('3', '3');

As you can see, the numbers are now in the form of strings: strings can hold way more data than integers.

Going further

bcmath provides a wide range of functions for performing arbitrary precision arithmetic operations. Here are some of the most commonly used functions:

  1. bcadd() - Adds two arbitrary precision numbers.
  2. bcsub() - Subtracts one arbitrary precision number from another.
  3. bcmul() - Multiplies two arbitrary precision numbers.
  4. bcdiv() - Divides one arbitrary precision number by another.
  5. bcpow() - Raises a number to a specified power.
  6. bcsqrt() - Calculates the square root of an arbitrary precision number.
  7. bcmod() - Returns the modulus of one arbitrary precision number divided by another.
  8. bccomp() - Compares two arbitrary precision numbers and returns 0 if they are equal, 1 if the first is larger, and -1 if the second is larger.
  9. bcround() - Rounds an arbitrary precision number to a specified number of decimal places.
  10. bcscale() - Sets the default scale (number of decimal places) used by bcmath functions.

These functions are just a few examples of what bcmath can do. For a full list of bcmath functions and their descriptions, you can refer to the official PHP documentation.

In conclusion, bcmath is a powerful PHP extension for high-speed computation of large numbers that exceed PHP's built-in data types. It provides arbitrary precision arithmetic and is essential for accurate calculations.

Despite being slower and less memory-efficient than PHP's built-in arithmetic operators, bcmath is necessary when dealing with numbers beyond PHP's data type limits.

Check out my other posts for more tips and insights on programming and tech.

Thank you for reading, and have a good day!

🎉

--

--

Edouard Courty

Web Developer & IT Teacher based in Paris - Back-end guru - Co-founder of @IMXrarity