- How do I install brick/math in a Laravel application?
- Install it with Composer using composer require brick/math. No Laravel service provider or configuration is required because the package is framework-agnostic. Composer autoloading makes the Brick\Math classes available anywhere in your application.
- Does brick/math support Laravel and which PHP version do I need?
- brick/math is not Laravel-specific, but it integrates cleanly with Laravel through Composer. The current release requires PHP 8.2 or later. Older package versions support PHP 8.1, 8.0, and 7.4, so choose a version that matches your Laravel application's PHP runtime.
- What is brick/math used for in Laravel projects?
- It provides arbitrary-precision arithmetic for big integers, decimals, and rational numbers. Laravel developers commonly use it for financial calculations, cryptography, scientific computing, and any domain where floating-point inaccuracies are unacceptable. It fits well in service layers, model accessors, Artisan commands, and API responses.
- Do I need the GMP or BCMath PHP extension for brick/math?
- No, the library works without GMP or BCMath by falling back to pure-PHP implementations. However, installing GMP or BCMath is highly recommended for performance. The fastest available calculator implementation is selected automatically at runtime.
- How do I replace float calculations with BigDecimal in Laravel?
- Start with a proof of concept on one critical financial calculation and compare results against your existing logic. Avoid passing floats directly because brick/math rejects them due to precision loss; use string conversions or BigDecimal::fromFloatExact() carefully. Then adopt incrementally, ideally wrapping calculations in value objects or DTOs.
- Is brick/math fast enough for production Laravel workloads?
- Performance depends on whether GMP or BCMath is available and on the volume of operations. With GMP or BCMath it is suitable for many production workloads, while the pure-PHP fallback may add latency in high-volume or batch-processing paths. Benchmark your critical paths before integrating.
- How does brick/math handle rounding and division precision?
- brick/math requires explicit rounding modes such as RoundingMode::HalfUp for many operations, which differs from native PHP behavior. Division in newer versions requires a scale parameter, for example with dividedBy(). You should define a clear rounding policy and validate edge cases such as 1/3 division.
- Can I serialize BigDecimal or BigInteger to JSON in Laravel API responses?
- BigNumber classes provide native jsonSerialize() support, so they can be returned directly in Laravel API responses. You can also cast them to strings for a simpler wire format. For model attributes, store values as strings and use accessors and mutators to convert them.
- How should I test brick/math usage in Laravel unit tests?
- Focus tests on precision edge cases, rounding behavior, and MathExceptions. Because BigNumber objects are immutable value objects, you can often use real instances instead of mocks. If you wrap calculations in services, inject those services and assert exact string or decimal results.
- What are the alternatives to brick/math and is it maintained?
- Alternatives include calling GMP or BCMath directly, using MoneyPHP for currency-specific work, or choosing another arbitrary-precision library. brick/math is well tested with 100% coverage and is considered stable for production despite its 0.x versioning. Breaking changes start new 0.x release cycles, so lock to a release cycle such as ^0.19.