LibrEDA project web page. https://libreda.codeberg.page
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

#### 35 lines 12 KiB Raw Permalink Blame History

 `Average in num_integer - Rust` `` `

Trait num_integer::Average[][src]

pub trait Average: Integer {`
`    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

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

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

Implementors

Returns the floor value of the average of self and other.

` `

Returns the ceil value of the average of self and other.

` `
`