2021-08-24 21:33:48 +02:00
|
|
|
use std::iter::Iterator;
|
|
|
|
|
|
2018-01-14 21:43:29 +01:00
|
|
|
/// A max function for f64's without NaNs
|
|
|
|
|
pub fn max(vals: &[f64]) -> f64 {
|
2018-05-22 22:57:51 +02:00
|
|
|
*vals
|
|
|
|
|
.iter()
|
2018-01-14 21:43:29 +01:00
|
|
|
.max_by(|a, b| a.partial_cmp(b).unwrap())
|
|
|
|
|
.unwrap()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A min function for f64's without NaNs
|
|
|
|
|
pub fn min(vals: &[f64]) -> f64 {
|
2018-05-22 22:57:51 +02:00
|
|
|
*vals
|
|
|
|
|
.iter()
|
2018-01-14 21:43:29 +01:00
|
|
|
.min_by(|a, b| a.partial_cmp(b).unwrap())
|
|
|
|
|
.unwrap()
|
|
|
|
|
}
|
2018-01-18 18:58:51 +01:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_max() {
|
2021-08-24 21:50:31 +02:00
|
|
|
let assert_float_eq = |a: f64, b: f64| {
|
|
|
|
|
assert!((a - b).abs() < f64::EPSILON);
|
2021-07-27 09:10:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
assert_float_eq(1.0, max(&[1.0]));
|
|
|
|
|
assert_float_eq(-1.0, max(&[-1.0]));
|
|
|
|
|
assert_float_eq(-1.0, max(&[-2.0, -1.0]));
|
|
|
|
|
assert_float_eq(1.0, max(&[-1.0, 1.0]));
|
|
|
|
|
assert_float_eq(1.0, max(&[-1.0, 1.0, 0.0]));
|
2018-01-18 18:58:51 +01:00
|
|
|
}
|