bcmod
bcmod(string $num1, string $num2, ?int $scale = null): string
任意精度整数的取模运算:操作数与结果均为数字字符串,模数为 0 时 PHP 8 抛 DivisionByZeroError。
C C23
#include <stdio.h>
/* C 无任意精度:unsigned __int128(编译器扩展)放得下本例数值;打印需手写 */
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) {
/* 无超大整数字面量:按十进制位逐字符构造 */
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);
/* 整数取模除零是未定义行为:必须先检查 */
unsigned __int128 m = 0;
if (m == 0) printf("zero modulus detected: skipped\n");
return 0;
}
52 2 zero modulus detected: skipped
C++ C++26
#include <iostream>
#include <string>
// C++ 无任意精度:unsigned __int128(编译器扩展)放得下本例数值;打印转字符串手写
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";
// 整数取模除零是未定义行为:必须先检查
unsigned __int128 m = 0;
if (m == 0) std::cout << "zero modulus detected: skipped\n";
return 0;
}
52 2 zero modulus detected: skipped
C# v11.0.100
using System.Numerics;
// BigInteger:任意精度取模(正操作数结果非负;负数语义为向零截断)
var n = BigInteger.Parse("123456789012345678901234567890");
Console.WriteLine(n % 97);
Console.WriteLine(17 % 5);
// 模数为 0 抛 DivideByZeroException
try { _ = BigInteger.One % BigInteger.Zero; }
catch (DivideByZeroException) { Console.WriteLine("DivideByZeroException caught"); }
52 2 DivideByZeroException caught
Go v1.27.4
package main
import (
"fmt"
"math/big"
)
// big.Int 模 0 会 panic:包一层函数用 defer/recover 演示错误分支
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 的 % 有 64 位上限;超大数用 big.Int.Mod(欧几里得语义,结果非负)
n, _ := new(big.Int).SetString("123456789012345678901234567890", 10)
fmt.Println(n.Mod(n, big.NewInt(97)))
fmt.Println(17 % 5)
modByZero()
}
52 2 panic recovered
Java v27.0.1
import java.math.BigInteger;
public class Main {
public static void main(String[] args) {
// long 的 % 有 64 位上限;超大数用 BigInteger.mod(模数须为正,结果非负)
BigInteger n = new BigInteger("123456789012345678901234567890");
System.out.println(n.mod(BigInteger.valueOf(97)));
System.out.println(17 % 5);
// 模数为 0 抛 ArithmeticException
try {
BigInteger.ONE.mod(BigInteger.ZERO);
} catch (ArithmeticException e) {
System.out.println("ArithmeticException caught");
}
}
}
52 2 ArithmeticException caught
JavaScript v26.13.1
// Number 的 % 会先把大数浮点化(精度丢失):大整数取模用 BigInt
console.log((123456789012345678901234567890n % 97n).toString());
console.log((17n % 5n).toString());
// BigInt 模数为 0 抛 RangeError(Number 是 NaN 静默返回)
try {
1n % 0n;
} catch (e) {
console.log(e.constructor.name + " caught");
}
52 2 RangeError caught
PHP v8.6.0
// bcmod:任意精度整数取模(操作数是数字字符串,正操作数结果非负)
echo bcmod("123456789012345678901234567890", "97"), "\n";
echo bcmod("17", "5"), "\n";
// PHP 8 起模数为 0 抛 DivisionByZeroError(7.x 返回 null + warning)
try {
bcmod("1", "0");
} catch (DivisionByZeroError) {
echo "DivisionByZeroError caught";
}
52 2 DivisionByZeroError caught
Python v3.15.2
# int 天生任意精度:% 直接可用(floor 语义:结果符号随模数,与 PHP 的截断语义不同)
print(123456789012345678901234567890 % 97)
print(17 % 5)
# 模数为 0 抛 ZeroDivisionError
try:
1 % 0
except ZeroDivisionError:
print("ZeroDivisionError caught")
52 2 ZeroDivisionError caught
Rust v1.99.1
// 标准库无任意精度:u128 上限约 3.4e38,本例数值放得下(更大需第三方 num-bigint crate)
fn main() {
let n: u128 = 123456789012345678901234567890;
println!("{}", n % 97);
println!("{}", 17u128 % 5);
// 整数取模除零 panic;checked_rem 返回 None
println!("{:?}", 1u128.checked_rem(0));
}
52 2 None
TypeScript v26.13.1
// Number 的 % 会先把大数浮点化(精度丢失):大整数取模用 BigInt
const n: bigint = 123456789012345678901234567890n;
console.log((n % 97n).toString());
console.log((17n % 5n).toString());
// BigInt 模数为 0 抛 RangeError(Number 是 NaN 静默返回)
try {
1n % 0n;
} catch (e) {
console.log((e as Error).constructor.name + " caught");
}
52 2 RangeError caught
语言差异说明
C:无任意精度——示例用 unsigned __int128(GCC/Clang 编译器扩展,非标准 C,上限约 3.4e38);超此范围需 GMP(mpz_mod);取模除零是未定义行为,必须先判零。
C++:同 C;实战 boost::multiprecision::cpp_int 或 GMP。
C#:System.Numerics.BigInteger 任意精度;% 为向零截断语义(-7 % 5 == -2);0 模抛 DivideByZeroException。
Go:% 向零截断(-7 % 5 == -2);big.Int.Mod 是欧几里得语义(结果非负),截断语义用 big.Int.Rem;模 0 panic,需 recover 兜底。
Java:% 向零截断;BigInteger.mod 要求模数为正且结果非负(欧几里得语义),截断语义用 BigInteger.remainder;0 模抛 ArithmeticException。
JavaScript:Number 的 % 对大数先浮点化(超 2^53 后精度丢失,结果不可信);BigInt 的 % 向零截断,结果仍是 BigInt(输出会带 n 后缀,需 toString());Number 的 0 模返回 NaN 静默不报错,BigInt 抛 RangeError。
PHP:字符串进字符串出;结果符号随被除数(bcmod("-5", "3") === "-2",向零截断)——7.2 起操作数不再截断为整数、语义对齐 fmod() 而非 % 运算符(此前为纯整数语义),跨版本升级注意;0 模数 8.0 起抛 DivisionByZeroError(7.x 返回 null + warning);第 3 参 scale 控制结果小数位(受 bcscale() 全局值影响)。
Python:int 原生任意精度;% 是 floor 语义——结果符号随模数(-7 % 5 == 3,与 PHP/C 系的截断语义相反,跨语言移植最大陷阱);divmod 同时取商余;0 模抛 ZeroDivisionError。
Rust:%(rem)向零截断,欧几里得语义用 rem_euclid;整数 0 模 panic,checked_rem/checked_rem_euclid 返回 Option。
TypeScript:同 JavaScript。