当我尝试做 10 次方 100 时,我得到 p>
thread 'main' panicked at 'attempt to multiply with overflow', shorter.rs:33
note: Run with `RUST_BACKTRACE=1` for a backtrace.
这是正常的,因为 10^100 大于 2^64(甚至 2^128)。
最佳答案
如果您确实只使用整数,则可以使用 big_int 中的 BigInt
或 BigUint
箱。一个示例用法可能是:
extern crate num_bigint;
use num_bigint::{BigInt, Sign};
fn main() {
let x = BigInt::new(Sign::Plus, vec![1, 0]);
println!(num::pow(x, 100).to_str_radix(10));
}
关于rust - 如何处理 Rust 中不适合 128 位的真实大数而不会丢失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44325802/