bcmod

bcmod(string $num1, string $num2, ?int $scale = null): string

Modulo of arbitrary-precision integers: operands and the result are number strings; a zero modulus throws DivisionByZeroError in PHP 8.

C C23

C23
#include <stdio.h>

/* C has no arbitrary precision: unsigned __int128 (a compiler extension) fits this example's values; printing must be hand-written */
static void print_u128(unsigned __int128 v) {
    char buf[48];
    int i = 0;
    if (v == 0) { putchar('0'); return; }
    while (v > 0) {
        buf[i++] = (char)('0' + (int)(v % 10));
        v /= 10;
    }
    while (i > 0) putchar(buf[--i]);
}

int main(void) {
    /* No huge-integer literal: build digit by character in decimal */
    unsigned __int128 n = 0;
    const char *digits = "123456789012345678901234567890";
    for (const char *p = digits; *p; p++) {
        n = n * 10 + (unsigned)(*p - '0');
    }
    print_u128(n % 97);
    putchar('\n');
    printf("%d\n", 17 % 5);
    /* Integer modulo by zero is undefined behavior: must check first */
    unsigned __int128 m = 0;
    if (m == 0) printf("zero modulus detected: skipped\n");
    return 0;
}
Output
52
2
zero modulus detected: skipped

C++ C++26

C++26
#include <iostream>
#include <string>

// C++ has no arbitrary precision: unsigned __int128 (a compiler extension) fits this example's values; printing needs a hand-written to-string
static std::string to_string_u128(unsigned __int128 v) {
    if (v == 0) return "0";
    std::string s;
    while (v > 0) {
        s += static_cast<char>('0' + static_cast<int>(v % 10));
        v /= 10;
    }
    return std::string(s.rbegin(), s.rend());
}

int main() {
    unsigned __int128 n = 0;
    for (char ch : std::string("123456789012345678901234567890")) {
        n = n * 10 + static_cast<unsigned __int128>(ch - '0');
    }
    std::cout << to_string_u128(n % 97) << "\n";
    std::cout << 17 % 5 << "\n";
    // Integer modulo by zero is undefined behavior: must check first
    unsigned __int128 m = 0;
    if (m == 0) std::cout << "zero modulus detected: skipped\n";
    return 0;
}
Output
52
2
zero modulus detected: skipped

C# v11.0.100

v11.0.100
using System.Numerics;

// BigInteger: arbitrary-precision modulo (non-negative result for positive operands; negative operands use truncation semantics)
var n = BigInteger.Parse("123456789012345678901234567890");
Console.WriteLine(n % 97);
Console.WriteLine(17 % 5);
// A zero modulus throws DivideByZeroException
try { _ = BigInteger.One % BigInteger.Zero; }
catch (DivideByZeroException) { Console.WriteLine("DivideByZeroException caught"); }
Output
52
2
DivideByZeroException caught

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"math/big"
)

// big.Int panics on a zero modulus: wrap in a function with defer/recover to demo the error branch
func modByZero() {
	defer func() {
		if r := recover(); r != nil {
			fmt.Println("panic recovered")
		}
	}()
	var z big.Int
	z.Mod(big.NewInt(1), big.NewInt(0))
}

func main() {
	// int64 % is capped at 64 bits; huge numbers use big.Int.Mod (Euclidean semantics, non-negative result)
	n, _ := new(big.Int).SetString("123456789012345678901234567890", 10)
	fmt.Println(n.Mod(n, big.NewInt(97)))
	fmt.Println(17 % 5)
	modByZero()
}
Output
52
2
panic recovered

Java v27.0.1

v27.0.1
import java.math.BigInteger;

public class Main {
    public static void main(String[] args) {
        // long % is capped at 64 bits; huge numbers use BigInteger.mod (modulus must be positive, non-negative result)
        BigInteger n = new BigInteger("123456789012345678901234567890");
        System.out.println(n.mod(BigInteger.valueOf(97)));
        System.out.println(17 % 5);
        // A zero modulus throws ArithmeticException
        try {
            BigInteger.ONE.mod(BigInteger.ZERO);
        } catch (ArithmeticException e) {
            System.out.println("ArithmeticException caught");
        }
    }
}
Output
52
2
ArithmeticException caught

JavaScript v26.13.1

v26.13.1
// Number % first floats large numbers (losing precision): use BigInt for big-integer modulo
console.log((123456789012345678901234567890n % 97n).toString());
console.log((17n % 5n).toString());
// BigInt throws RangeError on a zero modulus (Number silently returns NaN)
try {
  1n % 0n;
} catch (e) {
  console.log(e.constructor.name + " caught");
}
Output
52
2
RangeError caught

PHP v8.6.0

v8.6.0
// bcmod: arbitrary-precision integer modulo (operands are numeric strings; non-negative result for positive operands)
echo bcmod("123456789012345678901234567890", "97"), "\n";
echo bcmod("17", "5"), "\n";
// Since PHP 8 a zero modulus throws DivisionByZeroError (7.x returned null + a warning)
try {
    bcmod("1", "0");
} catch (DivisionByZeroError) {
    echo "DivisionByZeroError caught";
}
Output
52
2
DivisionByZeroError caught

Python v3.15.2

v3.15.2
# int is arbitrary-precision by nature: % works directly (floor semantics — the result takes the modulus's sign, unlike PHP's truncation)
print(123456789012345678901234567890 % 97)
print(17 % 5)
# A zero modulus raises ZeroDivisionError
try:
    1 % 0
except ZeroDivisionError:
    print("ZeroDivisionError caught")
Output
52
2
ZeroDivisionError caught

Rust v1.99.1

v1.99.1
// No arbitrary precision in the standard library: u128 tops out around 3.4e38, which fits this example (bigger needs the third-party num-bigint crate)
fn main() {
    let n: u128 = 123456789012345678901234567890;
    println!("{}", n % 97);
    println!("{}", 17u128 % 5);
    // Integer modulo by zero panics; checked_rem returns None
    println!("{:?}", 1u128.checked_rem(0));
}
Output
52
2
None

TypeScript v26.13.1

v26.13.1
// Number % first floats large numbers (losing precision): use BigInt for big-integer modulo
const n: bigint = 123456789012345678901234567890n;
console.log((n % 97n).toString());
console.log((17n % 5n).toString());
// BigInt throws RangeError on a zero modulus (Number silently returns NaN)
try {
  1n % 0n;
} catch (e) {
  console.log((e as Error).constructor.name + " caught");
}
Output
52
2
RangeError caught

Language differences

C: no arbitrary precision — the example uses unsigned __int128 (a GCC/Clang compiler extension, not standard C, capped near 3.4e38); beyond that use GMP (mpz_mod); modulo by zero is undefined behavior, so check first.

C++: same as C; boost::multiprecision::cpp_int or GMP for real work.

C#: System.Numerics.BigInteger is arbitrary precision; % truncates toward zero (-7 % 5 == -2); a zero modulus throws DivideByZeroException.

Go: % truncates toward zero (-7 % 5 == -2); big.Int.Mod is Euclidean (non-negative result), use big.Int.Rem for truncated semantics; Mod by zero panics and needs a recover.

Java: % truncates toward zero; BigInteger.mod requires a positive modulus and yields a non-negative result (Euclidean), remainder gives truncated semantics; a zero modulus throws ArithmeticException.

JavaScript: Number's % floats large values first (loss of precision past 2^53, results untrustworthy); BigInt's % truncates toward zero and still returns a BigInt (printed with an n suffix, needs toString()); Number modulo by zero silently yields NaN, BigInt throws RangeError.

PHP: strings in, strings out; the result takes the sign of the dividend (bcmod("-5", "3") === "-2", truncated toward zero) — since 7.2 the operands are no longer truncated to integers and the semantics follow fmod() rather than the % operator (purely integer semantics before), so beware across version upgrades; a zero modulus throws DivisionByZeroError since 8.0 (7.x returned null plus a warning); the third scale argument sets the decimal places of the result (subject to the bcscale() global).

Python: int is arbitrary precision natively; % is floor semantics — the result takes the modulus's sign (-7 % 5 == 3, opposite of the truncated semantics in PHP/C-family, the biggest cross-language porting trap); divmod returns quotient and remainder together; a zero modulus raises ZeroDivisionError.

Rust: % (rem) truncates toward zero, rem_euclid gives Euclidean semantics; integer modulo by zero panics, checked_rem/checked_rem_euclid return an Option.

TypeScript: same as JavaScript.