# [−] [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) -> Self` [src]

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

#### `fn cbrt(&self) -> Self` [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`)