statrs/prec.rs
1//! Provides utility functions for working with floating point precision
2
3/// Standard epsilon, maximum relative precision of IEEE 754 double-precision
4/// floating point numbers (64 bit) e.g. `2^-53`
5pub const F64_PREC: f64 = 0.00000000000000011102230246251565;
6
7/// Default accuracy for `f64`, equivalent to `0.0 * F64_PREC`
8pub const DEFAULT_F64_ACC: f64 = 0.0000000000000011102230246251565;
9
10/// Returns true if `a` and `b `are within `acc` of each other.
11/// If `a` or `b` are infinite, returns `true` only if both are
12/// infinite and similarly signed. Always returns `false` if
13/// either number is a `NAN`.
14pub fn almost_eq(a: f64, b: f64, acc: f64) -> bool {
15 // only true if a and b are infinite with same
16 // sign
17 if a.is_infinite() || b.is_infinite() {
18 return a == b;
19 }
20
21 // NANs are never equal
22 if a.is_nan() && b.is_nan() {
23 return false;
24 }
25
26 (a - b).abs() < acc
27}