brick/math
brick/math is a PHP library for arbitrary precision arithmetic. It provides BigInteger, BigDecimal, BigRational, and BigNumber classes, automatically using GMP or BCMath when available for fast, exact calculations.
brick/math is a framework-agnostic utility library, not a Laravel package. It has no service provider, configuration, migrations, or HTTP surface, so it fits as a domain-level dependency rather than an application subsystem. It is best leveraged for bounded, precision-sensitive calculations: monetary proration, tax and FX conversion, metering, quotas, loyalty balances, scientific quantities, and large-integer identifiers. It should not replace database numeric types or become a general-purpose ORM abstraction.0.x cycle is pinned. GMP or BCMath is strongly recommended for production performance; the library falls back to pure PHP automatically, but that fallback has materially different throughput characteristics.0.x cycle boundaries, so version pinning is a hard requirement rather than a best practice. The most important product-risk area is untrusted numeric input: of() is documented for trusted input, and an input such as 1e1000000000 can expand memory usage dramatically. Any user-supplied number must go through parse() with explicit syntax and maxDigits constraints. Additional risks include inconsistent rounding policy across teams, accidental float conversion at boundaries, and performance regressions in high-volume paths if GMP/BCMath is absent.brick/money for currency-aware behavior? What volume and latency envelope must precision-safe calculations meet?BigInteger/BigDecimal calls scattered across controllers, models, and jobs will create inconsistent behavior.BigDecimal or BigRational, and preserve legacy outputs through shadow calculations before cutover. For persistence, map database DECIMAL/NUMERIC columns or canonical strings into BigDecimal via custom Eloquent casts, and emit strings—not floats—through API resources. For untrusted input, standardize a parse()-based validator before any arithmetic method is called.0.13, PHP 8.0 can use 0.11, PHP 7.4 can use 0.10, and PHP 7.1–7.3 can use 0.9. Ensure GMP or BCMath is installed consistently in Docker images and CI. The library's serialization is designed to remain portable across machines with different extensions, but application-level serialization—JSON, queues, cache—should still use explicit string representations rather than relying on implicit object behavior.0.x release model. Lock to a specific cycle, monitor changelogs and releases, and schedule upgrades as planned work rather than allowing automatic minor-version drift. MIT licensing keeps legal and distribution overhead low.maxDigits on untrusted input, cap decimal scales, avoid unbounded exponent expansion, and keep large-number objects out of queue payloads where strings suffice. Database precision must match domain scale, or storage will silently undermine the library's guarantees.RoundingNecessaryException when scale is insufficient, DivisionByZeroException, NumberFormatException, and possible memory exhaustion from oversized inputs. Extension differences, float conversion at API or database boundaries, and inconsistent rounding can produce silent financial errors rather than crashes. Catch the common MathException interface at domain boundaries and translate failures into explicit product behavior—especially for money, where failing closed is preferable to approximating.parse() usage, rounding modes, scale ownership, and exception handling. TPM should invest in a short internal standard, shared fixtures, and review checkpoints; the optional PHPStan extension can improve exception-type inference but is third-party and should be evaluated separately before adoption.How can I help you explore Laravel packages today?