bcpow

bcpow(string $num, string $exponent, ?int $scale = null): string

任意精度数的整数次幂:2^64 这类早已溢出 int 的幂以字符串精确返回。

C C23

C23
#include <stdio.h>

/* C 无任意精度:unsigned __int128(编译器扩展)放得下 2^100;打印需手写 */
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]);
}

/* 手写快速幂取模(对应 bcpowmod / Python pow(a, b, m)) */
static unsigned __int128 mod_pow(unsigned __int128 base, unsigned exp, unsigned __int128 m) {
    unsigned __int128 result = 1;
    base %= m;
    while (exp > 0) {
        if (exp % 2 == 1) result = result * base % m;
        base = base * base % m;
        exp /= 2;
    }
    return result;
}

int main(void) {
    /* 2 的幂即位移动(指数 < 128 才安全) */
    print_u128(((unsigned __int128)1) << 64);
    putchar('\n');
    print_u128(((unsigned __int128)1) << 100);
    putchar('\n');
    /* 64 位溢出对照:无符号移位静默回绕,得不到真实 2^64 */
    printf("%llu\n", (unsigned long long)1 << 63);
    printf("%llu\n", (unsigned long long)mod_pow(2, 100, 97));
    return 0;
}
输出
18446744073709551616
1267650600228229401496703205376
9223372036854775808
16

C++ C++26

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

// C++ 无任意精度:unsigned __int128(编译器扩展)放得下 2^100;打印转字符串手写
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());
}

// 手写快速幂取模
static unsigned __int128 mod_pow(unsigned __int128 base, unsigned exp, unsigned __int128 m) {
    unsigned __int128 result = 1;
    base %= m;
    while (exp > 0) {
        if (exp % 2 == 1) result = result * base % m;
        base = base * base % m;
        exp /= 2;
    }
    return result;
}

int main() {
    std::cout << to_string_u128(((unsigned __int128)1) << 64) << "\n";
    std::cout << to_string_u128(((unsigned __int128)1) << 100) << "\n";
    // 64 位溢出对照:无符号移位静默回绕
    std::cout << ((unsigned long long)1 << 63) << "\n";
    std::cout << static_cast<unsigned long long>(mod_pow(2, 100, 97)) << "\n";
    return 0;
}
输出
18446744073709551616
1267650600228229401496703205376
9223372036854775808
16

C# v11.0.100

v11.0.100
using System.Numerics;

// BigInteger.Pow:任意精度幂;unchecked 下 long 移位静默回绕
Console.WriteLine(BigInteger.Pow(2, 64));
Console.WriteLine(BigInteger.Pow(2, 100));
Console.WriteLine(unchecked(1L << 63));
// ModPow:一步取模幂
Console.WriteLine(BigInteger.ModPow(2, 100, 97));
输出
18446744073709551616
1267650600228229401496703205376
-9223372036854775808
16

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"math/big"
)

func main() {
	// big.Int.Exp:任意精度幂;第 3 参 nil 即普通幂,非 nil 即取模幂
	base := big.NewInt(2)
	fmt.Println(new(big.Int).Exp(base, big.NewInt(64), nil))
	fmt.Println(new(big.Int).Exp(base, big.NewInt(100), nil))
	// int64 移位:常量表达式编译期报溢出错误,变量运算运行期静默回绕
	var one int64 = 1
	fmt.Println(one << 63)
	fmt.Println(new(big.Int).Exp(base, big.NewInt(100), big.NewInt(97)))
}
输出
18446744073709551616
1267650600228229401496703205376
-9223372036854775808
16

Java v27.0.1

v27.0.1
import java.math.BigInteger;

public class Main {
    public static void main(String[] args) {
        // BigInteger.pow:任意精度幂(指数为 int)
        System.out.println(BigInteger.TWO.pow(64));
        System.out.println(BigInteger.TWO.pow(100));
        // long 溢出:静默回绕(2^63 落在 Long.MIN_VALUE)
        System.out.println((long) 1 << 63);
        // modPow:一步取模幂
        System.out.println(BigInteger.TWO.modPow(BigInteger.valueOf(100), BigInteger.valueOf(97)));
    }
}
输出
18446744073709551616
1267650600228229401496703205376
-9223372036854775808
16

JavaScript v26.13.1

v26.13.1
// BigInt **:任意精度;Number ** 2^63 起精度丢失(尾数只有 53 位)
console.log((2n ** 64n).toString());
console.log((2n ** 100n).toString());
console.log(2 ** 63);
// 无内置取模幂:手写快速幂
function modPow(base, exp, m) {
  let result = 1n;
  base %= m;
  while (exp > 0n) {
    if (exp % 2n === 1n) result = (result * base) % m;
    base = (base * base) % m;
    exp /= 2n;
  }
  return result;
}
console.log(modPow(2n, 100n, 97n).toString());
输出
18446744073709551616
1267650600228229401496703205376
9223372036854776000
16

PHP v8.6.0

v8.6.0
// bcpow:任意精度幂——2^64 早已超出 int 上限(约 9.2e18),字符串精确返回
echo bcpow("2", "64"), "\n";
echo bcpow("2", "100"), "\n";
// 对照:int 幂在 2^63 起上溢转 float,科学计数法且精度丢失
echo 2 ** 63, "\n";
// PHP 8 起指数带小数部分抛 ValueError
try {
    bcpow("2", "0.5");
} catch (ValueError) {
    echo "ValueError caught";
}
输出
18446744073709551616
1267650600228229401496703205376
9.2233720368548E+18
ValueError caught

Python v3.15.2

v3.15.2
# ** 与 pow:整数幂任意精度原生(与 bcpow 对应);float 幂才丢精度
print(2 ** 64)
print(2 ** 100)
print(2.0 ** 63)
# pow(base, exp, mod):一步取模幂(密码学常用)
print(pow(2, 100, 97))
输出
18446744073709551616
1267650600228229401496703205376
9.223372036854776e+18
16

Rust v1.99.1

v1.99.1
// 标准库无任意精度:u128 上限 2^128-1,2^100 放得下(更大需第三方 num-bigint crate)
fn main() {
    println!("{}", 1u128 << 64);
    println!("{}", 1u128 << 100);
    // u64 溢出对照:无符号移位静默回绕
    println!("{}", 1u64 << 63);
    // 取模幂:无内置 mod_pow,手写快速幂
    fn mod_pow(mut base: u128, mut exp: u32, m: u128) -> u128 {
        let mut result = 1u128;
        base %= m;
        while exp > 0 {
            if exp % 2 == 1 {
                result = result * base % m;
            }
            base = base * base % m;
            exp /= 2;
        }
        result
    }
    println!("{}", mod_pow(2, 100, 97));
}
输出
18446744073709551616
1267650600228229401496703205376
9223372036854775808
16

TypeScript v26.13.1

v26.13.1
// BigInt **:任意精度;Number ** 2^63 起精度丢失(尾数只有 53 位)
const p64: bigint = 2n ** 64n;
const p100: bigint = 2n ** 100n;
console.log(p64.toString());
console.log(p100.toString());
console.log(2 ** 63);
// 无内置取模幂:手写快速幂
function modPow(base: bigint, exp: bigint, m: bigint): bigint {
  let result = 1n;
  base %= m;
  while (exp > 0n) {
    if (exp % 2n === 1n) result = (result * base) % m;
    base = (base * base) % m;
    exp /= 2n;
  }
  return result;
}
console.log(modPow(2n, 100n, 97n).toString());
输出
18446744073709551616
1267650600228229401496703205376
9223372036854776000
16

语言差异说明

C:无任意精度——unsigned __int128(编译器扩展)上限 2^128-1,2 的幂可用移位(指数 < 128);更大指数/底数需 GMP(mpz_pow_ui、mpz_powm 取模幂);u64 移位溢出静默回绕。

C++:同 C;实战 boost::multiprecision::cpp_int(有 pow 自由函数)或 GMP。

C#:BigInteger.Pow(value, int exponent) 任意精度;BigInteger.ModPow 一步取模幂;unchecked 下 long 溢出静默回绕(默认编译器对常量溢出才报错)。

Go:big.Int.Exp(base, exp, nil) 第 3 参为 nil 即普通幂、非 nil 即取模幂(指数也可是大数);注意 int64(1) << 63 作为常量表达式是编译期溢出错误,先存变量再移位才是运行期静默回绕(得 -2^63)。

Java:BigInteger.pow(int) 任意精度、modPow 取模幂;long 溢出静默回绕(2^63 落在 Long.MIN_VALUE)。

JavaScript:Number 的 ** 在 2^63 起精度丢失(浮点尾数 53 位,2 ** 63 打印 9223372036854776000,与真值差 808);BigInt 的 ** 任意精度;指数非法(负数/非整数)抛 RangeError;无内置取模幂,手写快速幂。

PHP:bcpow 字符串进字符串出、任意精度;对照 2 ** 63 上溢自动转 float(打印 9.2233720368548E+18,科学计数法且精度丢失);8.0 起指数带小数部分抛 ValueError(不再支持分数幂,开方用 bcsqrt);取模幂用 bcpowmod(密码学场景务必用它,先幂后模会生成天文数字中间值)。

Python:**/pow 整数幂任意精度原生(指数还可为负得分数 float);pow(base, exp, mod) 三参形式一步取模幂且内部用快速幂;2.0 ** 63 才是浮点(9.223372036854776e+18)。

Rust:标准库无任意精度——u128 内的 2 的幂可移位(1u128 << 100);更大需 num-bigint crate(pow、modpow);u64 移位溢出静默回绕(debug 下算术溢出 panic,但移位不检查)。

TypeScript:同 JavaScript。