num_integer

Trait Average

Source
pub trait Average: Integer {
    // Required methods
    fn average_ceil(&self, other: &Self) -> Self;
    fn average_floor(&self, other: &Self) -> Self;
}
Expand description

Provides methods to compute the average of two integers, without overflows.

Required Methods§

Source

fn average_ceil(&self, other: &Self) -> Self

Returns the ceiling value of the average of self and other. – ⌈(self + other)/2⌉

§Examples
use num_integer::Average;

assert_eq!(( 3).average_ceil(&10),  7);
assert_eq!((-2).average_ceil(&-5), -3);
assert_eq!(( 4).average_ceil(& 4),  4);

assert_eq!(u8::max_value().average_ceil(&2), 129);
assert_eq!(i8::min_value().average_ceil(&-1), -64);
assert_eq!(i8::min_value().average_ceil(&i8::max_value()), 0);
Source

fn average_floor(&self, other: &Self) -> Self

Returns the floor value of the average of self and other. – ⌊(self + other)/2⌋

§Examples
use num_integer::Average;

assert_eq!(( 3).average_floor(&10),  6);
assert_eq!((-2).average_floor(&-5), -4);
assert_eq!(( 4).average_floor(& 4),  4);

assert_eq!(u8::max_value().average_floor(&2), 128);
assert_eq!(i8::min_value().average_floor(&-1), -65);
assert_eq!(i8::min_value().average_floor(&i8::max_value()), -1);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<I> Average for I
where I: Integer + Shr<usize, Output = I>, for<'a, 'b> &'a I: BitAnd<&'b I, Output = I> + BitOr<&'b I, Output = I> + BitXor<&'b I, Output = I>,