A PHP library for arbitrary-precision arithmetic with support for integers, decimals, and rational numbers.
Brick\Math is a PHP library that provides arbitrary-precision arithmetic, enabling accurate calculations with numbers of any size without the limitations of native PHP types. It solves the problem of handling large integers, precise decimals, and rational numbers (fractions) in PHP, where native types like int and float have size and precision constraints. The library emphasizes reliability, performance, and immutability, automatically leveraging extensions like GMP or BCMath when available for faster operations.
PHP developers working on applications requiring precise mathematical calculations, such as financial systems, scientific computing, cryptography, or any scenario where handling very large numbers or exact decimal representations is critical. It is also suitable for developers needing to avoid floating-point inaccuracies or integer overflow issues in PHP.
Developers choose Brick\Math over alternatives because it offers a comprehensive, type-safe, and immutable API for arbitrary-precision integers, decimals, and rational numbers in one library, with automatic performance optimization via GMP or BCMath. Its focus on exception safety, flexible rounding modes, and seamless serialization across different PHP environments provides a robust solution for precise arithmetic without external dependencies beyond optional extensions.
Arbitrary-precision arithmetic library for PHP
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Automatically leverages GMP or BCMath extensions when available for faster calculations, as mentioned in the README, without requiring manual configuration.
All number objects are immutable, enabling safe method chaining and predictable behavior, which prevents side effects in complex calculations, as highlighted in the overview.
Supports arbitrary-precision integers, decimals, and rational numbers in one library with classes like BigInteger, BigDecimal, and BigRational, covering diverse precision needs from financial to scientific applications.
Provides a unified MathException interface and specific exception classes, allowing fine-grained error handling for cases like division by zero or rounding errors, as detailed in the exceptions section.
As a 0.x release, breaking changes are introduced in new version cycles, requiring careful version locking and upgrade planning, which can disrupt production environments, as noted in the project status.
Calculations must be performed through library methods like plus() and multipliedBy(), not using PHP's standard arithmetic operators, making code less intuitive and requiring a learning curve for developers accustomed to native syntax.
Direct instantiation from floats is avoided due to precision loss; developers must use dedicated methods like fromFloatExact() or fromFloatShortest(), adding complexity and potential for errors when working with float-based data.