#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Tolerance {
Exact,
Ulp1,
Ulp4,
Digits15,
Digits14,
Digits12,
Digits10,
Digits8,
Digits6,
Digits4,
Digits3,
Digits2,
Qualitative,
}
impl Tolerance {
#[must_use]
pub const fn abs_tol(&self) -> f64 {
match self {
Self::Exact => 0.0,
Self::Ulp1 => f64::EPSILON,
Self::Ulp4 => 4.0 * f64::EPSILON,
Self::Digits15 => 1e-15,
Self::Digits14 => 1e-14,
Self::Digits12 => 1e-12,
Self::Digits10 => 1e-10,
Self::Digits8 => 1e-8,
Self::Digits6 => 1e-6,
Self::Digits4 => 1e-4,
Self::Digits3 => 1e-3,
Self::Digits2 => 1e-2,
Self::Qualitative => 0.1,
}
}
#[must_use]
pub fn approx_eq(self, a: f64, b: f64) -> bool {
if self == Self::Exact {
return a == b;
}
(a - b).abs() <= self.abs_tol()
}
#[must_use]
pub fn approx_eq_rel(self, a: f64, b: f64) -> bool {
if self == Self::Exact {
return a == b;
}
let scale = a.abs().max(b.abs()).max(1.0);
(a - b).abs() <= self.abs_tol() * scale
}
pub const CPU_F64: Self = Self::Digits15;
pub const GPU_F64: Self = Self::Digits12;
pub const DF64: Self = Self::Digits10;
pub const GPU_TRANSCENDENTAL: Self = Self::Digits8;
pub const F32: Self = Self::Digits6;
#[must_use]
pub const fn loosen(&self) -> Self {
match self {
Self::Exact => Self::Ulp1,
Self::Ulp1 => Self::Ulp4,
Self::Ulp4 => Self::Digits15,
Self::Digits15 => Self::Digits14,
Self::Digits14 => Self::Digits12,
Self::Digits12 => Self::Digits10,
Self::Digits10 => Self::Digits8,
Self::Digits8 => Self::Digits6,
Self::Digits6 => Self::Digits4,
Self::Digits4 => Self::Digits3,
Self::Digits3 => Self::Digits2,
Self::Digits2 => Self::Qualitative,
Self::Qualitative => Self::Qualitative,
}
}
pub const COUNT: usize = 13;
}
impl std::fmt::Display for Tolerance {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Exact => write!(f, "exact (0 ULP)"),
Self::Ulp1 => write!(f, "1 ULP ({:.2e})", f64::EPSILON),
Self::Ulp4 => write!(f, "4 ULP ({:.2e})", 4.0 * f64::EPSILON),
_ => write!(f, "{:.0e}", self.abs_tol()),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn tier_ordering() {
assert!(Tolerance::Exact < Tolerance::Ulp1);
assert!(Tolerance::Ulp1 < Tolerance::Digits15);
assert!(Tolerance::Digits15 < Tolerance::Qualitative);
}
#[test]
fn exact_requires_bitwise() {
assert!(Tolerance::Exact.approx_eq(1.0, 1.0));
assert!(!Tolerance::Exact.approx_eq(1.0, 1.0 + f64::EPSILON));
}
#[test]
fn ulp1_accepts_epsilon() {
let a = 1.0_f64;
let b = a + f64::EPSILON;
assert!(Tolerance::Ulp1.approx_eq(a, b));
}
#[test]
fn gpu_f64_tier() {
let cpu = std::f64::consts::PI;
#[expect(
clippy::approx_constant,
reason = "intentionally truncated pi to simulate GPU f64 precision loss"
)]
let gpu = 3.141_592_653_590_f64;
assert!(Tolerance::GPU_F64.approx_eq(cpu, gpu));
}
#[test]
fn f32_tier_accepts_6_digits() {
let a = 1.234_567_f64;
let b = 1.234_567_5_f64;
assert!(Tolerance::F32.approx_eq(a, b));
}
#[test]
fn loosen_chain_reaches_qualitative() {
let mut t = Tolerance::Exact;
for _ in 0..20 {
t = t.loosen();
}
assert_eq!(t, Tolerance::Qualitative);
}
#[test]
fn relative_tolerance() {
let a = 1e10_f64;
let b = a + 1.0;
assert!(!Tolerance::Digits12.approx_eq(a, b));
assert!(Tolerance::Digits10.approx_eq_rel(a, b));
}
#[test]
fn display_exact() {
let exact = Tolerance::Exact;
assert_eq!(format!("{exact}"), "exact (0 ULP)");
}
#[test]
fn count_is_correct() {
assert_eq!(Tolerance::COUNT, 13);
}
}