pub trait AverageRound<RHS = Self> {
type Output;
// Required method
fn average_round(
self,
other: RHS,
rm: RoundingMode,
) -> (Self::Output, Ordering);
}Expand description
Computes the average (arithmetic mean) of two numbers and rounds according to a specified
rounding mode. An Ordering is also returned, indicating whether the returned value is less
than, equal to, or greater than the exact value.
The average is computed without overflow: the result is always exact or within a half of the exact value, so it always fits in the same type as the inputs.
Required Associated Types§
Required Methods§
fn average_round(self, other: RHS, rm: RoundingMode) -> (Self::Output, Ordering)
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Source§impl AverageRound for i8
impl AverageRound for i8
Source§fn average_round(self, other: i8, rm: RoundingMode) -> (i8, Ordering)
fn average_round(self, other: i8, rm: RoundingMode) -> (i8, Ordering)
Computes the average (arithmetic mean) of two numbers and rounds according to a
specified rounding mode. An Ordering is also returned, indicating whether the
returned value is less than, equal to, or greater than the exact value.
The average is computed without overflow; the result always fits in the same type as the inputs.
Let $a = \frac{x + y}{2}$, and let $g$ be the function that just returns the first
element of the pair, without the Ordering. Since $a$ is either an integer or a
half more than an integer,
$$ g(x, y, \mathrm{Floor}) = \lfloor a \rfloor, $$
$$ g(x, y, \mathrm{Ceiling}) = \lceil a \rceil, $$
$$ g(x, y, \mathrm{Down}) = \operatorname{sgn}(a) \lfloor |a| \rfloor, $$
$$ g(x, y, \mathrm{Up}) = \operatorname{sgn}(a) \lceil |a| \rceil, $$
$$ g(x, y, \mathrm{Nearest}) = \begin{cases} a & \text{if} \quad a \in \Z, \\ \lfloor a \rfloor & \text{if} \quad a \notin \Z \ \text{and} \ \lfloor a \rfloor \ \text{is even}, \\ \lceil a \rceil & \text{if} \quad a \notin \Z \ \text{and} \ \lfloor a \rfloor \ \text{is odd,} \end{cases} $$
and $g(x, y, \mathrm{Exact}) = a$, but panics if $a \notin \Z$.
Then
$f(x, y, r) = (g(x, y, r), \operatorname{cmp}(g(x, y, r), a))$.
§Worst-case complexity
Constant time and additional memory.
§Panics
Panics if rm is Exact but the average of self and other is not an integer.
§Examples
See here.
type Output = i8
Source§impl AverageRound for i16
impl AverageRound for i16
Source§fn average_round(self, other: i16, rm: RoundingMode) -> (i16, Ordering)
fn average_round(self, other: i16, rm: RoundingMode) -> (i16, Ordering)
Computes the average (arithmetic mean) of two numbers and rounds according to a
specified rounding mode. An Ordering is also returned, indicating whether the
returned value is less than, equal to, or greater than the exact value.
The average is computed without overflow; the result always fits in the same type as the inputs.
Let $a = \frac{x + y}{2}$, and let $g$ be the function that just returns the first
element of the pair, without the Ordering. Since $a$ is either an integer or a
half more than an integer,
$$ g(x, y, \mathrm{Floor}) = \lfloor a \rfloor, $$
$$ g(x, y, \mathrm{Ceiling}) = \lceil a \rceil, $$
$$ g(x, y, \mathrm{Down}) = \operatorname{sgn}(a) \lfloor |a| \rfloor, $$
$$ g(x, y, \mathrm{Up}) = \operatorname{sgn}(a) \lceil |a| \rceil, $$
$$ g(x, y, \mathrm{Nearest}) = \begin{cases} a & \text{if} \quad a \in \Z, \\ \lfloor a \rfloor & \text{if} \quad a \notin \Z \ \text{and} \ \lfloor a \rfloor \ \text{is even}, \\ \lceil a \rceil & \text{if} \quad a \notin \Z \ \text{and} \ \lfloor a \rfloor \ \text{is odd,} \end{cases} $$
and $g(x, y, \mathrm{Exact}) = a$, but panics if $a \notin \Z$.
Then
$f(x, y, r) = (g(x, y, r), \operatorname{cmp}(g(x, y, r), a))$.
§Worst-case complexity
Constant time and additional memory.
§Panics
Panics if rm is Exact but the average of self and other is not an integer.
§Examples
See here.
type Output = i16
Source§impl AverageRound for i32
impl AverageRound for i32
Source§fn average_round(self, other: i32, rm: RoundingMode) -> (i32, Ordering)
fn average_round(self, other: i32, rm: RoundingMode) -> (i32, Ordering)
Computes the average (arithmetic mean) of two numbers and rounds according to a
specified rounding mode. An Ordering is also returned, indicating whether the
returned value is less than, equal to, or greater than the exact value.
The average is computed without overflow; the result always fits in the same type as the inputs.
Let $a = \frac{x + y}{2}$, and let $g$ be the function that just returns the first
element of the pair, without the Ordering. Since $a$ is either an integer or a
half more than an integer,
$$ g(x, y, \mathrm{Floor}) = \lfloor a \rfloor, $$
$$ g(x, y, \mathrm{Ceiling}) = \lceil a \rceil, $$
$$ g(x, y, \mathrm{Down}) = \operatorname{sgn}(a) \lfloor |a| \rfloor, $$
$$ g(x, y, \mathrm{Up}) = \operatorname{sgn}(a) \lceil |a| \rceil, $$
$$ g(x, y, \mathrm{Nearest}) = \begin{cases} a & \text{if} \quad a \in \Z, \\ \lfloor a \rfloor & \text{if} \quad a \notin \Z \ \text{and} \ \lfloor a \rfloor \ \text{is even}, \\ \lceil a \rceil & \text{if} \quad a \notin \Z \ \text{and} \ \lfloor a \rfloor \ \text{is odd,} \end{cases} $$
and $g(x, y, \mathrm{Exact}) = a$, but panics if $a \notin \Z$.
Then
$f(x, y, r) = (g(x, y, r), \operatorname{cmp}(g(x, y, r), a))$.
§Worst-case complexity
Constant time and additional memory.
§Panics
Panics if rm is Exact but the average of self and other is not an integer.
§Examples
See here.
type Output = i32
Source§impl AverageRound for i64
impl AverageRound for i64
Source§fn average_round(self, other: i64, rm: RoundingMode) -> (i64, Ordering)
fn average_round(self, other: i64, rm: RoundingMode) -> (i64, Ordering)
Computes the average (arithmetic mean) of two numbers and rounds according to a
specified rounding mode. An Ordering is also returned, indicating whether the
returned value is less than, equal to, or greater than the exact value.
The average is computed without overflow; the result always fits in the same type as the inputs.
Let $a = \frac{x + y}{2}$, and let $g$ be the function that just returns the first
element of the pair, without the Ordering. Since $a$ is either an integer or a
half more than an integer,
$$ g(x, y, \mathrm{Floor}) = \lfloor a \rfloor, $$
$$ g(x, y, \mathrm{Ceiling}) = \lceil a \rceil, $$
$$ g(x, y, \mathrm{Down}) = \operatorname{sgn}(a) \lfloor |a| \rfloor, $$
$$ g(x, y, \mathrm{Up}) = \operatorname{sgn}(a) \lceil |a| \rceil, $$
$$ g(x, y, \mathrm{Nearest}) = \begin{cases} a & \text{if} \quad a \in \Z, \\ \lfloor a \rfloor & \text{if} \quad a \notin \Z \ \text{and} \ \lfloor a \rfloor \ \text{is even}, \\ \lceil a \rceil & \text{if} \quad a \notin \Z \ \text{and} \ \lfloor a \rfloor \ \text{is odd,} \end{cases} $$
and $g(x, y, \mathrm{Exact}) = a$, but panics if $a \notin \Z$.
Then
$f(x, y, r) = (g(x, y, r), \operatorname{cmp}(g(x, y, r), a))$.
§Worst-case complexity
Constant time and additional memory.
§Panics
Panics if rm is Exact but the average of self and other is not an integer.
§Examples
See here.
type Output = i64
Source§impl AverageRound for i128
impl AverageRound for i128
Source§fn average_round(self, other: i128, rm: RoundingMode) -> (i128, Ordering)
fn average_round(self, other: i128, rm: RoundingMode) -> (i128, Ordering)
Computes the average (arithmetic mean) of two numbers and rounds according to a
specified rounding mode. An Ordering is also returned, indicating whether the
returned value is less than, equal to, or greater than the exact value.
The average is computed without overflow; the result always fits in the same type as the inputs.
Let $a = \frac{x + y}{2}$, and let $g$ be the function that just returns the first
element of the pair, without the Ordering. Since $a$ is either an integer or a
half more than an integer,
$$ g(x, y, \mathrm{Floor}) = \lfloor a \rfloor, $$
$$ g(x, y, \mathrm{Ceiling}) = \lceil a \rceil, $$
$$ g(x, y, \mathrm{Down}) = \operatorname{sgn}(a) \lfloor |a| \rfloor, $$
$$ g(x, y, \mathrm{Up}) = \operatorname{sgn}(a) \lceil |a| \rceil, $$
$$ g(x, y, \mathrm{Nearest}) = \begin{cases} a & \text{if} \quad a \in \Z, \\ \lfloor a \rfloor & \text{if} \quad a \notin \Z \ \text{and} \ \lfloor a \rfloor \ \text{is even}, \\ \lceil a \rceil & \text{if} \quad a \notin \Z \ \text{and} \ \lfloor a \rfloor \ \text{is odd,} \end{cases} $$
and $g(x, y, \mathrm{Exact}) = a$, but panics if $a \notin \Z$.
Then
$f(x, y, r) = (g(x, y, r), \operatorname{cmp}(g(x, y, r), a))$.
§Worst-case complexity
Constant time and additional memory.
§Panics
Panics if rm is Exact but the average of self and other is not an integer.
§Examples
See here.
type Output = i128
Source§impl AverageRound for isize
impl AverageRound for isize
Source§fn average_round(self, other: isize, rm: RoundingMode) -> (isize, Ordering)
fn average_round(self, other: isize, rm: RoundingMode) -> (isize, Ordering)
Computes the average (arithmetic mean) of two numbers and rounds according to a
specified rounding mode. An Ordering is also returned, indicating whether the
returned value is less than, equal to, or greater than the exact value.
The average is computed without overflow; the result always fits in the same type as the inputs.
Let $a = \frac{x + y}{2}$, and let $g$ be the function that just returns the first
element of the pair, without the Ordering. Since $a$ is either an integer or a
half more than an integer,
$$ g(x, y, \mathrm{Floor}) = \lfloor a \rfloor, $$
$$ g(x, y, \mathrm{Ceiling}) = \lceil a \rceil, $$
$$ g(x, y, \mathrm{Down}) = \operatorname{sgn}(a) \lfloor |a| \rfloor, $$
$$ g(x, y, \mathrm{Up}) = \operatorname{sgn}(a) \lceil |a| \rceil, $$
$$ g(x, y, \mathrm{Nearest}) = \begin{cases} a & \text{if} \quad a \in \Z, \\ \lfloor a \rfloor & \text{if} \quad a \notin \Z \ \text{and} \ \lfloor a \rfloor \ \text{is even}, \\ \lceil a \rceil & \text{if} \quad a \notin \Z \ \text{and} \ \lfloor a \rfloor \ \text{is odd,} \end{cases} $$
and $g(x, y, \mathrm{Exact}) = a$, but panics if $a \notin \Z$.
Then
$f(x, y, r) = (g(x, y, r), \operatorname{cmp}(g(x, y, r), a))$.
§Worst-case complexity
Constant time and additional memory.
§Panics
Panics if rm is Exact but the average of self and other is not an integer.
§Examples
See here.
type Output = isize
Source§impl AverageRound for u8
impl AverageRound for u8
Source§fn average_round(self, other: u8, rm: RoundingMode) -> (u8, Ordering)
fn average_round(self, other: u8, rm: RoundingMode) -> (u8, Ordering)
Computes the average (arithmetic mean) of two numbers and rounds according to a
specified rounding mode. An Ordering is also returned, indicating whether the
returned value is less than, equal to, or greater than the exact value.
The average is computed without overflow; the result always fits in the same type as the inputs.
Let $a = \frac{x + y}{2}$, and let $g$ be the function that just returns the first
element of the pair, without the Ordering. Since $a$ is either an integer or a
half more than an integer,
$$ g(x, y, \mathrm{Floor}) = \lfloor a \rfloor, $$
$$ g(x, y, \mathrm{Ceiling}) = \lceil a \rceil, $$
$$ g(x, y, \mathrm{Down}) = \operatorname{sgn}(a) \lfloor |a| \rfloor, $$
$$ g(x, y, \mathrm{Up}) = \operatorname{sgn}(a) \lceil |a| \rceil, $$
$$ g(x, y, \mathrm{Nearest}) = \begin{cases} a & \text{if} \quad a \in \Z, \\ \lfloor a \rfloor & \text{if} \quad a \notin \Z \ \text{and} \ \lfloor a \rfloor \ \text{is even}, \\ \lceil a \rceil & \text{if} \quad a \notin \Z \ \text{and} \ \lfloor a \rfloor \ \text{is odd,} \end{cases} $$
and $g(x, y, \mathrm{Exact}) = a$, but panics if $a \notin \Z$.
Then
$f(x, y, r) = (g(x, y, r), \operatorname{cmp}(g(x, y, r), a))$.
§Worst-case complexity
Constant time and additional memory.
§Panics
Panics if rm is Exact but the average of self and other is not an integer.
§Examples
See here.
type Output = u8
Source§impl AverageRound for u16
impl AverageRound for u16
Source§fn average_round(self, other: u16, rm: RoundingMode) -> (u16, Ordering)
fn average_round(self, other: u16, rm: RoundingMode) -> (u16, Ordering)
Computes the average (arithmetic mean) of two numbers and rounds according to a
specified rounding mode. An Ordering is also returned, indicating whether the
returned value is less than, equal to, or greater than the exact value.
The average is computed without overflow; the result always fits in the same type as the inputs.
Let $a = \frac{x + y}{2}$, and let $g$ be the function that just returns the first
element of the pair, without the Ordering. Since $a$ is either an integer or a
half more than an integer,
$$ g(x, y, \mathrm{Floor}) = \lfloor a \rfloor, $$
$$ g(x, y, \mathrm{Ceiling}) = \lceil a \rceil, $$
$$ g(x, y, \mathrm{Down}) = \operatorname{sgn}(a) \lfloor |a| \rfloor, $$
$$ g(x, y, \mathrm{Up}) = \operatorname{sgn}(a) \lceil |a| \rceil, $$
$$ g(x, y, \mathrm{Nearest}) = \begin{cases} a & \text{if} \quad a \in \Z, \\ \lfloor a \rfloor & \text{if} \quad a \notin \Z \ \text{and} \ \lfloor a \rfloor \ \text{is even}, \\ \lceil a \rceil & \text{if} \quad a \notin \Z \ \text{and} \ \lfloor a \rfloor \ \text{is odd,} \end{cases} $$
and $g(x, y, \mathrm{Exact}) = a$, but panics if $a \notin \Z$.
Then
$f(x, y, r) = (g(x, y, r), \operatorname{cmp}(g(x, y, r), a))$.
§Worst-case complexity
Constant time and additional memory.
§Panics
Panics if rm is Exact but the average of self and other is not an integer.
§Examples
See here.
type Output = u16
Source§impl AverageRound for u32
impl AverageRound for u32
Source§fn average_round(self, other: u32, rm: RoundingMode) -> (u32, Ordering)
fn average_round(self, other: u32, rm: RoundingMode) -> (u32, Ordering)
Computes the average (arithmetic mean) of two numbers and rounds according to a
specified rounding mode. An Ordering is also returned, indicating whether the
returned value is less than, equal to, or greater than the exact value.
The average is computed without overflow; the result always fits in the same type as the inputs.
Let $a = \frac{x + y}{2}$, and let $g$ be the function that just returns the first
element of the pair, without the Ordering. Since $a$ is either an integer or a
half more than an integer,
$$ g(x, y, \mathrm{Floor}) = \lfloor a \rfloor, $$
$$ g(x, y, \mathrm{Ceiling}) = \lceil a \rceil, $$
$$ g(x, y, \mathrm{Down}) = \operatorname{sgn}(a) \lfloor |a| \rfloor, $$
$$ g(x, y, \mathrm{Up}) = \operatorname{sgn}(a) \lceil |a| \rceil, $$
$$ g(x, y, \mathrm{Nearest}) = \begin{cases} a & \text{if} \quad a \in \Z, \\ \lfloor a \rfloor & \text{if} \quad a \notin \Z \ \text{and} \ \lfloor a \rfloor \ \text{is even}, \\ \lceil a \rceil & \text{if} \quad a \notin \Z \ \text{and} \ \lfloor a \rfloor \ \text{is odd,} \end{cases} $$
and $g(x, y, \mathrm{Exact}) = a$, but panics if $a \notin \Z$.
Then
$f(x, y, r) = (g(x, y, r), \operatorname{cmp}(g(x, y, r), a))$.
§Worst-case complexity
Constant time and additional memory.
§Panics
Panics if rm is Exact but the average of self and other is not an integer.
§Examples
See here.
type Output = u32
Source§impl AverageRound for u64
impl AverageRound for u64
Source§fn average_round(self, other: u64, rm: RoundingMode) -> (u64, Ordering)
fn average_round(self, other: u64, rm: RoundingMode) -> (u64, Ordering)
Computes the average (arithmetic mean) of two numbers and rounds according to a
specified rounding mode. An Ordering is also returned, indicating whether the
returned value is less than, equal to, or greater than the exact value.
The average is computed without overflow; the result always fits in the same type as the inputs.
Let $a = \frac{x + y}{2}$, and let $g$ be the function that just returns the first
element of the pair, without the Ordering. Since $a$ is either an integer or a
half more than an integer,
$$ g(x, y, \mathrm{Floor}) = \lfloor a \rfloor, $$
$$ g(x, y, \mathrm{Ceiling}) = \lceil a \rceil, $$
$$ g(x, y, \mathrm{Down}) = \operatorname{sgn}(a) \lfloor |a| \rfloor, $$
$$ g(x, y, \mathrm{Up}) = \operatorname{sgn}(a) \lceil |a| \rceil, $$
$$ g(x, y, \mathrm{Nearest}) = \begin{cases} a & \text{if} \quad a \in \Z, \\ \lfloor a \rfloor & \text{if} \quad a \notin \Z \ \text{and} \ \lfloor a \rfloor \ \text{is even}, \\ \lceil a \rceil & \text{if} \quad a \notin \Z \ \text{and} \ \lfloor a \rfloor \ \text{is odd,} \end{cases} $$
and $g(x, y, \mathrm{Exact}) = a$, but panics if $a \notin \Z$.
Then
$f(x, y, r) = (g(x, y, r), \operatorname{cmp}(g(x, y, r), a))$.
§Worst-case complexity
Constant time and additional memory.
§Panics
Panics if rm is Exact but the average of self and other is not an integer.
§Examples
See here.
type Output = u64
Source§impl AverageRound for u128
impl AverageRound for u128
Source§fn average_round(self, other: u128, rm: RoundingMode) -> (u128, Ordering)
fn average_round(self, other: u128, rm: RoundingMode) -> (u128, Ordering)
Computes the average (arithmetic mean) of two numbers and rounds according to a
specified rounding mode. An Ordering is also returned, indicating whether the
returned value is less than, equal to, or greater than the exact value.
The average is computed without overflow; the result always fits in the same type as the inputs.
Let $a = \frac{x + y}{2}$, and let $g$ be the function that just returns the first
element of the pair, without the Ordering. Since $a$ is either an integer or a
half more than an integer,
$$ g(x, y, \mathrm{Floor}) = \lfloor a \rfloor, $$
$$ g(x, y, \mathrm{Ceiling}) = \lceil a \rceil, $$
$$ g(x, y, \mathrm{Down}) = \operatorname{sgn}(a) \lfloor |a| \rfloor, $$
$$ g(x, y, \mathrm{Up}) = \operatorname{sgn}(a) \lceil |a| \rceil, $$
$$ g(x, y, \mathrm{Nearest}) = \begin{cases} a & \text{if} \quad a \in \Z, \\ \lfloor a \rfloor & \text{if} \quad a \notin \Z \ \text{and} \ \lfloor a \rfloor \ \text{is even}, \\ \lceil a \rceil & \text{if} \quad a \notin \Z \ \text{and} \ \lfloor a \rfloor \ \text{is odd,} \end{cases} $$
and $g(x, y, \mathrm{Exact}) = a$, but panics if $a \notin \Z$.
Then
$f(x, y, r) = (g(x, y, r), \operatorname{cmp}(g(x, y, r), a))$.
§Worst-case complexity
Constant time and additional memory.
§Panics
Panics if rm is Exact but the average of self and other is not an integer.
§Examples
See here.
type Output = u128
Source§impl AverageRound for usize
impl AverageRound for usize
Source§fn average_round(self, other: usize, rm: RoundingMode) -> (usize, Ordering)
fn average_round(self, other: usize, rm: RoundingMode) -> (usize, Ordering)
Computes the average (arithmetic mean) of two numbers and rounds according to a
specified rounding mode. An Ordering is also returned, indicating whether the
returned value is less than, equal to, or greater than the exact value.
The average is computed without overflow; the result always fits in the same type as the inputs.
Let $a = \frac{x + y}{2}$, and let $g$ be the function that just returns the first
element of the pair, without the Ordering. Since $a$ is either an integer or a
half more than an integer,
$$ g(x, y, \mathrm{Floor}) = \lfloor a \rfloor, $$
$$ g(x, y, \mathrm{Ceiling}) = \lceil a \rceil, $$
$$ g(x, y, \mathrm{Down}) = \operatorname{sgn}(a) \lfloor |a| \rfloor, $$
$$ g(x, y, \mathrm{Up}) = \operatorname{sgn}(a) \lceil |a| \rceil, $$
$$ g(x, y, \mathrm{Nearest}) = \begin{cases} a & \text{if} \quad a \in \Z, \\ \lfloor a \rfloor & \text{if} \quad a \notin \Z \ \text{and} \ \lfloor a \rfloor \ \text{is even}, \\ \lceil a \rceil & \text{if} \quad a \notin \Z \ \text{and} \ \lfloor a \rfloor \ \text{is odd,} \end{cases} $$
and $g(x, y, \mathrm{Exact}) = a$, but panics if $a \notin \Z$.
Then
$f(x, y, r) = (g(x, y, r), \operatorname{cmp}(g(x, y, r), a))$.
§Worst-case complexity
Constant time and additional memory.
§Panics
Panics if rm is Exact but the average of self and other is not an integer.
§Examples
See here.