arbi

Trait Pow

Source
pub trait Pow<T> {
    type Output;

    // Required method
    fn pow(self, exponent: T) -> Self::Output;
}
Expand description

Return self to the power exponent.

§Usage

use arbi::{Arbi, Pow};

let mut a = Arbi::from(2);

// Raise an `Arbi` integer to the power of a `usize` (`u128` also supported,
// up to a certain range).
a = a.pow(125_usize);
assert_eq!(a, 1_u128 << 125);

// Raise an `Arbi` integer to the power of another `Arbi`
a = a.pow(Arbi::from(2));
assert_eq!(a, Arbi::from_str_radix(
    "400000000000000000000000000000000000000000000000000000000000000",
    16
).unwrap());
a = a.pow(&Arbi::from(2));
assert_eq!(a, Arbi::from_str_radix(
    "1000000000000000000000000000000000000000000000000000000000000000000000\
     00000000000000000000000000000000000000000000000000000000",
    16
).unwrap());

A sufficient condition for the left shift operation to lead to exhaustion of the internal digit vector’s maximum capacity is that base is at least 2 in absolute value and that the exponent is greater than or equal to Arbi::MAX_BITS (a value greater than usize::MAX). Rather than allow programs to run a computation that is guaranteed to run out of memory, programs will panic if these conditions are met:

use arbi::{Arbi, Pow};

let mut a = Arbi::from(Arbi::MAX_BITS);
let mut one = Arbi::neg_one();
let mut two = Arbi::from(2);

one = one.pow(&a); // Ok
assert_eq!(one, 1);

two = two.pow(&a); // Panics

Negative exponents will panic:

use arbi::{Arbi, Pow};

let mut a = Arbi::from(2);
a = a.pow(Arbi::from(-10));

Required Associated Types§

Source

type Output

The return type of pow().

Required Methods§

Source

fn pow(self, exponent: T) -> Self::Output

Return self to the power exponent.

Implementors§