1

I am trying to write tests for various algorithms in my crate. However, I have come across a bit of a hurdle regarding how f64 values behave. They both return the same value, but I'm not too sure of the precision of the calculated series, because the assertion fails.

fn floating_point() {
    let s1 = Series::new("a", vec![1.0, 5.0, 7.7]);
    let s2 = Series::new("a", vec![4.566667]);

    let avg = s1.mean_as_series();
    println!("{}", avg);
    println!("{}", s2);
    assert!(s2.series_equal(&avg))
}
shape: (1,)
Series: 'a' [f64]
[
        4.566667
]
shape: (1,)
Series: 'a' [f64]
[
        4.566667
]

What would be the recommended way to determine if 2 series like this are equivalent? I have looked into rounding the series, but I am looking for alternatives to that. The behaviour seems to be inconsistent with 5 decimal points on different series. some fail, some pass.

Also, what would be the way to do this with a Dataframe without extracting the series?

Progman
  • 16,827
  • 6
  • 33
  • 48
Kival M
  • 182
  • 1
  • 10
  • The best way to test float equality is to check that the (magnitude of the) difference between the two floats is sufficiently close to 0 – BallpointBen Jun 29 '22 at 18:14
  • https://stackoverflow.com/questions/4915462/how-should-i-do-floating-point-comparison – KamilCuk Jun 29 '22 at 19:31

0 Answers0