Brick\Math
A PHP library to work with arbitrary-precision numbers.
Introduction
This library provides immutable classes to work with three types of numbers:
BigInteger — an integer number such as 123
BigDecimal — a decimal number such as 1.23
BigRational — a fraction such as 2/3 — always reduced to lowest terms, e.g. 2/6 becomes 1/3
It automatically uses GMP or BCMath when available, and falls back to a pure-PHP implementation otherwise.
All classes work with a virtually unlimited number of digits, and are only limited by available memory and CPU time.
Installation
This library is installable via Composer:
composer require brick/math
Requirements
This library requires PHP 8.2 or later.
Although the library can work seamlessly on any PHP installation, it is highly recommended that you install the
GMP or BCMath extension
to speed up calculations. The fastest available calculator implementation will be automatically selected at runtime.
Number classes
The three number classes all extend the same BigNumber class:
Brick\Math\BigNumber
├── BigInteger
├── BigDecimal
└── BigRational
BigNumber is an abstract class that defines the common behaviour of all number classes:
of() — to obtain an instance
- sign methods:
isZero(), isPositive(), etc.
- comparison methods:
isEqualTo(), isGreaterThan(), etc.
min(), max(), sum(), toString(), etc.
Instantiation
The constructors of the classes are not public, so you must use a factory method to obtain an instance.
All classes provide an of() factory method that accepts any of the following types:
BigNumber instances
int numbers
string representations of integer, decimal and rational numbers
Example:
BigInteger::of(123456);
BigInteger::of('9999999999999999999999999999999999999999999');
BigDecimal::of('9.99999999999999999999999999999999999999999999');
BigDecimal::of('1.23e1000');
BigRational::of('2/3');
The of() method of each class accepts all the representations above, as long as the value can be safely converted to that class:
BigInteger::of('1e3'); // 1000
BigInteger::of('1.00'); // 1
BigInteger::of('1.01'); // RoundingNecessaryException
BigDecimal::of('1/8'); // 0.125
BigDecimal::of('1/3'); // RoundingNecessaryException
BigRational::of('1.1'); // 11/10
BigRational::of('1.15'); // 23/20
[!NOTE]
The of() factory method does not accept float values, because casting a float to string can be lossy.
To convert a float to a BigDecimal, use one of the dedicated methods:
// Exact IEEE-754 representation — the value the float actually holds:
BigDecimal::fromFloatExact(0.1); // 0.1000000000000000055511151231257827021181583404541015625
// Shortest decimal that round-trips back to the same float:
BigDecimal::fromFloatShortest(0.1); // 0.1
Parsing untrusted input
of() places no hard limits on its input: a string with millions of digits is accepted as is, and a number in
exponential notation is expanded to its full length, so a string as short as 1e1000000000 yields a number with
a billion digits.
If your input comes from an untrusted source, such as an HTTP request, use parse() instead, which requires you
to specify the allowed syntax and a maximum number of digits:
use Brick\Math\NumberSyntax;
BigDecimal::parse($input, allowedSyntax: NumberSyntax::DECIMAL, maxDigits: 20);
The $allowedSyntax parameter restricts the accepted notations. Plain integers such as 123 are always accepted,
and each NumberSyntax case (DecimalPoint, Exponent, Fraction) allows one additional feature. The enum also
provides constants for the most common combinations:
NumberSyntax::INTEGER — integers only: 123
NumberSyntax::DECIMAL — integers and decimal numbers: 123, 123.45; typical for monetary input
NumberSyntax::SCIENTIFIC — integers, decimal numbers and exponents: 123, 123.45, 1.5e-3; accepts every JSON number
NumberSyntax::RATIONAL — integers and fractions: 123, 22/7
NumberSyntax::ALL — the full syntax accepted by of(): 123, 123.45, 1.5e-3, 22/7
The $maxDigits parameter limits the number of digits, counted both as written in the input and in the resulting number,
so that a value such as 1e1000000000 is rejected before it is ever expanded:
BigDecimal::parse('123.45', allowedSyntax: NumberSyntax::DECIMAL, maxDigits: 20); // 123.45
BigDecimal::parse('1.2e3', allowedSyntax: NumberSyntax::DECIMAL, maxDigits: 20); // NumberFormatException (exponent not allowed)
BigDecimal::parse('1e1000000000', allowedSyntax: NumberSyntax::SCIENTIFIC, maxDigits: 20); // NumberFormatException (too many digits)
Parameter types
All methods that accept a number (plus(), minus(), multipliedBy(), etc.) accept the same types as of().
For example, given the following number:
$integer = BigInteger::of(123);
The following lines are equivalent:
$integer->multipliedBy(123);
$integer->multipliedBy('123');
$integer->multipliedBy($integer);
As with of(), other types of numbers are accepted, as long as they can be safely converted to the current type:
echo BigInteger::of(2)->multipliedBy('2.0'); // 4
echo BigInteger::of(2)->multipliedBy('2.5'); // RoundingNecessaryException
echo BigDecimal::of('2.5')->multipliedBy(2); // 5.0
These parameters are converted with of(), so the same rules apply: for untrusted strings, use
parse() first, and pass the resulting number to the method.
Immutability & chaining
The BigInteger, BigDecimal and BigRational classes are immutable: their value never changes,
so that they can be safely passed around. All methods that return a BigInteger, BigDecimal or BigRational
return a new object, leaving the original object unaffected:
$ten = BigInteger::of(10);
echo $ten->plus(5); // 15
echo $ten->multipliedBy(3); // 30
The methods can be chained for better readability:
echo BigInteger::of(10)->plus(5)->multipliedBy(3); // 45
Rounding
Unless documented otherwise, all methods either return an exact result or throw an exception if the result is not exact.
Where applicable, this behaviour is configurable through an optional RoundingMode parameter:
| Rounding mode |
Description |
RoundingMode::Unnecessary |
Requires an exact result; throws if rounding would be needed. |
RoundingMode::Up |
Rounds away from zero. |
RoundingMode::Down |
Rounds toward zero. |
RoundingMode::Ceiling |
Rounds toward positive infinity. |
RoundingMode::Floor |
Rounds toward negative infinity. |
RoundingMode::HalfUp |
Rounds to nearest; ties away from zero. |
RoundingMode::HalfDown |
Rounds to nearest; ties toward zero. |
RoundingMode::HalfCeiling |
Rounds to nearest; ties toward positive infinity. |
RoundingMode::HalfFloor |
Rounds to nearest; ties toward negative infinity. |
RoundingMode::HalfEven |
Rounds to nearest; ties to the even neighbor. |
RoundingMode::HalfOdd |
Rounds to nearest; ties to the odd neighbor. |
See the Division section for examples of RoundingMode in action.
[!TIP]
PHP 8.4 introduced a native RoundingMode enum, used by
round(), bcround() and BcMath\Number::round(). If you already have a native rounding mode at hand, you can
convert it to its Brick\Math equivalent:
$roundingMode = RoundingMode::fromNativeRoundingMode(\RoundingMode::HalfAwayFromZero);
Arithmetic operations
Addition, subtraction and multiplication
These operations are straightforward on all number classes:
echo BigInteger::of(1)->plus(2)->multipliedBy(3); // 9
echo BigDecimal::of('1.2')->plus('3.4')->multipliedBy('5.6'); // 25.76
echo BigRational::of('2/3')->plus('5/6')->multipliedBy('5/4'); // 15/8
The scale of BigDecimal operation results is predictable:
- for addition and subtraction, it is the larger of the two operand scales;
- for multiplication, it is the sum of the operand scales.
BigRational results are automatically reduced to lowest terms.
Division
Division uses a class-specific API because exactness and precision rules differ between integers, decimals, and rationals.
BigInteger
By default, dividing a BigInteger returns the exact result of the division, or throws an exception if the remainder
of the division is not zero:
echo BigInteger::of(999)->dividedBy(3); // 333
echo BigInteger::of(1000)->dividedBy(3); // RoundingNecessaryException
You can pass an optional RoundingMode to round the result, if necessary:
echo BigInteger::of(1000)->dividedBy(3, RoundingMode::Down); // 333
echo BigInteger::of(1000)->dividedBy(3, RoundingMode::Up); // 334
You can also compute quotients and remainders:
echo BigInteger::of(1000)->quotient(3); // 333
echo BigInteger::of(1000)->remainder(3); // 1
You can also get both in one call:
[$quotient, $remainder] = BigInteger::of(1000)->quotientAndRemainder(3);
BigDecimal
Dividing a BigDecimal always requires a scale to be specified. If the exact result of the division does not fit in
the given scale, a RoundingMode must be provided:
echo BigDecimal::of(1)->dividedBy('8', 3); // 0.125
echo BigDecimal::of(1)->dividedBy('8', 2); // RoundingNecessaryException
echo BigDecimal::of(1)->dividedBy('8', 2, RoundingMode::HalfDown); // 0.12
echo BigDecimal::of(1)->dividedBy('8', 2, RoundingMode::HalfUp); // 0.13
If you know that the division yields a finite number of decimal places, you can use dividedByExact(), which will
automatically compute the required scale to fit the result, or throw an exception if the division yields an infinite
repeating decimal:
echo BigDecimal::of(1)->dividedByExact(256); // 0.00390625
echo BigDecimal::of(1)->dividedByExact(11); // RoundingNecessaryException
BigRational
The result of the division of a BigRational can always be represented exactly:
echo BigRational::of('13/99')->dividedBy('7'); // 13/693
echo BigRational::of('13/99')->dividedBy('9/8'); // 104/891
BigRational results are automatically reduced to lowest terms.
Other arithmetic operations
In addition to plus(), minus(), multipliedBy(), and dividedBy(), the library provides:
- exponentiation with
power() on all number classes
- square root with
sqrt() on BigInteger and BigDecimal
- nth root with
nthRoot() on BigInteger and BigDecimal
- greatest common divisor / least common multiple with
gcd(), lcm(), gcdAll(), lcmAll() on BigInteger
- modular arithmetic with
mod(), modInverse(), and modPow() on BigInteger
- reciprocal with
reciprocal() on BigRational
- decimal-point shifts with
withPointMovedLeft() and withPointMovedRight() on BigDecimal
Sign and comparison
All number classes share the same sign and comparison methods through BigNumber.
Sign methods
Use these methods to inspect the sign of a number:
getSign() — returns -1, 0, or 1 for values < 0, = 0, and > 0, respectively
isZero()
isNegative()
isNegativeOrZero()
isPositive()
isPositiveOrZero()
For sign-related transformations, use:
abs() — returns the absolute value
negated() — returns the opposite value
Comparison methods
Comparison works across all number classes (BigInteger, BigDecimal, BigRational):
compareTo() — returns -1, 0, or 1 if this number is <, =, or > the given number
isEqualTo()
isLessThan()
isLessThanOrEqualTo()
isGreaterThan()
isGreaterThanOrEqualTo()