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).
bcmath with a robust, maintained library.bcmath/gmp calls in core financial logic (e.g., order matching, fee calculations).Adopt if:
float/int types are insufficient.bcmath/gmp extension inconsistencies).$total->plus($tax)->dividedBy(100)).Look elsewhere if:
bcmath).PHP_INT_MAX (use GMP directly).GMP/BCMath)."This library solves a critical gap in PHP’s math capabilities—precision errors cost us money and reputation. For example, in our crypto wallet, a 0.0001 BTC miscalculation due to floating-point rounding could lead to customer disputes or regulatory fines. Brick\Math ensures exact decimal arithmetic, just like Java’s BigDecimal, but natively in PHP. It’s MIT-licensed, battle-tested (2K+ stars), and maintained by the Brick team—no vendor lock-in. The ROI? Fewer bugs in financial systems, faster compliance audits, and the ability to handle edge cases (like 100! or π to 1000 digits) without custom code. We’re not just fixing a bug; we’re future-proofing our math."
*"Brick\Math gives us three superpowers:
bcmath extension headaches or floating-point quirks. Works the same on every server.$total->plus($tax)->dividedBy(100)) and proper error handling (e.g., RoundingNecessaryException).GMP/BCMath if available, falls back to pure PHP).Migration path:
BigDecimal for financial logic (replace number_format()/bcadd()).BigInteger for cryptographic hashes or large IDs.float inputs with BigDecimal::fromFloatExact() to avoid silent precision loss.Tradeoffs:
Let’s prototype this in the payment service next sprint—it’s a drop-in replacement for our ad-hoc bcmath calls and will save us from floating-point bugs forever."*
How can I help you explore Laravel packages today?