Download the PHP package fab2s/math without Composer
On this page you can find all versions of the php package fab2s/math. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package math
Math
A fluent, high-precision arithmetic library for PHP built on bcmath. Designed for financial calculations, scientific computing, and anywhere floating-point errors are unacceptable.
The Problem
Floating-point arithmetic has well-known precision limitations:
bcmathsupports numbers of any size and precision up to 2,147,483,647 decimals, represented as strings.
Installation
Requirements
- PHP 8.2+
- ext-bcmath (required)
- ext-gmp (optional, faster base conversions, mod, pow and powMod)
Features
Fluent API
Chain operations naturally with variadic argument support:
Math is immutable — every operation returns a new instance, leaving the original unchanged:
The overhead is a single clone per operation (two properties: a string and an int).
Mutable Variant
For performance-sensitive hot loops, MathMutable modifies the instance in place:
MathMutable extends Math, so it is accepted anywhere Math is type-hinted.
Strict Validation
Math rejects ambiguous inputs that bcmath would silently convert to 0:
Full Arithmetic Operations
Comparisons & Inspection
Base Conversion (2-62)
Uses GMP when available for faster conversions:
Formatting
Formatting does not mutate the internal number:
Precision Control
Default precision is 9 decimal places. Control it globally or per-instance:
Precision is not handled via
bcscale()to avoid global state issues in long-running processes.
Normalized Output
Results are automatically normalized for accurate comparisons:
Instance Reuse
Pass Math instances directly to avoid re-validation:
Laravel Integration
Cast Eloquent model attributes to Math instances:
Mutable Cast
Use MathMutableCast to get MathMutable instances instead of immutable Math:
Using separate cast classes enables proper static type resolution — Larastan/PHPStan will resolve MathCast properties to Math and MathMutableCast properties to MathMutable.
Upgrading from v2
In v2, Math was mutable, so MathCast attributes behaved as mutable values. In v3, Math is immutable by default — existing code that mutates cast attributes in place will silently lose changes:
To restore the previous behavior, switch to MathMutableCast:
API Reference
Factory Methods
| Method | Description |
|---|---|
Math::number($n) |
Create immutable instance |
Math::make($n) |
Alias for number() |
Math::fromBase($n, $base) |
Create from base 2-62 |
MathMutable::number($n) |
Create mutable instance |
MathMutable::make($n) |
Alias for number() |
MathMutable::fromBase($n, $base) |
Create mutable from base 2-62 |
Arithmetic
| Method | Description |
|---|---|
add(...$n) |
Addition |
sub(...$n) |
Subtraction |
mul(...$n) |
Multiplication |
div(...$n) |
Division |
quotientAndRemainder($n) |
Returns [$quotient, $remainder] |
mod($n) |
Modulo |
pow($n) |
Power |
powMod($exp, $mod) |
Modular exponentiation |
sqrt() |
Square root |
abs() |
Absolute value |
negate() |
Flip sign |
clamp($min, $max) |
Clip between bounds |
Rounding
| Method | Description |
|---|---|
round($precision) |
Round to precision |
floor() |
Round down |
ceil() |
Round up |
Comparison & Inspection
| Method | Description |
|---|---|
eq($n) |
Equal |
gt($n) |
Greater than |
gte($n) |
Greater than or equal |
lt($n) |
Less than |
lte($n) |
Less than or equal |
min(...$n) |
Minimum value |
max(...$n) |
Maximum value |
isZero() |
Check if zero |
isPositive() |
Check if positive |
isNegative() |
Check if negative |
isEven() |
Check if even integer |
isOdd() |
Check if odd integer |
Conversion & Output
| Method | Description |
|---|---|
toBase($base) |
Convert to base 2-62 |
format($dec, $point, $sep) |
Format with separators |
getNumber() |
Get raw (non-normalized) number |
getScale() |
Number of decimal places |
getIntegralPart() |
Part before the decimal point |
getFractionalPart() |
Part after the decimal point |
(string) |
Get normalized number |
Precision
| Method | Description |
|---|---|
setPrecision($p) |
Set instance precision |
getPrecision() |
Get instance precision |
Math::setGlobalPrecision($p) |
Set default for new instances |
Math::getGlobalPrecision() |
Get global precision |
Benchmarks
Compared against brick/math (PHP 8.5, opcache off, GMP enabled). The bold value is the faster one in each row, and Factor shows how many times faster it is.
| Operation | fab2s/math | brick/math | Factor |
|---|---|---|---|
| instantiate int | 0.117μs (±15.1%) | 0.134μs (±8.7%) | 1.14x |
| instantiate string | 0.107μs (±2.4%) | 0.351μs (±1.5%) | 3.29x |
| add | 0.343μs (±0.8%) | 1.177μs (±1.7%) | 3.43x |
| add variadic | 0.674μs (±2.4%) | 3.261μs (±1.3%) | 4.84x |
| sub | 0.345μs (±4.7%) | 1.155μs (±0.5%) | 3.35x |
| mul | 0.382μs (±2.2%) | 1.162μs (±1.6%) | 3.04x |
| div | 0.365μs (±6.5%) | 2.055μs (±5.6%) | 5.63x |
| pow | 0.406μs (±24.3%) | 0.728μs (±0.8%) | 1.79x |
| mod | 0.416μs (±0.8%) | 1.270μs (±7.8%) | 3.06x |
| sqrt | 1.123μs (±0.9%) | 1.380μs (±1.3%) | 1.23x |
| abs | 0.185μs (±0.6%) | 0.517μs (±1.1%) | 2.80x |
| negate | 0.215μs (±1.8%) | 0.466μs (±1.0%) | 2.17x |
| clamp | 0.478μs (±0.7%) | 2.117μs (±5.6%) | 4.43x |
| quotient & remainder | 0.453μs (±0.9%) | 1.306μs (±0.8%) | 2.89x |
| inspection | 1.065μs (±3.1%) | 1.460μs (±2.5%) | 1.37x |
| round | 0.342μs (±3.3%) | 1.483μs (±1.1%) | 4.33x |
| ceil | 0.320μs (±0.5%) | 1.298μs (±3.1%) | 4.06x |
| floor | 0.289μs (±1.9%) | 1.174μs (±1.2%) | 4.06x |
| comparisons | 0.644μs (±1.5%) | 2.777μs (±1.2%) | 4.31x |
| to string | 0.326μs (±1.6%) | 0.511μs (±0.5%) | 1.57x |
| chained workflow | 0.965μs (±1.2%) | 3.683μs (±0.9%) | 3.82x |
| large number ops | 0.903μs (±1.7%) | 3.433μs (±1.1%) | 3.80x |
| accumulate 100 additions | 21.421μs (±1.8%) | 73.261μs (±1.2%) | 3.42x |
| base convert to 62 | 0.615μs (±1.3%) | 2.580μs (±1.5%) | 4.19x |
| base convert to 16 | 0.607μs (±1.3%) | 0.458μs (±1.9%) | 0.75x |
| integer mul | 0.424μs (±1.4%) | 1.014μs (±11.0%) | 2.39x |
| integer powmod | 0.551μs (±0.7%) | 1.410μs (±1.3%) | 2.56x |
| create 1000 instances | 127.530μs (±1.8%) | 385.754μs (±1.1%) | 3.02x |
All operations above use immutable Math (the default). fab2s/math wins every operation except base-16 conversion, where brick/math delegates to GMP's native hex output. The speed advantage comes from keeping bcmath's C-level string arithmetic as the hot path for decimal operations, while brick/math pays for an extra object-wrapping layer on top of GMP. Integer-only operations (mod, pow, powMod, base conversion) use GMP directly when the extension is available, combining the best of both backends. Realistic workflows like chained calculations or 100-iteration accumulations show a consistent 3-5x advantage, with immutability costing only a lightweight clone per operation (two properties: a string and an int).
MathMutable eliminates the clone overhead entirely for hot loops:
| Operation | MathMutable | Math (immutable) | brick/math |
|---|---|---|---|
| chained workflow | 1.018μs (±0.2%) | 1.189μs (±3.0%) | 5.352μs (±1.2%) |
| accumulate 100 | 17.499μs (±1.1%) | 20.765μs (±2.3%) | 74.043μs (±0.5%) |
| branch | 1.457μs (±6.2%) | 1.388μs (±2.7%) | 5.596μs (±1.1%) |
Run benchmarks yourself:
Compatibility
| PHP | Laravel |
|---|---|
| 8.1 | 10 |
| 8.2 | 10, 11, 12 |
| 8.3 | 10, 11, 12 |
| 8.4 | 10, 11, 12 |
Related
Math is also included in OpinHelpers, a collection of utilities for common PHP challenges.
Contributing
Contributions are welcome. Please open issues and submit pull requests.
License
Math is open-source software licensed under the MIT license.