float64) are unacceptable. Directly leverages PHP 8.4’s BCMath\Number for arbitrary-precision arithmetic.$product->price * $quantity).BCMath\Number objects and database storage (e.g., DECIMAL/NUMERIC columns). Assumes database supports precise decimal storage.numeric|decimal:2) for input sanitization.BCMath\Number operations are slower than native floats. Benchmark critical paths (e.g., bulk calculations) to validate trade-offs.BCMath\Number to avoid silent truncation."123.45"), which could break APIs expecting numeric types. Explicit type hints or custom JSON encoders may be needed.BCMath\Number is stateless).0.1 + 0.2 = 0.30000000000000004) currently causing issues?DECIMAL(10,2)) already aligned with precision needs?float, int) or only high-precision fields?min:0, max:100)?BCMath extensions are disabled on the server?DECIMAL/NUMERIC columns (MySQL, PostgreSQL, SQLite). Avoid FLOAT/DOUBLE columns.BCMath\Number → string conversion).spatie/laravel-data for DTOs or beberlei/assert for runtime validation.league/flysystem if storing large decimal values in files.BCMath extension is enabled (php -m | grep bcmath).BCMath\Number precision (e.g., DECIMAL(20,4)).protected $casts = ['price' => 'float'] with:
use YuWuHsien\Decimal\Casts\AsDecimal;
protected $casts = ['price' => AsDecimal::class];
where, orderBy, or aggregations (e.g., sum) to handle BCMath\Number objects.BCMath\Number objects:
// In a resource/transformer
public function toArray($request) {
return [
'price' => (string) $this->price, // or custom formatting
];
}
$model->price * 2).float/int casts will break without updates.BCMath\Number objects are stored directly.float if BCMath is unavailable:
protected $casts = [
'price' => \YuWuHsien\Decimal\Casts\AsDecimal::class,
// Fallback for non-BCMath environments
// 'price' => function ($value) {
// return is_string($value) ? (float) $value : $value;
// },
];
BCMath functionality.Product with price).FinancialTransaction).BCMath\Number API. Future PHP versions may introduce breaking changes.BCMath operations for observability:
try {
$total = $model->price * $quantity;
} catch (ArithmeticError $e) {
Log::error("BCMath error: {$e->getMessage()}");
}
BCMath precision.BCMath\Number objects to clients expecting float.BCMath operations in where clauses.BCMath extension issues.BCMath limitations (e.g., no native support for NaN/Infinity).BCMath\Number operations are ~10–100x slower than native floats. Benchmark critical paths (e.g., batch processing).BCMath in where/orderBy clauses if possible (push logic to application layer).BCMath calculations (e.g., precompute tax totals).BCMath enabled.BCMath becomes a bottleneck.| Failure Scenario | Impact | Mitigation |
|---|---|---|
BCMath extension disabled |
Casts fail silently or throw errors | Feature flag + fallback to float. |
| Database column precision mismatch | Silent data corruption | Add database migrations to align precision. |
| Arithmetic overflow |
How can I help you explore Laravel packages today?