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);

Object Safety§

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>,