Trait malachite_base::num::arithmetic::traits::RoundToMultiple
source · [−]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
fn round_to_multiple(self, other: RHS, rm: RoundingMode) -> Self::Output
Implementations on Foreign Types
sourceimpl RoundToMultiple<u8> for u8
impl RoundToMultiple<u8> for u8
sourcefn round_to_multiple(self, other: u8, rm: RoundingMode) -> u8
fn round_to_multiple(self, other: u8, rm: RoundingMode) -> u8
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
isExact
, butself
is not a multiple ofother
. - If the multiple is outside the representable range.
- If
self
is nonzero,other
is zero, andrm
is trying to round away from zero.
Examples
See here.
type Output = u8
sourceimpl RoundToMultiple<u16> for u16
impl RoundToMultiple<u16> for u16
sourcefn round_to_multiple(self, other: u16, rm: RoundingMode) -> u16
fn round_to_multiple(self, other: u16, rm: RoundingMode) -> u16
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
isExact
, butself
is not a multiple ofother
. - If the multiple is outside the representable range.
- If
self
is nonzero,other
is zero, andrm
is trying to round away from zero.
Examples
See here.
type Output = u16
sourceimpl RoundToMultiple<u32> for u32
impl RoundToMultiple<u32> for u32
sourcefn round_to_multiple(self, other: u32, rm: RoundingMode) -> u32
fn round_to_multiple(self, other: u32, rm: RoundingMode) -> u32
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
isExact
, butself
is not a multiple ofother
. - If the multiple is outside the representable range.
- If
self
is nonzero,other
is zero, andrm
is trying to round away from zero.
Examples
See here.
type Output = u32
sourceimpl RoundToMultiple<u64> for u64
impl RoundToMultiple<u64> for u64
sourcefn round_to_multiple(self, other: u64, rm: RoundingMode) -> u64
fn round_to_multiple(self, other: u64, rm: RoundingMode) -> u64
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
isExact
, butself
is not a multiple ofother
. - If the multiple is outside the representable range.
- If
self
is nonzero,other
is zero, andrm
is trying to round away from zero.
Examples
See here.
type Output = u64
sourceimpl RoundToMultiple<u128> for u128
impl RoundToMultiple<u128> for u128
sourcefn round_to_multiple(self, other: u128, rm: RoundingMode) -> u128
fn round_to_multiple(self, other: u128, rm: RoundingMode) -> u128
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
isExact
, butself
is not a multiple ofother
. - If the multiple is outside the representable range.
- If
self
is nonzero,other
is zero, andrm
is trying to round away from zero.
Examples
See here.
type Output = u128
sourceimpl RoundToMultiple<usize> for usize
impl RoundToMultiple<usize> for usize
sourcefn round_to_multiple(self, other: usize, rm: RoundingMode) -> usize
fn round_to_multiple(self, other: usize, rm: RoundingMode) -> usize
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
isExact
, butself
is not a multiple ofother
. - If the multiple is outside the representable range.
- If
self
is nonzero,other
is zero, andrm
is trying to round away from zero.
Examples
See here.
type Output = usize
sourceimpl RoundToMultiple<i8> for i8
impl RoundToMultiple<i8> for i8
sourcefn round_to_multiple(self, other: i8, rm: RoundingMode) -> i8
fn round_to_multiple(self, other: i8, rm: RoundingMode) -> i8
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
isExact
, butself
is not a multiple ofother
. - If the multiple is outside the representable range.
- If
self
is nonzero,other
is zero, andrm
is trying to round away from zero.
Examples
See here.
type Output = i8
sourceimpl RoundToMultiple<i16> for i16
impl RoundToMultiple<i16> for i16
sourcefn round_to_multiple(self, other: i16, rm: RoundingMode) -> i16
fn round_to_multiple(self, other: i16, rm: RoundingMode) -> i16
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
isExact
, butself
is not a multiple ofother
. - If the multiple is outside the representable range.
- If
self
is nonzero,other
is zero, andrm
is trying to round away from zero.
Examples
See here.
type Output = i16
sourceimpl RoundToMultiple<i32> for i32
impl RoundToMultiple<i32> for i32
sourcefn round_to_multiple(self, other: i32, rm: RoundingMode) -> i32
fn round_to_multiple(self, other: i32, rm: RoundingMode) -> i32
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
isExact
, butself
is not a multiple ofother
. - If the multiple is outside the representable range.
- If
self
is nonzero,other
is zero, andrm
is trying to round away from zero.
Examples
See here.
type Output = i32
sourceimpl RoundToMultiple<i64> for i64
impl RoundToMultiple<i64> for i64
sourcefn round_to_multiple(self, other: i64, rm: RoundingMode) -> i64
fn round_to_multiple(self, other: i64, rm: RoundingMode) -> i64
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
isExact
, butself
is not a multiple ofother
. - If the multiple is outside the representable range.
- If
self
is nonzero,other
is zero, andrm
is trying to round away from zero.
Examples
See here.
type Output = i64
sourceimpl RoundToMultiple<i128> for i128
impl RoundToMultiple<i128> for i128
sourcefn round_to_multiple(self, other: i128, rm: RoundingMode) -> i128
fn round_to_multiple(self, other: i128, rm: RoundingMode) -> i128
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
isExact
, butself
is not a multiple ofother
. - If the multiple is outside the representable range.
- If
self
is nonzero,other
is zero, andrm
is trying to round away from zero.
Examples
See here.
type Output = i128
sourceimpl RoundToMultiple<isize> for isize
impl RoundToMultiple<isize> for isize
sourcefn round_to_multiple(self, other: isize, rm: RoundingMode) -> isize
fn round_to_multiple(self, other: isize, rm: RoundingMode) -> isize
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
isExact
, butself
is not a multiple ofother
. - If the multiple is outside the representable range.
- If
self
is nonzero,other
is zero, andrm
is trying to round away from zero.
Examples
See here.