bccomp

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

Compares two arbitrary-precision number strings and returns -1, 0 or 1.

C C23

C23
#include <stdio.h>
#include <string.h>

// Hand-written string comparison (unsigned integers: length first, then digit by digit), returning -1/0/1
static int bccomp_str(const char *a, const char *b) {
    size_t la = strlen(a), lb = strlen(b);
    if (la != lb) return la < lb ? -1 : 1;
    int c = strcmp(a, b);
    return c < 0 ? -1 : (c > 0 ? 1 : 0);
}

int main(void) {
    printf("%d\n", bccomp_str("123456789012345678901234567891",
                              "123456789012345678901234567890"));
    printf("%d\n", bccomp_str("25", "25"));
    return 0;
}
Output
1
0

C++ C++26

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

// Hand-written string comparison (unsigned integers: length first, then digit by digit), returning -1/0/1
static int bccomp_str(const std::string &a, const std::string &b) {
    if (a.size() != b.size()) return a.size() < b.size() ? -1 : 1;
    return a < b ? -1 : (a > b ? 1 : 0);
}

int main() {
    std::cout << bccomp_str("123456789012345678901234567891",
                            "123456789012345678901234567890") << "\n";
    std::cout << bccomp_str("25", "25") << "\n";
    return 0;
}
Output
1
0

C# v11.0.100

v11.0.100
// float can no longer distinguish the two numbers at this magnitude
Console.WriteLine(1.00000000000000001 == 1.0);
// decimal distinguishes them within its precision (capped at ~28 significant digits)
Console.WriteLine(1.00000000000000001m > 1m ? 1 : -1);
Console.WriteLine(2.5m == 2.5m);
Output
True
1
True

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"math/big"
)

func main() {
	// float64 can no longer distinguish the two numbers at this magnitude (variables avoid compile-time constant folding)
	x1 := 1.00000000000000001
	fmt.Println(x1 == 1.0)
	// big.Float distinguishes them with 64-bit internal precision: Cmp returns -1/0/1
	x, _ := new(big.Float).SetPrec(64).SetString("1.00000000000000001")
	y, _ := new(big.Float).SetPrec(64).SetString("1")
	fmt.Println(x.Cmp(y))
	fmt.Println(x.Cmp(x))
}
Output
true
1
0

Java v27.0.1

v27.0.1
import java.math.BigDecimal;

public class Main {
    public static void main(String[] args) {
        // float can no longer distinguish the two numbers at this magnitude
        System.out.println(1.00000000000000001 == 1.0);
        // BigDecimal compares exactly in decimal: compareTo returns -1/0/1
        System.out.println(new BigDecimal("1.00000000000000001")
                .compareTo(new BigDecimal("1")));
        System.out.println(new BigDecimal("2.5").compareTo(new BigDecimal("2.5")));
    }
}
Output
true
1
0

JavaScript v26.13.1

v26.13.1
// float can no longer distinguish the two numbers at this magnitude
console.log(1.00000000000000001 === 1.0);
// Exact-comparison idiom: scale up with BigInt and compare digit by digit (integer cases)
console.log(123456789012345678901234567891n > 123456789012345678901234567890n ? 1 : -1);
console.log(2.5 === 2.5);
Output
true
1
true

PHP v8.6.0

v8.6.0
// bccomp: exact comparison at the given scale, returning -1/0/1
echo bccomp("1.00000000000000001", "1", 17), "\n";
// float can no longer distinguish the two numbers at this magnitude (literal parsing rounds already)
var_dump(1.00000000000000001 === 1.0);
echo bccomp("2.5", "2.5"), "\n";
Output
1
bool(true)
0

Python v3.15.2

v3.15.2
from decimal import Decimal

# Decimal compares exactly in decimal
print((Decimal("1.00000000000000001") > Decimal("1")))
# float has already collapsed both into the same number at this magnitude
print(1.00000000000000001 == 1.0)
print(Decimal("2.5") == Decimal("2.5"))
Output
True
True
True

Rust v1.99.1

v1.99.1
// float can no longer distinguish the two numbers at this magnitude
fn main() {
    println!("{}", 1.00000000000000001f64 == 1.0f64);
    // Exact-comparison idiom: hand-written string comparison (unsigned integers: length first, then digit by digit)
    fn cmp_big(a: &str, b: &str) -> std::cmp::Ordering {
        a.len().cmp(&b.len()).then_with(|| a.cmp(b))
    }
    println!("{:?}", cmp_big(
        "123456789012345678901234567891",
        "123456789012345678901234567890"
    ));
    println!("{:?}", cmp_big("25", "25"));
}
Output
true
Greater
Equal

TypeScript v26.13.1

v26.13.1
// float can no longer distinguish the two numbers at this magnitude
console.log(1.00000000000000001 === 1.0);
// Exact-comparison idiom: scale up with BigInt and compare digit by digit (integer cases)
const a: bigint = 123456789012345678901234567891n;
const b: bigint = 123456789012345678901234567890n;
console.log(a > b ? 1 : -1);
console.log(2.5 === 2.5);
Output
true
1
true

Language differences

C: no built-in — hand-write string comparison (unsigned integers: compare lengths first, then digits); signs and decimals need extra work.

C++: same hand-rolled approach as C; std::string's lexicographic compare happens to work for equal-length unsigned integers.

C#: decimal can distinguish the 1e-17 gap in this example within its precision cap; beyond 28 digits use BigInteger.CompareTo.

Go: float64 already cannot tell the two numbers apart (doubles carry roughly 15-16 decimal digits); big.Float with sufficient SetPrec returns -1/0/1 from Cmp, and big.Int's Cmp has the same shape.

Java: BigDecimal.compareTo is the standard answer; note equals also compares scale (2.50 does not equal 2.5), while compareTo is purely numeric.

JavaScript: Number is a double that fails past roughly 16 decimal digits; the idiom is BigInt for integer comparison (>/< work directly) or a decimal library.

PHP: bccomp compares strings with scale specifying decimal precision (at 17 digits, 1.00000000000000001 > 1 correctly yields 1); floats already fail to distinguish these two near the 1e-16 magnitude.

Python: Decimal compares exactly; the float side demonstrates the trap where the literal 1.00000000000000001 is rounded to 1.0 at parse time.

Rust: no standard-library big decimals; the demo hand-writes cmp_big (unsigned integers: length first, then lexicographic), returning std::cmp::Ordering.

TypeScript: same as JavaScript — BigInt comparison or a third-party library.