Skip to content

Commit

Permalink
Make prec::almost_eq a wrapper for abs_diff_eq
Browse files Browse the repository at this point in the history
  • Loading branch information
FreezyLemon committed Jun 5, 2024
1 parent 3e7970e commit 046586c
Showing 1 changed file with 5 additions and 16 deletions.
21 changes: 5 additions & 16 deletions src/prec.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
//! Provides utility functions for working with floating point precision
use approx::AbsDiffEq;

/// Standard epsilon, maximum relative precision of IEEE 754 double-precision
/// floating point numbers (64 bit) e.g. `2^-53`
pub const F64_PREC: f64 = 0.00000000000000011102230246251565;

/// Default accuracy for `f64`, equivalent to `0.0 * F64_PREC`
pub const DEFAULT_F64_ACC: f64 = 0.0000000000000011102230246251565;

/// Returns true if `a` and `b `are within `acc` of each other.
/// If `a` or `b` are infinite, returns `true` only if both are
/// infinite and similarly signed. Always returns `false` if
/// either number is a `NAN`.
/// Compares if two floats are close via `approx::abs_diff_eq`
/// using a maximum absolute difference (epsilon) of `acc`.
pub fn almost_eq(a: f64, b: f64, acc: f64) -> bool {
// only true if a and b are infinite with same
// sign
if a.is_infinite() || b.is_infinite() {
return a == b;
}

// NANs are never equal
if a.is_nan() && b.is_nan() {
return false;
}

(a - b).abs() < acc
a.abs_diff_eq(&b, acc)
}

/// Compares if two floats are close via `approx::relative_eq!`
Expand Down

0 comments on commit 046586c

Please sign in to comment.