statrs/
prec.rs

1//! Provides utility functions for working with floating point precision
2
3use approx::AbsDiffEq;
4
5/// Standard epsilon, maximum relative precision of IEEE 754 double-precision
6/// floating point numbers (64 bit) e.g. `2^-53`
7pub const F64_PREC: f64 = 0.00000000000000011102230246251565;
8
9/// Default accuracy for `f64`, equivalent to `0.0 * F64_PREC`
10pub const DEFAULT_F64_ACC: f64 = 0.0000000000000011102230246251565;
11
12/// Compares if two floats are close via `approx::abs_diff_eq`
13/// using a maximum absolute difference (epsilon) of `acc`.
14pub fn almost_eq(a: f64, b: f64, acc: f64) -> bool {
15    if a.is_infinite() && b.is_infinite() {
16        return a == b;
17    }
18    a.abs_diff_eq(&b, acc)
19}
20
21/// Compares if two floats are close via `approx::relative_eq!`
22/// and `crate::consts::ACC` relative precision.
23/// Updates first argument to value of second argument
24pub fn convergence(x: &mut f64, x_new: f64) -> bool {
25    let res = approx::relative_eq!(*x, x_new, max_relative = crate::consts::ACC);
26    *x = x_new;
27    res
28}