Skip to main content

Average

Trait Average 

Source
pub trait Average<RHS = Self> {
    type Output;

    // Required method
    fn average(self, other: RHS) -> Self::Output;
}
Expand description

Computes the average (arithmetic mean) of two numbers, rounding to the nearest integer. Two-way ties are broken by rounding to the even integer.

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§

Source

fn average(self, other: RHS) -> Self::Output

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl Average for f32

Source§

fn average(self, other: f32) -> f32

Computes the average (arithmetic mean) of two floating-point numbers.

For finite values the result is the correctly rounded average: the nearest representable value, with ties going to the value with the even mantissa. The computation avoids intermediate overflow and underflow, so extreme values average correctly. If either value is infinite or NaN, the result is whatever (x + y) / 2.0 produces.

§Worst-case complexity

Constant time and additional memory.

§Examples

See here.

Source§

type Output = f32

Source§

impl Average for f64

Source§

fn average(self, other: f64) -> f64

Computes the average (arithmetic mean) of two floating-point numbers.

For finite values the result is the correctly rounded average: the nearest representable value, with ties going to the value with the even mantissa. The computation avoids intermediate overflow and underflow, so extreme values average correctly. If either value is infinite or NaN, the result is whatever (x + y) / 2.0 produces.

§Worst-case complexity

Constant time and additional memory.

§Examples

See here.

Source§

type Output = f64

Source§

impl Average for i8

Source§

fn average(self, other: i8) -> i8

Computes the average (arithmetic mean) of two numbers, rounding to the nearest integer. Two-way ties are broken by rounding to the even integer.

The average is computed without overflow; the result always fits in the same type as the inputs. This is equivalent to average_round with Nearest.

$$ f(x, y) = \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} $$

where $a = \frac{x + y}{2}$.

§Worst-case complexity

Constant time and additional memory.

§Examples

See here.

Source§

type Output = i8

Source§

impl Average for i16

Source§

fn average(self, other: i16) -> i16

Computes the average (arithmetic mean) of two numbers, rounding to the nearest integer. Two-way ties are broken by rounding to the even integer.

The average is computed without overflow; the result always fits in the same type as the inputs. This is equivalent to average_round with Nearest.

$$ f(x, y) = \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} $$

where $a = \frac{x + y}{2}$.

§Worst-case complexity

Constant time and additional memory.

§Examples

See here.

Source§

type Output = i16

Source§

impl Average for i32

Source§

fn average(self, other: i32) -> i32

Computes the average (arithmetic mean) of two numbers, rounding to the nearest integer. Two-way ties are broken by rounding to the even integer.

The average is computed without overflow; the result always fits in the same type as the inputs. This is equivalent to average_round with Nearest.

$$ f(x, y) = \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} $$

where $a = \frac{x + y}{2}$.

§Worst-case complexity

Constant time and additional memory.

§Examples

See here.

Source§

type Output = i32

Source§

impl Average for i64

Source§

fn average(self, other: i64) -> i64

Computes the average (arithmetic mean) of two numbers, rounding to the nearest integer. Two-way ties are broken by rounding to the even integer.

The average is computed without overflow; the result always fits in the same type as the inputs. This is equivalent to average_round with Nearest.

$$ f(x, y) = \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} $$

where $a = \frac{x + y}{2}$.

§Worst-case complexity

Constant time and additional memory.

§Examples

See here.

Source§

type Output = i64

Source§

impl Average for i128

Source§

fn average(self, other: i128) -> i128

Computes the average (arithmetic mean) of two numbers, rounding to the nearest integer. Two-way ties are broken by rounding to the even integer.

The average is computed without overflow; the result always fits in the same type as the inputs. This is equivalent to average_round with Nearest.

$$ f(x, y) = \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} $$

where $a = \frac{x + y}{2}$.

§Worst-case complexity

Constant time and additional memory.

§Examples

See here.

Source§

type Output = i128

Source§

impl Average for isize

Source§

fn average(self, other: isize) -> isize

Computes the average (arithmetic mean) of two numbers, rounding to the nearest integer. Two-way ties are broken by rounding to the even integer.

The average is computed without overflow; the result always fits in the same type as the inputs. This is equivalent to average_round with Nearest.

$$ f(x, y) = \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} $$

where $a = \frac{x + y}{2}$.

§Worst-case complexity

Constant time and additional memory.

§Examples

See here.

Source§

type Output = isize

Source§

impl Average for u8

Source§

fn average(self, other: u8) -> u8

Computes the average (arithmetic mean) of two numbers, rounding to the nearest integer. Two-way ties are broken by rounding to the even integer.

The average is computed without overflow; the result always fits in the same type as the inputs. This is equivalent to average_round with Nearest.

$$ f(x, y) = \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} $$

where $a = \frac{x + y}{2}$.

§Worst-case complexity

Constant time and additional memory.

§Examples

See here.

Source§

type Output = u8

Source§

impl Average for u16

Source§

fn average(self, other: u16) -> u16

Computes the average (arithmetic mean) of two numbers, rounding to the nearest integer. Two-way ties are broken by rounding to the even integer.

The average is computed without overflow; the result always fits in the same type as the inputs. This is equivalent to average_round with Nearest.

$$ f(x, y) = \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} $$

where $a = \frac{x + y}{2}$.

§Worst-case complexity

Constant time and additional memory.

§Examples

See here.

Source§

type Output = u16

Source§

impl Average for u32

Source§

fn average(self, other: u32) -> u32

Computes the average (arithmetic mean) of two numbers, rounding to the nearest integer. Two-way ties are broken by rounding to the even integer.

The average is computed without overflow; the result always fits in the same type as the inputs. This is equivalent to average_round with Nearest.

$$ f(x, y) = \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} $$

where $a = \frac{x + y}{2}$.

§Worst-case complexity

Constant time and additional memory.

§Examples

See here.

Source§

type Output = u32

Source§

impl Average for u64

Source§

fn average(self, other: u64) -> u64

Computes the average (arithmetic mean) of two numbers, rounding to the nearest integer. Two-way ties are broken by rounding to the even integer.

The average is computed without overflow; the result always fits in the same type as the inputs. This is equivalent to average_round with Nearest.

$$ f(x, y) = \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} $$

where $a = \frac{x + y}{2}$.

§Worst-case complexity

Constant time and additional memory.

§Examples

See here.

Source§

type Output = u64

Source§

impl Average for u128

Source§

fn average(self, other: u128) -> u128

Computes the average (arithmetic mean) of two numbers, rounding to the nearest integer. Two-way ties are broken by rounding to the even integer.

The average is computed without overflow; the result always fits in the same type as the inputs. This is equivalent to average_round with Nearest.

$$ f(x, y) = \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} $$

where $a = \frac{x + y}{2}$.

§Worst-case complexity

Constant time and additional memory.

§Examples

See here.

Source§

type Output = u128

Source§

impl Average for usize

Source§

fn average(self, other: usize) -> usize

Computes the average (arithmetic mean) of two numbers, rounding to the nearest integer. Two-way ties are broken by rounding to the even integer.

The average is computed without overflow; the result always fits in the same type as the inputs. This is equivalent to average_round with Nearest.

$$ f(x, y) = \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} $$

where $a = \frac{x + y}{2}$.

§Worst-case complexity

Constant time and additional memory.

§Examples

See here.

Source§

type Output = usize

Implementors§