pub trait RootRem<POW> {
    type RootOutput;
    type RemOutput;

    fn root_rem(self, exp: POW) -> (Self::RootOutput, Self::RemOutput);
}
Expand description

Finds the floor of the $n$th root of a number, returning both the root and the remainder.

Required Associated Types

Required Methods

Implementations on Foreign Types

Returns the floor of the $n$th root of a u8, and the remainder (the difference between the u8 and the $n$th power of the floor).

$f(x, n) = (\lfloor\sqrt[n]{x}\rfloor, x - \lfloor\sqrt[n]{x}\rfloor^2)$.

Worst-case complexity

Constant time and additional memory.

Panics

Panics if exp is zero.

Examples

See here.

Notes

The u8 implementation uses lookup tables.

Returns the floor of the $n$th root of a u16, and the remainder (the difference between the u16 and the $n$th power of the floor).

$f(x, n) = (\lfloor\sqrt[n]{x}\rfloor, x - \lfloor\sqrt[n]{x}\rfloor^2)$.

Worst-case complexity

Constant time and additional memory.

Panics

Panics if exp is zero.

Examples

See here.

Notes

The u16 implementation calls the implementation for u32s.

Returns the floor of the $n$th root of a u32, and the remainder (the difference between the u32 and the $n$th power of the floor).

$f(x, n) = (\lfloor\sqrt[n]{x}\rfloor, x - \lfloor\sqrt[n]{x}\rfloor^2)$.

Worst-case complexity

Constant time and additional memory.

Panics

Panics if exp is zero.

Examples

See here.

Notes

For cube roots, the u32 implementation uses a piecewise Chebyshev approximation. For other roots, it uses Newton’s method. In both implementations, the result of these approximations is adjusted afterwards to account for error.

Returns the floor of the $n$th root of a u64, and the remainder (the difference between the u64 and the $n$th power of the floor).

$f(x, n) = (\lfloor\sqrt[n]{x}\rfloor, x - \lfloor\sqrt[n]{x}\rfloor^2)$.

Worst-case complexity

Constant time and additional memory.

Panics

Panics if exp is zero.

Examples

See here.

Notes

For cube roots, the u64 implementation uses a piecewise Chebyshev approximation. For other roots, it uses Newton’s method. In both implementations, the result of these approximations is adjusted afterwards to account for error.

Returns the floor of the $n$th root of a usize, and the remainder (the difference between the usize and the $n$th power of the floor).

$f(x, n) = (\lfloor\sqrt[n]{x}\rfloor, x - \lfloor\sqrt[n]{x}\rfloor^2)$.

Worst-case complexity

Constant time and additional memory.

Panics

Panics if exp is zero.

Examples

See here.

Notes

The usize implementation calls the u32 or u64 implementations.

Returns the floor of the $n$th root of a u128, and the remainder (the difference between the u128 and the $n$th power of the floor).

$f(x, n) = (\lfloor\sqrt[n]{x}\rfloor, x - \lfloor\sqrt[n]{x}\rfloor^n)$.

Worst-case complexity

Constant time and additional memory.

Panics

Panics if exp is zero.

Examples

See here.

Notes

The u128 implementation computes the root using floating-point arithmetic. The approximate result is adjusted afterwards to account for error.

Implementors