Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with real large numbers which don't fit in 128 bits in Rust without loss?

Tags:

rust

When I try to do 10 power 100, I get

thread 'main' panicked at 'attempt to multiply with overflow', shorter.rs:33
note: Run with `RUST_BACKTRACE=1` for a backtrace.

which is normal because 10^100 is greater than 2^64 (and even 2^128).

like image 671
Darkaird Avatar asked Jun 02 '17 09:06

Darkaird


1 Answers

If you're definitely only working with integers you can use either BigInt or BigUint from the big_int crate. An example usage could be:

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));
}
like image 128
Jake Conkerton-Darby Avatar answered Oct 22 '22 17:10

Jake Conkerton-Darby