mini\Util\Math\Decimal final class

Documentation

Immutable fixed-point decimal with BigInt backing

Stores value as scaled BigInt internally. Scale determines decimal places. Designed to align with SQL DECIMAL semantics for use in Table ColumnDef types.

Precision model:

  • Basic arithmetic (+, -, *, /, %) is exact within the specified scale
  • Transcendental functions (sqrt, exp, ln, pow) use iterative algorithms (Newton-Raphson, Taylor series) computed to configurable precision

Accepts BigInt|int|float|string for arithmetic operations.

Usage: $a = Decimal::of('123.45', 2); // scale 2 $b = Decimal::of('10', 4); // 10.0000, scale 4 $result = $a->add($b)->multiply(2); echo $result; // prints decimal string

Inheritance

Implements: mini\Util\Math\NumberInterface Stringable

Properties (2)

private readonly mini\Util\Math\BigInt $unscaled
private readonly int $scale

Methods (39)

Documentation missing

Create from string/int with specified scale

Create zero with specified scale

Create one with specified scale

Parse string/int with auto-detected scale

Add: result scale = max(this.scale, other.scale)

Subtract: result scale = max(this.scale, other.scale)

Multiply: result scale = this.scale + other.scale

Divide with specified result scale

Modulus: result scale = max(this.scale, other.scale)

Absolute value

Square root using Newton-Raphson iteration

Exponential function e^x using Taylor series

Natural logarithm using Newton-Raphson on exp

Reciprocal: 1/x

Power with arbitrary exponent: x^y = exp(y * ln(x))

Integer power using binary exponentiation

Compare: returns -1 if less, 0 if equal, 1 if greater

High-performance static comparison of decimal strings

Documentation missing

Documentation missing

Documentation missing

Documentation missing

Documentation missing

Documentation missing

Documentation missing

Documentation missing

Get the scale (number of decimal places)

Get the unscaled BigInt value

Change scale with rounding

Convert to string representation

Convert to float (may lose precision)

Documentation missing

Documentation missing

Coerce input to Decimal

Align two decimals to the same scale

Get 10^n as string (for BigInt operations)

Divide with rounding (half up by default)

Source

src/Util/Math/Decimal.php:24-817