bccomp

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

比较两个任意精度数字符串,返回 -1、0 或 1。

C C23

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

// 手写字符串比较(无符号整数:先比长度再逐位),返回 -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) {
    // 两位大数比较:末位差 1
    printf("%d\n", bccomp_str("123456789012345678901234567891",
                              "123456789012345678901234567890"));
    printf("%d\n", bccomp_str("25", "25"));
    return 0;
}
输出
1
0

C++ C++26

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

// 手写字符串比较(无符号整数:先比长度再逐位),返回 -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;
}
输出
1
0

C# v11.0.100

v11.0.100
// 浮点在此精度已无法区分两数
Console.WriteLine(1.00000000000000001 == 1.0);
// decimal 在精度内可区分(上限约 28 位有效数字)
Console.WriteLine(1.00000000000000001m > 1m ? 1 : -1);
Console.WriteLine(2.5m == 2.5m);
输出
True
1
True

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"math/big"
)

func main() {
	// float64 在此精度已无法区分两数(用变量避开编译期常量精确求值)
	x1 := 1.00000000000000001
	fmt.Println(x1 == 1.0)
	// big.Float 以 64 位内部精度区分:Cmp 返回 -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))
}
输出
true
1
0

Java v27.0.1

v27.0.1
import java.math.BigDecimal;

public class Main {
    public static void main(String[] args) {
        // 浮点在此精度已无法区分两数
        System.out.println(1.00000000000000001 == 1.0);
        // BigDecimal 按十进制精确比较:compareTo 返回 -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")));
    }
}
输出
true
1
0

JavaScript v26.13.1

v26.13.1
// 浮点在此精度已无法区分两数
console.log(1.00000000000000001 === 1.0);
// 精确比较惯用法:BigInt 放大后逐位比较(整数场景)
console.log(123456789012345678901234567891n > 123456789012345678901234567890n ? 1 : -1);
console.log(2.5 === 2.5);
输出
true
1
true

PHP v8.6.0

v8.6.0
// bccomp:按 scale 指定精度精确比较,返回 -1/0/1
echo bccomp("1.00000000000000001", "1", 17), "\n";
// 浮点在此精度已无法区分两数(字面量解析即舍入)
var_dump(1.00000000000000001 === 1.0);
echo bccomp("2.5", "2.5"), "\n";
输出
1
bool(true)
0

Python v3.15.2

v3.15.2
from decimal import Decimal

# Decimal 按十进制精确比较
print((Decimal("1.00000000000000001") > Decimal("1")))
# 浮点在此精度已合并为同一个数
print(1.00000000000000001 == 1.0)
print(Decimal("2.5") == Decimal("2.5"))
输出
True
True
True

Rust v1.99.1

v1.99.1
// 浮点在此精度已无法区分两数
fn main() {
    println!("{}", 1.00000000000000001f64 == 1.0f64);
    // 精确比较惯用法:手写按字符串比较(无符号整数:先比长度再逐位)
    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"));
}
输出
true
Greater
Equal

TypeScript v26.13.1

v26.13.1
// 浮点在此精度已无法区分两数
console.log(1.00000000000000001 === 1.0);
// 精确比较惯用法:BigInt 放大后逐位比较(整数场景)
const a: bigint = 123456789012345678901234567891n;
const b: bigint = 123456789012345678901234567890n;
console.log(a > b ? 1 : -1);
console.log(2.5 === 2.5);
输出
true
1
true

语言差异说明

C:无内置,手写字符串比较(无符号整数:先比位数再逐位 strcmp);带符号/小数需自行扩展。

C++:同 C 手写;std::string 的字典序比较恰好可用于等长无符号整数。

C#:decimal 在精度上限内可区分本例 1e-17 的差异;超过 28 位用 BigInteger.CompareTo。

Go:float64 已无法区分本例两数(double 有效位约 15-16 位十进制);big.Float 设足 SetPrec 后 Cmp 返回 -1/0/1,big.Int 的 Cmp 同形。

Java:BigDecimal.compareTo 是标准答案;注意 equals 会连 scale 一起比(2.50 不 equals 2.5),compareTo 才是纯数值比较。

JavaScript:Number 是 double,约 16 位十进制后失效;惯用 BigInt 比较整数(>/< 直接可用),小数场景转第三方库。

PHP:bccomp 以字符串为载体比较,scale 指定小数位精度(本例 17 位下 1.00000000000000001 > 1 能判出 1);float 早在 1e-16 量级就分不出这两个数了。

Python:Decimal 比较精确;float 对照演示了字面量 1.00000000000000001 解析即舍入成 1.0 的陷阱。

Rust:无标准库大数十进制;演示手写 cmp_big(无符号整数:先比长度再字典序),返回 std::cmp::Ordering。

TypeScript:同 JavaScript,BigInt 比较或第三方库。