Brick\Math
Arbitrary-precision arithmetic for PHP with immutable big integer, decimal, and rational numbers.
Repository Health
Technical Analysis
Brick\Math is a PHP library for working with numbers of arbitrary size and precision, free from the rounding errors and overflow limits of native PHP int and float types. It provides immutable BigInteger, BigDecimal, and BigRational value objects with a rich, fluent arithmetic API.
Under the hood it automatically selects the fastest available calculator, using the GMP or BCMath extension when present and falling back to a pure-PHP implementation otherwise. With tens of millions of monthly installs, it is a foundational dependency for financial, scientific, and cryptographic PHP code that cannot tolerate floating-point imprecision.
What You Get
- Immutable
BigInteger,BigDecimal, andBigRationalclasses with a fluent arithmetic API - Exact division with configurable scale and rounding modes, plus rational (fraction) arithmetic
- Automatic selection of the fastest backend (GMP, BCMath, or pure PHP) at runtime
- Rich comparison, conversion, and formatting helpers with strict type safety
- Dedicated exception types for division-by-zero, rounding, and number-format errors
Common Use Cases
- Handling monetary amounts and financial calculations without floating-point drift
- Performing exact decimal math with controlled scale and rounding
- Computing with very large integers beyond PHP’s native int range
- Working with exact fractions via rational numbers
Under The Hood
Architecture - The public API lives in src/ as BigNumber (an abstract base) with concrete BigInteger, BigDecimal, and BigRational subclasses, all immutable. Actual arithmetic is delegated to swappable calculator implementations under src/Internal/ (a GMP calculator, a BCMath calculator, and a pure-PHP Calculator fallback); the fastest available one is detected and cached at runtime. Rounding behavior is centralized in RoundingMode, and error conditions raise dedicated types under src/Exception/.
Tech Stack - Pure PHP requiring PHP 8.2+, optionally accelerated by the GMP or BCMath extensions. It is distributed via Composer with PSR-4 autoloading under the Brick\Math\ namespace and has no production dependencies.
Code Quality - The project is rigorously maintained: PHPUnit tests in tests/, PHPStan static analysis (phpstan.neon), Codecov coverage, and a large release history (76+ tags) reflect strong discipline. Naming is consistent and every value type is strictly typed and immutable.
API Design - The fluent, immutable API reads naturally: BigDecimal::of('1.1')->plus('2.2')->multipliedBy(3). Division forces an explicit scale and rounding mode, surfacing precision decisions rather than hiding them, which trades a little verbosity for correctness.