Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Math Laravel Package

brick/math

Arbitrary-precision math for PHP. Work with big integers, decimals and rationals reliably, with automatic acceleration via GMP or BCMath when available. PHP 8.2+ supported. Stable 0.x release cycles suitable for production.

View on GitHub
Deep Wiki
Context7

brick/math is a PHP library for working with arbitrary-precision numbers, enabling safe calculations beyond native integer/float limits. It’s production-ready, well-tested, and targets modern PHP (8.2+), with older release lines available for legacy runtimes.

It auto-selects the fastest available backend at runtime and integrates smoothly into any codebase via Composer, with optional extensions to boost performance.

  • Arbitrary precision integers, decimals, and rational math (via Brick\Math types)
  • Automatic runtime optimization using GMP or BCMath when available
  • Predictable versioning: safe to pin a release cycle like ^0.17
  • Thoroughly tested with CI and coverage reporting
  • Clean, object-oriented API designed for correctness and precision
Frequently asked questions about Math
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.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport