- How do I install brick/math in a Laravel project?
- Run `composer require brick/math` in your Laravel project directory. The package auto-detects PHP 8.2+ and integrates seamlessly with Laravel’s dependency injection. For older PHP versions (7.1–8.1), pin to a legacy release like `0.13` or `0.11`.
- Will brick/math work in production if GMP/BCMath extensions are missing?
- Yes, but performance drops significantly (~100x slower). The library falls back to pure-PHP calculations. Test critical paths in staging first. For production, ensure GMP/BCMath is installed or accept the trade-off for edge cases like cryptography or high-precision finance.
- Can I use brick/math for exact monetary calculations in Laravel?
- Absolutely. Replace `float` or `double` with `BigDecimal` to avoid rounding errors (e.g., `0.1 + 0.2 ≠ 0.3`). Use `BigDecimal::of('19.99')` for input and methods like `multipliedBy()` for operations. Store values as strings or JSON in the database if needed.
- How do I integrate brick/math with Laravel Eloquent models?
- Add accessors/mutators to your model. For example, use `getAmountAttribute()` to convert a `BigDecimal` to a string for storage, and `setAmountAttribute()` to parse it back. For raw queries, cast results with `BigDecimal::of($row->amount)`. Avoid storing as native `float` types.
- What Laravel versions support brick/math, and are there breaking changes?
- Laravel 9+ (PHP 8.2+) is fully supported. Breaking changes are versioned as `0.x.0` (e.g., `0.17.0`). Lock to a patch version like `^0.17` to avoid surprises. Check the [release history](https://github.com/brick/math/releases) for deprecated methods before upgrading.
- How do I test brick/math in Laravel for edge cases like division by zero?
- Use PHPUnit to test `BigDecimal`/`BigInteger` operations. For example, verify `BigDecimal::of('1')->dividedBy(0)` throws `DivisionByZeroException`. Mock dependencies in Laravel’s service container if needed. The library includes comprehensive tests; extend them for domain-specific edge cases.
- Is brick/math thread-safe for Laravel’s queue workers or cache systems?
- Yes, `BigNumber` objects are immutable and thread-safe. However, serialize/deserialize carefully in Laravel’s cache or queue systems (e.g., `json_encode()`/`json_decode()`). Test with large numbers to ensure no precision loss during serialization.
- What’s the performance impact of brick/math vs. native PHP floats?
- With GMP/BCMath, performance is comparable to native operations for most use cases. Without extensions, pure-PHP mode is slower (~100x for large numbers). Benchmark critical paths (e.g., financial calculations) in staging before production deployment.
- Can I use brick/math alongside other math libraries like moneyphp/money?
- Yes, but avoid mixing types (e.g., don’t pass a `Money` object to `BigDecimal`). Use `BigDecimal` for raw precision and `moneyphp/money` for currency formatting. For example, convert `BigDecimal` to `Money` using `Money::of($amount, $currency)`.
- How do I migrate from bcmath() or GMP functions to brick/math in Laravel?
- Replace functions like `bcdiv()` with `BigDecimal::dividedBy()`. For example, `bcdiv('1.5', '2', 2)` becomes `BigDecimal::of('1.5')->dividedBy('2')->rounded(2)`. Audit your codebase for floating-point operations first, then migrate incrementally by wrapping critical calculations.