moontoast/math
PHP math utilities for Laravel and general use, offering convenient helpers for precise calculations, percentages, rounding, and numeric formatting. Lightweight and easy to integrate into apps where consistent arithmetic and number handling matter.
Installation
composer require moontoast/math
No additional configuration is required—just autoload the package.
First Use Case: Basic Arithmetic
use Math\BigInteger;
use Math\BigFloat;
// Large integer operations
$a = new BigInteger('12345678901234567890');
$b = new BigInteger('98765432109876543210');
$sum = $a->add($b); // BigInteger result
// High-precision floating-point
$x = new BigFloat('1.2345678901234567890');
$y = new BigFloat('0.98765432109876543210');
$product = $x->multiply($y); // BigFloat result
Where to Look First
BigInteger and BigFloat for most use cases.tests/ directory for real-world usage patterns.Large Number Handling
// Avoid PHP's float precision issues
$result = (new BigFloat('0.1'))->add(new BigFloat('0.2')); // Correctly returns 0.3
Integration with Laravel
use Illuminate\Support\Facades\Validator;
$validator = Validator::make($request->all(), [
'amount' => 'required|numeric|min:0',
]);
if ($validator->fails()) {
// Use BigFloat to validate exact amounts
$amount = new BigFloat($request->amount);
if ($amount->compare(new BigFloat('1000.0001')) < 0) {
// Custom logic for precise validation
}
}
Database Storage
BigInteger/BigFloat as strings in DB (e.g., amount column as VARCHAR).protected $casts = [
'amount' => BigFloat::class,
];
Performance Considerations
$cacheKey = 'tax_rate_' . $currency;
$taxRate = Cache::remember($cacheKey, 3600, function () {
return new BigFloat('0.0725'); // 7.25% VAT
});
API Responses
BigFloat/BigInteger as strings in JSON to preserve precision:
return response()->json([
'total' => (string) $order->total,
]);
Type Coercion
BigInteger + string). Always use explicit methods:
// Wrong: $result = $bigInt + '10'; // Throws exception
// Right: $result = $bigInt->add(new BigInteger('10'));
Precision Loss
BigFloat requires a precision parameter (default: 14 digits). Adjust for your needs:
$highPrecision = new BigFloat('1.2345678901234567890', 20);
Memory Usage
// Avoid storing unnecessary intermediates
$final = $a->multiply($b)->divide($c); // Chain operations
Laravel Eloquent Quirks
BigFloat/BigInteger fields from $fillable if not serialized.$results = Model::whereRaw('CAST(amount AS DECIMAL(30,10)) > ?', ['1000.00'])->get();
Comparison Issues
compare() for BigFloat/BigInteger comparisons (not ==):
if ($a->compare($b) === 0) {
// Equal
}
Serialization
$stored = (string) $bigFloat;
$restored = new BigFloat($stored);
Error Handling
MathException for invalid operations (e.g., division by zero):
try {
$result = $a->divide($b);
} catch (MathException $e) {
Log::error('Division error: ' . $e->getMessage());
}
Custom Operations
BigInteger/BigFloat via traits or inheritance:
class CustomBigFloat extends BigFloat {
public function percentageOf(BigFloat $other): BigFloat {
return $this->divide($other)->multiply(new BigFloat('100'));
}
}
Integration with Monolog
BigFloat/BigInteger values directly:
Log::info('Precision value', ['value' => (string) $bigFloat]);
Testing
assertEquals() with BigFloat/BigInteger in PHPUnit:
$this->assertEquals(
new BigFloat('0.3'),
(new BigFloat('0.1'))->add(new BigFloat('0.2'))
);
How can I help you explore Laravel packages today?