← All docs

BCMath

Arbitrary-precision decimal arithmetic: the 14 PHP bcmath functions.

elephc implements PHP’s 14 procedural bcmath functions through a pure-Rust decimal bridge. Operations use base-10 digits directly, so values are not converted through binary floating point and result strings preserve the requested number of fractional digits.

Functions

FunctionSignatureResult
bcadd()bcadd(string $num1, string $num2, ?int $scale = null): stringSum
bcsub()bcsub(string $num1, string $num2, ?int $scale = null): stringDifference
bcmul()bcmul(string $num1, string $num2, ?int $scale = null): stringProduct
bcdiv()bcdiv(string $num1, string $num2, ?int $scale = null): stringQuotient
bcmod()bcmod(string $num1, string $num2, ?int $scale = null): stringRemainder
bcdivmod()bcdivmod(string $num1, string $num2, ?int $scale = null): arrayIndexed [quotient, remainder] pair
bcpow()bcpow(string $num, string $exponent, ?int $scale = null): stringIntegral power
bcpowmod()bcpowmod(string $num, string $exponent, string $modulus, ?int $scale = null): stringIntegral modular power
bcsqrt()bcsqrt(string $num, ?int $scale = null): stringSquare root
bccomp()bccomp(string $num1, string $num2, ?int $scale = null): int-1, 0, or 1
bcscale()bcscale(?int $scale = null): intCurrent scale, or previous scale when setting
bcceil()bcceil(string $num): stringLeast integer greater than or equal to the number
bcfloor()bcfloor(string $num): stringGreatest integer less than or equal to the number
bcround()bcround(string $num, int $precision = 0, int $mode = 1): stringRounded decimal string

The four PHP 8.4 additions—bcceil(), bcfloor(), bcround(), and bcdivmod()—are available in every elephc PHP-version profile, like other registry builtins.

Scale and formatting

The process scale starts at 0. Omitting $scale, or passing null, reads the current value set by bcscale(); an explicit 0 always means zero fractional digits. Setting the scale returns its previous value.

echo bcscale(4);                 // 0
echo bcadd('1.234', '5');        // 6.2340
echo bcadd('1.234', '5', 0);     // 6

bcadd(), bcsub(), bcmul(), bcdiv(), and bcmod() truncate to the selected scale; they do not round. Only bcround(), bcceil(), and bcfloor() apply rounding. Output is normalized without a leading + or negative zero, and is padded with trailing zeros to the selected scale.

bcround() accepts the same integer mode values as elephc’s round(): 1 through 8, with 1 (PHP_ROUND_HALF_UP / RoundingMode::HalfAwayFromZero) as the default.

Accepted numbers and errors

Numeric strings are scanned verbatim and may contain an optional sign plus decimal digits with an optional point. Forms such as .5, 5., and +1.20 are valid. PHP also normalizes digitless forms such as "", +, -, ., +., and -. to zero. Surrounding whitespace, exponent notation such as 1e2, multiple decimal points, and other non-numeric text are not accepted.

Malformed numbers, negative or out-of-range scales, invalid powers or square roots, and unsupported rounding modes throw a catchable ValueError. Division or modulo by zero, including a negative power of zero, throws a catchable DivisionByZeroError. Error messages retain PHP function and argument names.

Linking and eval

Using any bc* function auto-links libelephc_bcmath. Use --with-bcmath to force-link the bridge when calls are reached only through indirection:

elephc --with-bcmath app.php

extension_loaded('bcmath') reports true when the AOT binary links the bridge. Dynamic eval() also exposes all 14 functions and shares the same process scale with surrounding AOT code.

Current scope

The procedural PHP 8.4 surface is supported. BcMath\Number, decimal operator overloading, and the bcmath.scale INI directive are not implemented; bcscale() is the supported process-scale interface.

Functions {#functions}

Generated from the shared symbol catalog by scripts/docs/gen_module_sections.py; do not edit this section by hand. Each function links to its reference page.

FunctionSignatureReturnsAOTeval()
bcadd()(string $num1, string $num2, ?int $scale = null): stringstring
bcceil()(string $num): stringstring
bccomp()(string $num1, string $num2, ?int $scale = null): intint
bcdiv()(string $num1, string $num2, ?int $scale = null): stringstring
bcdivmod()(string $num1, string $num2, ?int $scale = null): arrayarray
bcfloor()(string $num): stringstring
bcmod()(string $num1, string $num2, ?int $scale = null): stringstring
bcmul()(string $num1, string $num2, ?int $scale = null): stringstring
bcpow()(string $num, string $exponent, ?int $scale = null): stringstring
bcpowmod()(string $num, string $exponent, string $modulus, ?int $scale = null): stringstring
bcround()(string $num, int $precision = 0, int $mode = 1): stringstring
bcscale()(?int $scale = null): intint
bcsqrt()(string $num, ?int $scale = null): stringstring
bcsub()(string $num1, string $num2, ?int $scale = null): stringstring