Trait Roots
trait Roots: Integer
Provides methods to compute an integer's square root, cube root,
and arbitrary nth root.
Required Methods
fn nth_root(self: &Self, n: u32) -> SelfReturns the truncated principal
nth root of an integer --if x >= 0 { ⌊ⁿ√x⌋ } else { ⌈ⁿ√x⌉ }This is solving for
rinrⁿ = x, rounding toward zero. Ifxis positive, the result will satisfyrⁿ ≤ x < (r+1)ⁿ. Ifxis negative andnis odd, then(r-1)ⁿ < x ≤ rⁿ.Panics
Panics if
nis zero:# use num_integer::Roots; println!("can't compute ⁰√x : {}", 123.nth_root(0));or if
nis even andselfis negative:# use num_integer::Roots; println!("no imaginary numbers... {}", (-1).nth_root(10));Examples
use Roots; let x: i32 = 12345; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!;
Provided Methods
fn sqrt(self: &Self) -> SelfReturns the truncated principal square root of an integer --
⌊√x⌋This is solving for
rinr² = x, rounding toward zero. The result will satisfyr² ≤ x < (r+1)².Panics
Panics if
selfis less than zero:# use num_integer::Roots; println!("no imaginary numbers... {}", (-1).sqrt());Examples
use Roots; let x: i32 = 12345; assert_eq!; assert_eq!; assert_eq!;fn cbrt(self: &Self) -> SelfReturns the truncated principal cube root of an integer --
if x >= 0 { ⌊∛x⌋ } else { ⌈∛x⌉ }This is solving for
rinr³ = x, rounding toward zero. Ifxis positive, the result will satisfyr³ ≤ x < (r+1)³. Ifxis negative, then(r-1)³ < x ≤ r³.Examples
use Roots; let x: i32 = 1234; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!;
Implementors
impl Roots for i16impl Roots for i128impl Roots for u16impl Roots for u128impl Roots for i8impl Roots for i64impl Roots for u8impl Roots for u64impl Roots for i32impl Roots for isizeimpl Roots for u32impl Roots for usize