Weave 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

brick/math is a PHP library for arbitrary precision arithmetic. It provides BigInteger, BigDecimal, BigRational, and BigNumber classes, automatically using GMP or BCMath when available for fast, exact calculations.

View on GitHub
Deep Wiki
Context7

Brick\Math is a PHP library for arbitrary precision arithmetic, letting you perform exact calculations on numbers too large or too precise for native PHP integers and floats. It requires PHP 8.2+ and automatically selects the fastest available backend—GMP, BCMath, or pure PHP—so it works on any installation.

  • BigInteger, BigDecimal, and BigRational classes for integers, decimals, and fractions
  • Exact, rounding-free math with configurable rounding modes
  • Automatic runtime selection of GMP/BCMath for speed
  • Rich API: arithmetic, comparisons, powers, square roots, and conversions

Well tested and stable for production. Install via Composer: composer require brick/math.

Frequently asked questions about Math
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.
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.
jinas/dhivatar
store-doc/laravel-storage-driver
plus-it/laravel-translation-manager
diephp/perhaps
diephp/telegraph
osmaviation/spreadsheet
microsoft/fluentui-system-icons
mmccook/php-json-canonicalization-scheme
middlewares/firewall
qubiqx/montapacking
nyoncode/laravel-package-console
pnscripts/filament-broadquill-lite
pnscripts/laravel-sentfold-lite
vleap/mx-sdk-laravel-internal
twidpay/utils
htetmyathlaing/scout-elasticsearch-driver
artisan-toolbox/front-awesome
ls/client-assistant
users-au/laravel-client
cgv/localized-resource