Arbitrary-precision arithmetic library for PHP
The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:
A PHP library to work with arbitrary precision numbers.
This library is installable via Composer:
composer require brick/math
This library requires PHP 7.1 or later.
For PHP 5.6 and PHP 7.0 compatibility, you can use version
0.5. Note that these PHP versions are EOL and not supported anymore. If you're still using one of these PHP versions, you should consider upgrading as soon as possible.
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.
While this library is still under development, it is well tested and considered stable enough to use in production environments.
The current releases are numbered
0.x.y. When a non-breaking change is introduced (adding new methods, optimizing existing code, etc.),
When a breaking change is introduced, a new
0.xversion cycle is always started.
It is therefore safe to lock your project to a given release cycle, such as
If you need to upgrade to a newer release cycle, check the release history for a list of changes introduced by each further
This library provides the following public classes in the
And the following exceptions in the
The constructors of the classes are not public, you must use a factory method to obtain an instance.
All classes provide an
of()factory method that accepts any of the following types:
stringrepresentations of integer, decimal and rational numbers
BigRational::of('2/3'); BigRational::of('1.1'); // 11/10
Note that all
of()methods accept all of the representations above, as long as it can be safely converted to the current type:
BigInteger::of('1.00'); // 1 BigInteger::of('1.01'); // RoundingNecessaryException
BigDecimal::of('1/8'); // 0.125 BigDecimal::of('1/3'); // RoundingNecessaryException
Note about native integers: instantiating from an
intis safe as long as you don't exceed the maximum value for your platform (
PHP_INT_MAX), in which case it would be transparently converted to
floatby PHP without notice, and could result in a loss of information. In doubt, prefer instantiating from a
string, which supports an unlimited numbers of digits:
echo BigInteger::of(999999999999999999999); // 1000000000000000000000 echo BigInteger::of('999999999999999999999'); // 999999999999999999999
Note about floating-point values: instantiating from a
floatmight be unsafe, as floating-point values are imprecise by design, and could result in a loss of information. Always prefer instantiating from a
string, which supports an unlimited number of digits:
echo BigDecimal::of(1.99999999999999999999); // 2 echo BigDecimal::of('1.99999999999999999999'); // 1.99999999999999999999
BigRationalclasses are immutable: their value never changes, so that they can be safely passed around. All methods that return a
BigRationalreturn 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
All methods that accept a number:
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);
of(), other types of
BigNumberare acceptable, as long as they can be safely converted to the current type:
echo BigInteger::of(2)->multipliedBy(BigDecimal::of('2.0')); // 4 echo BigInteger::of(2)->multipliedBy(BigDecimal::of('2.5')); // RoundingNecessaryException echo BigDecimal::of(2.5)->multipliedBy(BigInteger::of(2)); // 5.0
By default, dividing a
BigIntegerreturns 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 rounding mode to round the result, if necessary:
echo BigInteger::of(1000)->dividedBy(3, RoundingMode::DOWN); // 333 echo BigInteger::of(1000)->dividedBy(3, RoundingMode::UP); // 334
If you're into quotients and remainders, there are methods for this, too:
echo BigInteger::of(1000)->quotient(3); // 333 echo BigInteger::of(1000)->remainder(3); // 1
You can even get both at the same time:
[$quotient, $remainder] = BigInteger::of(1000)->quotientAndRemainder(3);
BigDecimalalways requires a scale to be specified. If the exact result of the division does not fit in the given scale, a rounding mode 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::HALF_DOWN); // 0.12 echo BigDecimal::of(1)->dividedBy('8', 2, RoundingMode::HALF_UP); // 0.13
If you know that the division yields a finite number of decimals places, you can use
exactlyDividedBy(), 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)->exactlyDividedBy(256); // 0.00390625 echo BigDecimal::of(1)->exactlyDividedBy(11); // RoundingNecessaryException
The result of the division of a
BigRationalcan always be represented exactly:
echo BigRational::of('123/456')->dividedBy('7'); // 123/3192 echo BigRational::of('123/456')->dividedBy('9/8'); // 984/4104
BigIntegersupports bitwise operations:
and bit shifting:
BigRationalcan be safely serialized on a machine and unserialized on another, even if these machines do not share the same set of PHP extensions.
For example, serializing on a machine with GMP support and unserializing on a machine that does not have this extension installed will still work as expected.
Available as part of the Tidelift Subscription.
The maintainers of brick/math and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.