Arbitrary-precision arithmetic library for PHP
A PHP library to work with arbitrary-precision numbers.
This library provides immutable classes to work with three types of numbers:
BigInteger — an integer number such as 123BigDecimal — a decimal number such as 1.23BigRational — a fraction such as 2/3 — always reduced to lowest terms, e.g. 2/6 becomes 1/3It 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.
This library is installable via Composer:
composer require brick/mathThis 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.
The three number classes all extend the same BigNumber class:
Brick\Math\BigNumber
├── BigInteger
├── BigDecimal
└── BigRationalBigNumber is an abstract class that defines the common behaviour of all number classes:
of() — to obtain an instanceisZero(), isPositive(), etc.isEqualTo(), isGreaterThan(), etc.min(), max(), sum(), toString(), etc.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 instancesint numbersstring representations of integer, decimal and rational numbersExample:
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 acceptfloatvalues, because casting a float to string can be lossy. To convert a float to aBigDecimal, 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
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: 123NumberSyntax::DECIMAL — integers and decimal numbers: 123, 123.45; typical for monetary inputNumberSyntax::SCIENTIFIC — integers, decimal numbers and exponents: 123, 123.45, 1.5e-3; accepts every JSON numberNumberSyntax::RATIONAL — integers and fractions: 123, 22/7NumberSyntax::ALL — the full syntax accepted by of(): 123, 123.45, 1.5e-3, 22/7The $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)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.0These parameters are converted with of(), so the same rules apply: for untrusted strings, use
parse() first, and pass the resulting number to the method.
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); // 30The methods can be chained for better readability:
echo BigInteger::of(10)->plus(5)->multipliedBy(3); // 45Unless 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()andBcMath\Number::round(). If you already have a native rounding mode at hand, you can convert it to itsBrick\Mathequivalent:$roundingMode = RoundingMode::fromNativeRoundingMode(\RoundingMode::HalfAwayFromZero);
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/8The scale of BigDecimal operation results is predictable:
BigRational results are automatically reduced to lowest terms.
Division uses a class-specific API because exactness and precision rules differ between integers, decimals, and rationals.
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); // RoundingNecessaryExceptionYou 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); // 334You can also compute quotients and remainders:
echo BigInteger::of(1000)->quotient(3); // 333
echo BigInteger::of(1000)->remainder(3); // 1You can also get both in one call:
[$quotient, $remainder] = BigInteger::of(1000)->quotientAndRemainder(3);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.13If 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); // RoundingNecessaryExceptionThe 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/891BigRational results are automatically reduced to lowest terms.
In addition to plus(), minus(), multipliedBy(), and dividedBy(), the library provides:
power() on all number classessqrt() on BigInteger and BigDecimalnthRoot() on BigInteger and BigDecimalgcd(), lcm(), gcdAll(), lcmAll() on BigIntegermod(), modInverse(), and modPow() on BigIntegerreciprocal() on BigRationalwithPointMovedLeft() and withPointMovedRight() on BigDecimalAll number classes share the same sign and comparison methods through BigNumber.
Use these methods to inspect the sign of a number:
getSign() — returns -1, 0, or 1 for values < 0, = 0, and > 0, respectivelyisZero()isNegative()isNegativeOrZero()isPositive()isPositiveOrZero()For sign-related transformations, use:
abs() — returns thNo open issues yet, or sync has not completed.