Skip to content

Commit 7fa8336

Browse files
FreezyLemonYeungOnion
authored andcommitted
Make prec::almost_eq a wrapper for abs_diff_eq
1 parent 3e7970e commit 7fa8336

File tree

1 file changed

+5
-16
lines changed

1 file changed

+5
-16
lines changed

src/prec.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,18 @@
11
//! Provides utility functions for working with floating point precision
22
3+
use approx::AbsDiffEq;
4+
35
/// Standard epsilon, maximum relative precision of IEEE 754 double-precision
46
/// floating point numbers (64 bit) e.g. `2^-53`
57
pub const F64_PREC: f64 = 0.00000000000000011102230246251565;
68

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

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`.
12+
/// Compares if two floats are close via `approx::abs_diff_eq`
13+
/// using a maximum absolute difference (epsilon) of `acc`.
1414
pub 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
15+
a.abs_diff_eq(&b, acc)
2716
}
2817

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

0 commit comments

Comments
 (0)