brick/math
Arbitrary-precision math for PHP. Work with big integers, decimals, and rational numbers via a clean OOP API. Optimized with GMP or BCMath when available, with automatic runtime selection. Requires PHP 8.2+ (older versions available).
Installation:
composer require brick/math
Ensure PHP 8.2+ is used (or downgrade for older versions if needed).
First Use Case:
use Brick\Math\BigInteger;
use Brick\Math\BigDecimal;
// Arbitrary-precision integer
$bigInt = BigInteger::of('9999999999999999999999999999999999999999999');
// Arbitrary-precision decimal
$bigDec = BigDecimal::of('9.99999999999999999999999999999999999999999999');
// Perform operations
echo $bigInt->plus(1); // 100000000000000000000000000000000000000000000
echo $bigDec->multipliedBy(2); // 19.99999999999999999999999999999999999999999998
Key Files to Reference:
$amount = BigDecimal::of('100.00');
$taxRate = BigDecimal::of('0.075');
$tax = $amount->multipliedBy($taxRate);
$total = $amount->plus($tax);
0.1 + 0.2 !== 0.3 in PHP).$id = BigInteger::of('9999999999999999999999999999999999999999999');
$nextId = $id->plus(1); // No overflow
BigInteger::randomBits(256) for cryptographic randomness.$fraction = BigRational::of('2/3');
$result = $fraction->dividedBy('7'); // 2/21 (exact)
$price = BigDecimal::of('1.23456');
$rounded = $price->setScale(2, RoundingMode::HalfEven); // 1.23
setScale(), dividedBy() with RoundingMode.$result = BigInteger::of(10)
->multipliedBy(2)
->plus(5)
->dividedBy(3, RoundingMode::Down);
Brick\Math to avoid NumberFormatException:
if (!ctype_digit($input)) {
throw new InvalidArgumentException('Input must be a digit string.');
}
$number = BigInteger::of($input);
BigInteger/BigDecimal as strings in databases (e.g., PostgreSQL numeric or bigint):
$model->precision_value = $bigDec->toString();
$bigDec = BigDecimal::of($model->precision_value);
json_encode()/json_decode() for API responses:
$data = ['total' => $bigDec->jsonSerialize()];
BigNumber implements JsonSerializable and __toString().pecl install gmp bcmath
float: Never pass float directly to of(); use BigDecimal::fromFloatExact() instead.BigDecimal::fromFloatExact() to test edge cases:
$floatValue = 0.1 + 0.2; // 0.30000000000000004
$exact = BigDecimal::fromFloatExact($floatValue);
$this->assertEquals('0.30000000000000004', $exact->toString());
float to of() throws InvalidArgumentException (since PHP 0.15).BigDecimal::fromFloatExact() or cast to string:
// Bad (throws exception)
BigDecimal::of(0.1);
// Good
BigDecimal::of((string) 0.1); // or
BigDecimal::fromFloatExact(0.1);
BigInteger::dividedBy() throws RoundingNecessaryException for non-integer results.BigDecimal::dividedBy() requires a scale parameter.dividedByExact() for exact divisions or specify rounding modes:
// BigInteger (exact or round)
$result = BigInteger::of(10)->dividedBy(3, RoundingMode::Down); // 3
// BigDecimal (with scale)
$result = BigDecimal::of(1)->dividedBy(3, 2, RoundingMode::HalfUp); // 0.33
BigInteger bitwise ops (and(), or(), etc.) require non-negative operands.$a = BigInteger::of(-5);
$b = BigInteger::of(3);
$maskedA = $a->abs()->and($b->abs()); // Workaround
BigInteger::getLowestSetBit() returns null for zero (not -1).BigDecimal::getPrecision() returns 1 for zero.if ($number->isZero()) {
// Handle zero case
}
toString() for storage or JSON serialization:
$serialized = $bigDec->toString();
$unserialized = BigDecimal::of($serialized);
sqrt() changed in 0.15 (now RoundingMode::Unnecessary).$sqrt = BigDecimal::of(2)->sqrt(RoundingMode::Down
How can I help you explore Laravel packages today?