pub trait RoundToMultiple<RHS = Self> {
    type Output;

    fn round_to_multiple(self, other: RHS, rm: RoundingMode) -> Self::Output;
}
Expand description

Rounds a number to a multiple of another number, according to a specified rounding mode.

Required Associated Types

Required Methods

Implementations on Foreign Types

Rounds a number to a multiple of another number, according to a specified rounding mode.

The only rounding modes that are guaranteed to return without a panic are Down and Floor.

Let $q = \frac{x}{y}$:

$f(x, y, \mathrm{Down}) = f(x, y, \mathrm{Floor}) = y \lfloor q \rfloor.$

$f(x, y, \mathrm{Up}) = f(x, y, \mathrm{Ceiling}) = y \lceil q \rceil.$

$$ f(x, y, \mathrm{Nearest}) = \begin{cases} y \lfloor q \rfloor & \text{if} \quad q - \lfloor q \rfloor < \frac{1}{2} \\ y \lceil q \rceil & \text{if} \quad q - \lfloor q \rfloor > \frac{1}{2} \\ y \lfloor q \rfloor & \text{if} \quad q - \lfloor q \rfloor = \frac{1}{2} \ \text{and} \ \lfloor q \rfloor \ \text{is even} \\ y \lceil q \rceil & \text{if} \quad q - \lfloor q \rfloor = \frac{1}{2} \ \text{and} \ \lfloor q \rfloor \ \text{is odd.} \end{cases} $$

$f(x, y, \mathrm{Exact}) = x$, but panics if $q \notin \N$.

The following two expressions are equivalent:

  • x.round_to_multiple(other, RoundingMode::Exact)
  • { assert!(x.divisible_by(other)); x }

but the latter should be used as it is clearer and more efficient.

Worst-case complexity

Constant time and additional memory.

Panics
  • If rm is Exact, but self is not a multiple of other.
  • If the multiple is outside the representable range.
  • If self is nonzero, other is zero, and rm is trying to round away from zero.
Examples

See here.

Rounds a number to a multiple of another number, according to a specified rounding mode.

The only rounding modes that are guaranteed to return without a panic are Down and Floor.

Let $q = \frac{x}{y}$:

$f(x, y, \mathrm{Down}) = f(x, y, \mathrm{Floor}) = y \lfloor q \rfloor.$

$f(x, y, \mathrm{Up}) = f(x, y, \mathrm{Ceiling}) = y \lceil q \rceil.$

$$ f(x, y, \mathrm{Nearest}) = \begin{cases} y \lfloor q \rfloor & \text{if} \quad q - \lfloor q \rfloor < \frac{1}{2} \\ y \lceil q \rceil & \text{if} \quad q - \lfloor q \rfloor > \frac{1}{2} \\ y \lfloor q \rfloor & \text{if} \quad q - \lfloor q \rfloor = \frac{1}{2} \ \text{and} \ \lfloor q \rfloor \ \text{is even} \\ y \lceil q \rceil & \text{if} \quad q - \lfloor q \rfloor = \frac{1}{2} \ \text{and} \ \lfloor q \rfloor \ \text{is odd.} \end{cases} $$

$f(x, y, \mathrm{Exact}) = x$, but panics if $q \notin \N$.

The following two expressions are equivalent:

  • x.round_to_multiple(other, RoundingMode::Exact)
  • { assert!(x.divisible_by(other)); x }

but the latter should be used as it is clearer and more efficient.

Worst-case complexity

Constant time and additional memory.

Panics
  • If rm is Exact, but self is not a multiple of other.
  • If the multiple is outside the representable range.
  • If self is nonzero, other is zero, and rm is trying to round away from zero.
Examples

See here.

Rounds a number to a multiple of another number, according to a specified rounding mode.

The only rounding modes that are guaranteed to return without a panic are Down and Floor.

Let $q = \frac{x}{y}$:

$f(x, y, \mathrm{Down}) = f(x, y, \mathrm{Floor}) = y \lfloor q \rfloor.$

$f(x, y, \mathrm{Up}) = f(x, y, \mathrm{Ceiling}) = y \lceil q \rceil.$

$$ f(x, y, \mathrm{Nearest}) = \begin{cases} y \lfloor q \rfloor & \text{if} \quad q - \lfloor q \rfloor < \frac{1}{2} \\ y \lceil q \rceil & \text{if} \quad q - \lfloor q \rfloor > \frac{1}{2} \\ y \lfloor q \rfloor & \text{if} \quad q - \lfloor q \rfloor = \frac{1}{2} \ \text{and} \ \lfloor q \rfloor \ \text{is even} \\ y \lceil q \rceil & \text{if} \quad q - \lfloor q \rfloor = \frac{1}{2} \ \text{and} \ \lfloor q \rfloor \ \text{is odd.} \end{cases} $$

$f(x, y, \mathrm{Exact}) = x$, but panics if $q \notin \N$.

The following two expressions are equivalent:

  • x.round_to_multiple(other, RoundingMode::Exact)
  • { assert!(x.divisible_by(other)); x }

but the latter should be used as it is clearer and more efficient.

Worst-case complexity

Constant time and additional memory.

Panics
  • If rm is Exact, but self is not a multiple of other.
  • If the multiple is outside the representable range.
  • If self is nonzero, other is zero, and rm is trying to round away from zero.
Examples

See here.

Rounds a number to a multiple of another number, according to a specified rounding mode.

The only rounding modes that are guaranteed to return without a panic are Down and Floor.

Let $q = \frac{x}{y}$:

$f(x, y, \mathrm{Down}) = f(x, y, \mathrm{Floor}) = y \lfloor q \rfloor.$

$f(x, y, \mathrm{Up}) = f(x, y, \mathrm{Ceiling}) = y \lceil q \rceil.$

$$ f(x, y, \mathrm{Nearest}) = \begin{cases} y \lfloor q \rfloor & \text{if} \quad q - \lfloor q \rfloor < \frac{1}{2} \\ y \lceil q \rceil & \text{if} \quad q - \lfloor q \rfloor > \frac{1}{2} \\ y \lfloor q \rfloor & \text{if} \quad q - \lfloor q \rfloor = \frac{1}{2} \ \text{and} \ \lfloor q \rfloor \ \text{is even} \\ y \lceil q \rceil & \text{if} \quad q - \lfloor q \rfloor = \frac{1}{2} \ \text{and} \ \lfloor q \rfloor \ \text{is odd.} \end{cases} $$

$f(x, y, \mathrm{Exact}) = x$, but panics if $q \notin \N$.

The following two expressions are equivalent:

  • x.round_to_multiple(other, RoundingMode::Exact)
  • { assert!(x.divisible_by(other)); x }

but the latter should be used as it is clearer and more efficient.

Worst-case complexity

Constant time and additional memory.

Panics
  • If rm is Exact, but self is not a multiple of other.
  • If the multiple is outside the representable range.
  • If self is nonzero, other is zero, and rm is trying to round away from zero.
Examples

See here.

Rounds a number to a multiple of another number, according to a specified rounding mode.

The only rounding modes that are guaranteed to return without a panic are Down and Floor.

Let $q = \frac{x}{y}$:

$f(x, y, \mathrm{Down}) = f(x, y, \mathrm{Floor}) = y \lfloor q \rfloor.$

$f(x, y, \mathrm{Up}) = f(x, y, \mathrm{Ceiling}) = y \lceil q \rceil.$

$$ f(x, y, \mathrm{Nearest}) = \begin{cases} y \lfloor q \rfloor & \text{if} \quad q - \lfloor q \rfloor < \frac{1}{2} \\ y \lceil q \rceil & \text{if} \quad q - \lfloor q \rfloor > \frac{1}{2} \\ y \lfloor q \rfloor & \text{if} \quad q - \lfloor q \rfloor = \frac{1}{2} \ \text{and} \ \lfloor q \rfloor \ \text{is even} \\ y \lceil q \rceil & \text{if} \quad q - \lfloor q \rfloor = \frac{1}{2} \ \text{and} \ \lfloor q \rfloor \ \text{is odd.} \end{cases} $$

$f(x, y, \mathrm{Exact}) = x$, but panics if $q \notin \N$.

The following two expressions are equivalent:

  • x.round_to_multiple(other, RoundingMode::Exact)
  • { assert!(x.divisible_by(other)); x }

but the latter should be used as it is clearer and more efficient.

Worst-case complexity

Constant time and additional memory.

Panics
  • If rm is Exact, but self is not a multiple of other.
  • If the multiple is outside the representable range.
  • If self is nonzero, other is zero, and rm is trying to round away from zero.
Examples

See here.

Rounds a number to a multiple of another number, according to a specified rounding mode.

The only rounding modes that are guaranteed to return without a panic are Down and Floor.

Let $q = \frac{x}{y}$:

$f(x, y, \mathrm{Down}) = f(x, y, \mathrm{Floor}) = y \lfloor q \rfloor.$

$f(x, y, \mathrm{Up}) = f(x, y, \mathrm{Ceiling}) = y \lceil q \rceil.$

$$ f(x, y, \mathrm{Nearest}) = \begin{cases} y \lfloor q \rfloor & \text{if} \quad q - \lfloor q \rfloor < \frac{1}{2} \\ y \lceil q \rceil & \text{if} \quad q - \lfloor q \rfloor > \frac{1}{2} \\ y \lfloor q \rfloor & \text{if} \quad q - \lfloor q \rfloor = \frac{1}{2} \ \text{and} \ \lfloor q \rfloor \ \text{is even} \\ y \lceil q \rceil & \text{if} \quad q - \lfloor q \rfloor = \frac{1}{2} \ \text{and} \ \lfloor q \rfloor \ \text{is odd.} \end{cases} $$

$f(x, y, \mathrm{Exact}) = x$, but panics if $q \notin \N$.

The following two expressions are equivalent:

  • x.round_to_multiple(other, RoundingMode::Exact)
  • { assert!(x.divisible_by(other)); x }

but the latter should be used as it is clearer and more efficient.

Worst-case complexity

Constant time and additional memory.

Panics
  • If rm is Exact, but self is not a multiple of other.
  • If the multiple is outside the representable range.
  • If self is nonzero, other is zero, and rm is trying to round away from zero.
Examples

See here.

Rounds a number to a multiple of another number, according to a specified rounding mode.

The only rounding mode that is guaranteed to return without a panic is Down.

Let $q = \frac{x}{|y|}$:

$f(x, y, \mathrm{Down}) = \operatorname{sgn}(q) |y| \lfloor |q| \rfloor.$

$f(x, y, \mathrm{Up}) = \operatorname{sgn}(q) |y| \lceil |q| \rceil.$

$f(x, y, \mathrm{Floor}) = |y| \lfloor q \rfloor.$

$f(x, y, \mathrm{Ceiling}) = |y| \lceil q \rceil.$

$$ f(x, y, \mathrm{Nearest}) = \begin{cases} y \lfloor q \rfloor & \text{if} \quad q - \lfloor q \rfloor < \frac{1}{2} \\ y \lceil q \rceil & \text{if} \quad q - \lfloor q \rfloor > \frac{1}{2} \\ y \lfloor q \rfloor & \text{if} \quad q - \lfloor q \rfloor = \frac{1}{2} \ \text{and} \ \lfloor q \rfloor \ \text{is even} \\ y \lceil q \rceil & \text{if} \quad q - \lfloor q \rfloor = \frac{1}{2} \ \text{and} \ \lfloor q \rfloor \ \text{is odd.} \end{cases} $$

$f(x, y, \mathrm{Exact}) = q$, but panics if $q \notin \Z$.

The following two expressions are equivalent:

  • x.round_to_multiple(other, RoundingMode::Exact)
  • { assert!(x.divisible_by(other)); x }

but the latter should be used as it is clearer and more efficient.

Worst-case complexity

Constant time and additional memory.

Panics
  • If rm is Exact, but self is not a multiple of other.
  • If the multiple is outside the representable range.
  • If self is nonzero, other is zero, and rm is trying to round away from zero.
Examples

See here.

Rounds a number to a multiple of another number, according to a specified rounding mode.

The only rounding mode that is guaranteed to return without a panic is Down.

Let $q = \frac{x}{|y|}$:

$f(x, y, \mathrm{Down}) = \operatorname{sgn}(q) |y| \lfloor |q| \rfloor.$

$f(x, y, \mathrm{Up}) = \operatorname{sgn}(q) |y| \lceil |q| \rceil.$

$f(x, y, \mathrm{Floor}) = |y| \lfloor q \rfloor.$

$f(x, y, \mathrm{Ceiling}) = |y| \lceil q \rceil.$

$$ f(x, y, \mathrm{Nearest}) = \begin{cases} y \lfloor q \rfloor & \text{if} \quad q - \lfloor q \rfloor < \frac{1}{2} \\ y \lceil q \rceil & \text{if} \quad q - \lfloor q \rfloor > \frac{1}{2} \\ y \lfloor q \rfloor & \text{if} \quad q - \lfloor q \rfloor = \frac{1}{2} \ \text{and} \ \lfloor q \rfloor \ \text{is even} \\ y \lceil q \rceil & \text{if} \quad q - \lfloor q \rfloor = \frac{1}{2} \ \text{and} \ \lfloor q \rfloor \ \text{is odd.} \end{cases} $$

$f(x, y, \mathrm{Exact}) = q$, but panics if $q \notin \Z$.

The following two expressions are equivalent:

  • x.round_to_multiple(other, RoundingMode::Exact)
  • { assert!(x.divisible_by(other)); x }

but the latter should be used as it is clearer and more efficient.

Worst-case complexity

Constant time and additional memory.

Panics
  • If rm is Exact, but self is not a multiple of other.
  • If the multiple is outside the representable range.
  • If self is nonzero, other is zero, and rm is trying to round away from zero.
Examples

See here.

Rounds a number to a multiple of another number, according to a specified rounding mode.

The only rounding mode that is guaranteed to return without a panic is Down.

Let $q = \frac{x}{|y|}$:

$f(x, y, \mathrm{Down}) = \operatorname{sgn}(q) |y| \lfloor |q| \rfloor.$

$f(x, y, \mathrm{Up}) = \operatorname{sgn}(q) |y| \lceil |q| \rceil.$

$f(x, y, \mathrm{Floor}) = |y| \lfloor q \rfloor.$

$f(x, y, \mathrm{Ceiling}) = |y| \lceil q \rceil.$

$$ f(x, y, \mathrm{Nearest}) = \begin{cases} y \lfloor q \rfloor & \text{if} \quad q - \lfloor q \rfloor < \frac{1}{2} \\ y \lceil q \rceil & \text{if} \quad q - \lfloor q \rfloor > \frac{1}{2} \\ y \lfloor q \rfloor & \text{if} \quad q - \lfloor q \rfloor = \frac{1}{2} \ \text{and} \ \lfloor q \rfloor \ \text{is even} \\ y \lceil q \rceil & \text{if} \quad q - \lfloor q \rfloor = \frac{1}{2} \ \text{and} \ \lfloor q \rfloor \ \text{is odd.} \end{cases} $$

$f(x, y, \mathrm{Exact}) = q$, but panics if $q \notin \Z$.

The following two expressions are equivalent:

  • x.round_to_multiple(other, RoundingMode::Exact)
  • { assert!(x.divisible_by(other)); x }

but the latter should be used as it is clearer and more efficient.

Worst-case complexity

Constant time and additional memory.

Panics
  • If rm is Exact, but self is not a multiple of other.
  • If the multiple is outside the representable range.
  • If self is nonzero, other is zero, and rm is trying to round away from zero.
Examples

See here.

Rounds a number to a multiple of another number, according to a specified rounding mode.

The only rounding mode that is guaranteed to return without a panic is Down.

Let $q = \frac{x}{|y|}$:

$f(x, y, \mathrm{Down}) = \operatorname{sgn}(q) |y| \lfloor |q| \rfloor.$

$f(x, y, \mathrm{Up}) = \operatorname{sgn}(q) |y| \lceil |q| \rceil.$

$f(x, y, \mathrm{Floor}) = |y| \lfloor q \rfloor.$

$f(x, y, \mathrm{Ceiling}) = |y| \lceil q \rceil.$

$$ f(x, y, \mathrm{Nearest}) = \begin{cases} y \lfloor q \rfloor & \text{if} \quad q - \lfloor q \rfloor < \frac{1}{2} \\ y \lceil q \rceil & \text{if} \quad q - \lfloor q \rfloor > \frac{1}{2} \\ y \lfloor q \rfloor & \text{if} \quad q - \lfloor q \rfloor = \frac{1}{2} \ \text{and} \ \lfloor q \rfloor \ \text{is even} \\ y \lceil q \rceil & \text{if} \quad q - \lfloor q \rfloor = \frac{1}{2} \ \text{and} \ \lfloor q \rfloor \ \text{is odd.} \end{cases} $$

$f(x, y, \mathrm{Exact}) = q$, but panics if $q \notin \Z$.

The following two expressions are equivalent:

  • x.round_to_multiple(other, RoundingMode::Exact)
  • { assert!(x.divisible_by(other)); x }

but the latter should be used as it is clearer and more efficient.

Worst-case complexity

Constant time and additional memory.

Panics
  • If rm is Exact, but self is not a multiple of other.
  • If the multiple is outside the representable range.
  • If self is nonzero, other is zero, and rm is trying to round away from zero.
Examples

See here.

Rounds a number to a multiple of another number, according to a specified rounding mode.

The only rounding mode that is guaranteed to return without a panic is Down.

Let $q = \frac{x}{|y|}$:

$f(x, y, \mathrm{Down}) = \operatorname{sgn}(q) |y| \lfloor |q| \rfloor.$

$f(x, y, \mathrm{Up}) = \operatorname{sgn}(q) |y| \lceil |q| \rceil.$

$f(x, y, \mathrm{Floor}) = |y| \lfloor q \rfloor.$

$f(x, y, \mathrm{Ceiling}) = |y| \lceil q \rceil.$

$$ f(x, y, \mathrm{Nearest}) = \begin{cases} y \lfloor q \rfloor & \text{if} \quad q - \lfloor q \rfloor < \frac{1}{2} \\ y \lceil q \rceil & \text{if} \quad q - \lfloor q \rfloor > \frac{1}{2} \\ y \lfloor q \rfloor & \text{if} \quad q - \lfloor q \rfloor = \frac{1}{2} \ \text{and} \ \lfloor q \rfloor \ \text{is even} \\ y \lceil q \rceil & \text{if} \quad q - \lfloor q \rfloor = \frac{1}{2} \ \text{and} \ \lfloor q \rfloor \ \text{is odd.} \end{cases} $$

$f(x, y, \mathrm{Exact}) = q$, but panics if $q \notin \Z$.

The following two expressions are equivalent:

  • x.round_to_multiple(other, RoundingMode::Exact)
  • { assert!(x.divisible_by(other)); x }

but the latter should be used as it is clearer and more efficient.

Worst-case complexity

Constant time and additional memory.

Panics
  • If rm is Exact, but self is not a multiple of other.
  • If the multiple is outside the representable range.
  • If self is nonzero, other is zero, and rm is trying to round away from zero.
Examples

See here.

Rounds a number to a multiple of another number, according to a specified rounding mode.

The only rounding mode that is guaranteed to return without a panic is Down.

Let $q = \frac{x}{|y|}$:

$f(x, y, \mathrm{Down}) = \operatorname{sgn}(q) |y| \lfloor |q| \rfloor.$

$f(x, y, \mathrm{Up}) = \operatorname{sgn}(q) |y| \lceil |q| \rceil.$

$f(x, y, \mathrm{Floor}) = |y| \lfloor q \rfloor.$

$f(x, y, \mathrm{Ceiling}) = |y| \lceil q \rceil.$

$$ f(x, y, \mathrm{Nearest}) = \begin{cases} y \lfloor q \rfloor & \text{if} \quad q - \lfloor q \rfloor < \frac{1}{2} \\ y \lceil q \rceil & \text{if} \quad q - \lfloor q \rfloor > \frac{1}{2} \\ y \lfloor q \rfloor & \text{if} \quad q - \lfloor q \rfloor = \frac{1}{2} \ \text{and} \ \lfloor q \rfloor \ \text{is even} \\ y \lceil q \rceil & \text{if} \quad q - \lfloor q \rfloor = \frac{1}{2} \ \text{and} \ \lfloor q \rfloor \ \text{is odd.} \end{cases} $$

$f(x, y, \mathrm{Exact}) = q$, but panics if $q \notin \Z$.

The following two expressions are equivalent:

  • x.round_to_multiple(other, RoundingMode::Exact)
  • { assert!(x.divisible_by(other)); x }

but the latter should be used as it is clearer and more efficient.

Worst-case complexity

Constant time and additional memory.

Panics
  • If rm is Exact, but self is not a multiple of other.
  • If the multiple is outside the representable range.
  • If self is nonzero, other is zero, and rm is trying to round away from zero.
Examples

See here.

Implementors