/ thirdparty / hyperfine / src / util / min_max.rs
min_max.rs
 1  /// A max function for f64's without NaNs
 2  pub fn max(vals: &[f64]) -> f64 {
 3      *vals
 4          .iter()
 5          .max_by(|a, b| a.partial_cmp(b).unwrap())
 6          .unwrap()
 7  }
 8  
 9  /// A min function for f64's without NaNs
10  pub fn min(vals: &[f64]) -> f64 {
11      *vals
12          .iter()
13          .min_by(|a, b| a.partial_cmp(b).unwrap())
14          .unwrap()
15  }
16  
17  #[test]
18  fn test_max() {
19      let assert_float_eq = |a: f64, b: f64| {
20          assert!((a - b).abs() < f64::EPSILON);
21      };
22  
23      assert_float_eq(1.0, max(&[1.0]));
24      assert_float_eq(-1.0, max(&[-1.0]));
25      assert_float_eq(-1.0, max(&[-2.0, -1.0]));
26      assert_float_eq(1.0, max(&[-1.0, 1.0]));
27      assert_float_eq(1.0, max(&[-1.0, 1.0, 0.0]));
28  }