# [−][src]Trait num::integer::Roots

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

        fn sqrt(&self) -> Self { ... }
        fn cbrt(&self) -> Self { ... }
    }

Provides methods to compute an integer's square root, cube root, and arbitrary `n`th root.

## Required methods

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

Returns the truncated principal `n`th 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);

Loading content...

## Provided methods

### `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);

### `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));

Loading content...

## Implementations on Foreign Types

### `impl Roots for i8`[src]

#### `fn nth_root(&self, n: u32) -> i8`[src]

#### `fn sqrt(&self) -> i8`[src]

#### `fn cbrt(&self) -> i8`[src]

### `impl Roots for u128`[src]

#### `fn nth_root(&self, n: u32) -> u128`[src]

#### `fn sqrt(&self) -> u128`[src]

#### `fn cbrt(&self) -> u128`[src]

### `impl Roots for isize`[src]

#### `fn nth_root(&self, n: u32) -> isize`[src]

#### `fn sqrt(&self) -> isize`[src]

#### `fn cbrt(&self) -> isize`[src]

### `impl Roots for u32`[src]

#### `fn nth_root(&self, n: u32) -> u32`[src]

#### `fn sqrt(&self) -> u32`[src]

#### `fn cbrt(&self) -> u32`[src]

### `impl Roots for u16`[src]

#### `fn nth_root(&self, n: u32) -> u16`[src]

#### `fn sqrt(&self) -> u16`[src]

#### `fn cbrt(&self) -> u16`[src]

### `impl Roots for i16`[src]

#### `fn nth_root(&self, n: u32) -> i16`[src]

#### `fn sqrt(&self) -> i16`[src]

#### `fn cbrt(&self) -> i16`[src]

### `impl Roots for u8`[src]

#### `fn nth_root(&self, n: u32) -> u8`[src]

#### `fn sqrt(&self) -> u8`[src]

#### `fn cbrt(&self) -> u8`[src]

### `impl Roots for u64`[src]

#### `fn nth_root(&self, n: u32) -> u64`[src]

#### `fn sqrt(&self) -> u64`[src]

#### `fn cbrt(&self) -> u64`[src]

### `impl Roots for usize`[src]

#### `fn nth_root(&self, n: u32) -> usize`[src]

#### `fn sqrt(&self) -> usize`[src]

#### `fn cbrt(&self) -> usize`[src]

### `impl Roots for i128`[src]

#### `fn nth_root(&self, n: u32) -> i128`[src]

#### `fn sqrt(&self) -> i128`[src]

#### `fn cbrt(&self) -> i128`[src]

### `impl Roots for i32`[src]

#### `fn nth_root(&self, n: u32) -> i32`[src]

#### `fn sqrt(&self) -> i32`[src]

#### `fn cbrt(&self) -> i32`[src]

### `impl Roots for i64`[src]

#### `fn nth_root(&self, n: u32) -> i64`[src]

#### `fn sqrt(&self) -> i64`[src]

#### `fn cbrt(&self) -> i64`[src]

Loading content...

## Implementors

Loading content...

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g., `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g., `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g., `str,u8` or `String,struct:Vec,test`)