num_integer

Trait Roots

Source
pub trait Roots: Integer {
    // Required method
    fn nth_root(&self, n: u32) -> Self;

    // Provided methods
    fn sqrt(&self) -> Self { ... }
    fn cbrt(&self) -> Self { ... }
}
Expand description

Provides methods to compute an integer’s square root, cube root, and arbitrary nth root.

Required Methods§

Source

fn nth_root(&self, n: u32) -> Self

Returns the truncated principal nth root of an integer – if x >= 0 { ⌊ⁿ√x⌋ } else { ⌈ⁿ√x⌉ }

This is solving for r in rⁿ = x, rounding toward zero. If x is positive, the result will satisfy rⁿ ≤ x < (r+1)ⁿ. If x is negative and n is odd, then (r-1)ⁿ < x ≤ rⁿ.

§Panics

Panics if n is zero:

println!("can't compute ⁰√x : {}", 123.nth_root(0));

or if n is even and self is negative:

println!("no imaginary numbers... {}", (-1).nth_root(10));
§Examples
use num_integer::Roots;

let x: i32 = 12345;
assert_eq!(x.nth_root(1), x);
assert_eq!(x.nth_root(2), x.sqrt());
assert_eq!(x.nth_root(3), x.cbrt());
assert_eq!(x.nth_root(4), 10);
assert_eq!(x.nth_root(13), 2);
assert_eq!(x.nth_root(14), 1);
assert_eq!(x.nth_root(std::u32::MAX), 1);

assert_eq!(std::i32::MAX.nth_root(30), 2);
assert_eq!(std::i32::MAX.nth_root(31), 1);
assert_eq!(std::i32::MIN.nth_root(31), -2);
assert_eq!((std::i32::MIN + 1).nth_root(31), -1);

assert_eq!(std::u32::MAX.nth_root(31), 2);
assert_eq!(std::u32::MAX.nth_root(32), 1);

Provided Methods§

Source

fn sqrt(&self) -> Self

Returns the truncated principal square root of an integer – ⌊√x⌋

This is solving for r in r² = x, rounding toward zero. The result will satisfy r² ≤ x < (r+1)².

§Panics

Panics if self is less than zero:

println!("no imaginary numbers... {}", (-1).sqrt());
§Examples
use num_integer::Roots;

let x: i32 = 12345;
assert_eq!((x * x).sqrt(), x);
assert_eq!((x * x + 1).sqrt(), x);
assert_eq!((x * x - 1).sqrt(), x - 1);
Source

fn cbrt(&self) -> Self

Returns the truncated principal cube root of an integer – if x >= 0 { ⌊∛x⌋ } else { ⌈∛x⌉ }

This is solving for r in r³ = x, rounding toward zero. If x is positive, the result will satisfy r³ ≤ x < (r+1)³. If x is negative, then (r-1)³ < x ≤ r³.

§Examples
use num_integer::Roots;

let x: i32 = 1234;
assert_eq!((x * x * x).cbrt(), x);
assert_eq!((x * x * x + 1).cbrt(), x);
assert_eq!((x * x * x - 1).cbrt(), x - 1);

assert_eq!((-(x * x * x)).cbrt(), -x);
assert_eq!((-(x * x * x + 1)).cbrt(), -x);
assert_eq!((-(x * x * x - 1)).cbrt(), -(x - 1));

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Roots for i8

Source§

fn nth_root(&self, n: u32) -> Self

Source§

fn sqrt(&self) -> Self

Source§

fn cbrt(&self) -> Self

Source§

impl Roots for i16

Source§

fn nth_root(&self, n: u32) -> Self

Source§

fn sqrt(&self) -> Self

Source§

fn cbrt(&self) -> Self

Source§

impl Roots for i32

Source§

fn nth_root(&self, n: u32) -> Self

Source§

fn sqrt(&self) -> Self

Source§

fn cbrt(&self) -> Self

Source§

impl Roots for i64

Source§

fn nth_root(&self, n: u32) -> Self

Source§

fn sqrt(&self) -> Self

Source§

fn cbrt(&self) -> Self

Source§

impl Roots for i128

Source§

fn nth_root(&self, n: u32) -> Self

Source§

fn sqrt(&self) -> Self

Source§

fn cbrt(&self) -> Self

Source§

impl Roots for isize

Source§

fn nth_root(&self, n: u32) -> Self

Source§

fn sqrt(&self) -> Self

Source§

fn cbrt(&self) -> Self

Source§

impl Roots for u8

Source§

fn nth_root(&self, n: u32) -> Self

Source§

fn sqrt(&self) -> Self

Source§

fn cbrt(&self) -> Self

Source§

impl Roots for u16

Source§

fn nth_root(&self, n: u32) -> Self

Source§

fn sqrt(&self) -> Self

Source§

fn cbrt(&self) -> Self

Source§

impl Roots for u32

Source§

fn nth_root(&self, n: u32) -> Self

Source§

fn sqrt(&self) -> Self

Source§

fn cbrt(&self) -> Self

Source§

impl Roots for u64

Source§

fn nth_root(&self, n: u32) -> Self

Source§

fn sqrt(&self) -> Self

Source§

fn cbrt(&self) -> Self

Source§

impl Roots for u128

Source§

fn nth_root(&self, n: u32) -> Self

Source§

fn sqrt(&self) -> Self

Source§

fn cbrt(&self) -> Self

Source§

impl Roots for usize

Source§

fn nth_root(&self, n: u32) -> Self

Source§

fn sqrt(&self) -> Self

Source§

fn cbrt(&self) -> Self

Implementors§