pub mod precision;
mod registry;
pub use precision::*;
pub use registry::{all_tolerances, by_name, tier};
#[derive(Debug, Clone, Copy)]
pub struct Tolerance {
pub name: &'static str,
pub abs_tol: f64,
pub rel_tol: f64,
pub justification: &'static str,
}
#[must_use]
pub fn check(computed: f64, expected: f64, tol: &Tolerance) -> bool {
if !computed.is_finite() || !expected.is_finite() {
return computed == expected;
}
let abs = (computed - expected).abs();
if abs <= tol.abs_tol {
return true;
}
let scale = expected.abs().max(1.0);
abs <= tol.rel_tol * scale
}
pub const LINALG_MATMUL: Tolerance = Tolerance {
name: "linalg_matmul",
abs_tol: 1e-10,
rel_tol: 1e-10,
justification: "f64 dot-product accumulation; √n rounding for inner dim n",
};
pub const LINALG_TRANSPOSE: Tolerance = Tolerance {
name: "linalg_transpose",
abs_tol: 1e-14,
rel_tol: 1e-14,
justification: "exact data movement; only f64 representation",
};
pub const LINALG_FROBENIUS: Tolerance = Tolerance {
name: "linalg_frobenius",
abs_tol: 1e-10,
rel_tol: 1e-10,
justification: "f64 sum-of-squares reduction; accumulation order",
};
pub const REDUCTION_SUM: Tolerance = Tolerance {
name: "reduction_sum",
abs_tol: 1e-12,
rel_tol: 1e-12,
justification: "f64 accumulation; O(n) rounding for n elements",
};
pub const REDUCTION_MEAN: Tolerance = Tolerance {
name: "reduction_mean",
abs_tol: 1e-12,
rel_tol: 1e-12,
justification: "f64 sum then division; machine precision",
};
pub const REDUCTION_VARIANCE: Tolerance = Tolerance {
name: "reduction_variance",
abs_tol: 1e-10,
rel_tol: 1e-10,
justification: "two-pass mean; catastrophic cancellation in subtraction",
};
pub const REDUCTION_LOGSUMEXP: Tolerance = Tolerance {
name: "reduction_logsumexp",
abs_tol: 1e-10,
rel_tol: 1e-10,
justification: "numerically stable; exp/log round-trip",
};
pub const BIO_HMM: Tolerance = Tolerance {
name: "bio_hmm",
abs_tol: 1e-8,
rel_tol: 1e-8,
justification: "forward-backward accumulates rounding from T steps",
};
pub const BIO_ALLELE_FREQ: Tolerance = Tolerance {
name: "bio_allele_freq",
abs_tol: 1e-6,
rel_tol: 1e-6,
justification: "mean/variance over populations; f64 two-pass",
};
pub const BIO_NUCLEOTIDE_DIVERSITY: Tolerance = Tolerance {
name: "bio_nucleotide_diversity",
abs_tol: 1e-8,
rel_tol: 1e-8,
justification: "pairwise counting; exact arithmetic on integers",
};
pub const SPECIAL_ERF: Tolerance = Tolerance {
name: "special_erf",
abs_tol: 1e-10,
rel_tol: 1e-10,
justification: "A&S 7.1.26; ~6 digits accuracy in f64",
};
pub const SPECIAL_GAMMA: Tolerance = Tolerance {
name: "special_gamma",
abs_tol: 1e-10,
rel_tol: 1e-10,
justification: "Lanczos; ~12 digits for x in [0.5, 2]",
};
pub const SPECIAL_BESSEL: Tolerance = Tolerance {
name: "special_bessel",
abs_tol: 1e-6,
rel_tol: 1e-6,
justification: "A&S 9.4.1-9.4.6; ~6 digits in f64",
};
pub const HYDRO_ET0: Tolerance = Tolerance {
name: "hydro_et0",
abs_tol: 0.05,
rel_tol: 1e-3,
justification: "FAO-56 PM: ~0.05 mm/day measurement uncertainty; GPU f64 chain",
};
pub const HYDRO_SOIL_MOISTURE: Tolerance = Tolerance {
name: "hydro_soil_moisture",
abs_tol: 1e-4,
rel_tol: 1e-3,
justification: "Richards/water balance: iterative solver convergence + sensor noise",
};
pub const HYDRO_WATER_BALANCE: Tolerance = Tolerance {
name: "hydro_water_balance",
abs_tol: 0.1,
rel_tol: 1e-3,
justification: "cascaded ET₀→Kc→WB accumulates rounding over seasonal pipeline",
};
pub const HYDRO_CROP_COEFFICIENT: Tolerance = Tolerance {
name: "hydro_crop_coefficient",
abs_tol: 1e-6,
rel_tol: 1e-6,
justification: "linear interpolation between Kc_prev and Kc_next; exact on f64",
};
pub const PHYSICS_ANDERSON_EIGENVALUE: Tolerance = Tolerance {
name: "physics_anderson_eigenvalue",
abs_tol: 1e-10,
rel_tol: 1e-10,
justification: "Sturm bisection converges to machine precision for well-separated eigenvalues",
};
pub const PHYSICS_LATTICE_ACTION: Tolerance = Tolerance {
name: "physics_lattice_action",
abs_tol: 1e-6,
rel_tol: 1e-4,
justification: "plaquette trace accumulation over lattice volume; GPU f64 reduction order",
};
pub const PHYSICS_LYAPUNOV: Tolerance = Tolerance {
name: "physics_lyapunov",
abs_tol: 1e-4,
rel_tol: 1e-3,
justification: "log(norm) accumulation over L steps; statistical averaging over disorder",
};
pub const LATTICE_CG_FORCE: Tolerance = Tolerance {
name: "lattice_cg_force",
abs_tol: 1e-6,
rel_tol: 1e-6,
justification: "CG for force term: moderate precision, O(100) iterations typical",
};
pub const LATTICE_CG_METROPOLIS: Tolerance = Tolerance {
name: "lattice_cg_metropolis",
abs_tol: 1e-8,
rel_tol: 1e-8,
justification: "CG for Metropolis: high precision for detailed balance",
};
pub const LATTICE_RHMC_APPROX_ERROR: Tolerance = Tolerance {
name: "lattice_rhmc_approx_error",
abs_tol: 1e-3,
rel_tol: 1e-3,
justification: "rational approximation max relative error bound (Remez exchange)",
};
pub const LATTICE_PLAQUETTE: Tolerance = Tolerance {
name: "lattice_plaquette",
abs_tol: 1e-6,
rel_tol: 1e-4,
justification: "plaquette trace from GPU reduction; same as physics_lattice_action",
};
pub const LATTICE_FERMION_FORCE: Tolerance = Tolerance {
name: "lattice_fermion_force",
abs_tol: 1e-4,
rel_tol: 1e-3,
justification: "fermion force depends on CG solution accuracy and gauge fluctuations",
};
pub const LATTICE_METROPOLIS_DELTA_H: Tolerance = Tolerance {
name: "lattice_metropolis_delta_h",
abs_tol: 1.0,
rel_tol: 0.5,
justification: "ΔH of O(1) expected for well-tuned HMC; acceptance rate ~70-90%",
};
pub const BIO_DIVERSITY_SHANNON: Tolerance = Tolerance {
name: "bio_diversity_shannon",
abs_tol: 1e-8,
rel_tol: 1e-8,
justification: "log accumulation over S species; well-conditioned for p_i > 0",
};
pub const BIO_DIVERSITY_SIMPSON: Tolerance = Tolerance {
name: "bio_diversity_simpson",
abs_tol: 1e-10,
rel_tol: 1e-10,
justification: "sum-of-squares; exact integer arithmetic when from counts",
};
pub const BIO_PHYLOGENETIC: Tolerance = Tolerance {
name: "bio_phylogenetic",
abs_tol: 1e-6,
rel_tol: 1e-4,
justification: "branch-length accumulation over tree; float rounding per edge",
};
pub const DETERMINISM: Tolerance = Tolerance {
name: "tol::DETERMINISM",
abs_tol: 0.0,
rel_tol: f64::EPSILON,
justification: "pure data movement or integer arithmetic; no floating-point accumulation",
};
pub const MACHINE: Tolerance = Tolerance {
name: "tol::MACHINE",
abs_tol: 1e-15,
rel_tol: 1e-15,
justification: "one f64 multiply/add; unit roundoff ~1.1e-16",
};
pub const ACCUMULATION: Tolerance = Tolerance {
name: "tol::ACCUMULATION",
abs_tol: 1e-12,
rel_tol: 1e-12,
justification: "sum/dot-product of ~10^6 terms; error ~sqrt(n)·eps",
};
pub const TRANSCENDENTAL: Tolerance = Tolerance {
name: "tol::TRANSCENDENTAL",
abs_tol: 1e-10,
rel_tol: 1e-10,
justification: "range-reduced minimax with ~12 correct digits",
};
pub const ITERATIVE: Tolerance = Tolerance {
name: "tol::ITERATIVE",
abs_tol: 1e-8,
rel_tol: 1e-8,
justification: "iterative solver residual; problem-dependent conditioning",
};
pub const STATISTICAL: Tolerance = Tolerance {
name: "tol::STATISTICAL",
abs_tol: 1e-6,
rel_tol: 1e-4,
justification: "finite-sample statistics; dominated by sampling error not arithmetic",
};
pub const STOCHASTIC: Tolerance = Tolerance {
name: "tol::STOCHASTIC",
abs_tol: 1e-3,
rel_tol: 1e-2,
justification: "Monte Carlo; error ~1/sqrt(N_samples); PRNG seed sensitive",
};
pub const EQUILIBRIUM: Tolerance = Tolerance {
name: "tol::EQUILIBRIUM",
abs_tol: 1.0,
rel_tol: 1e-2,
justification: "thermodynamic equilibrium; fluctuations dominate",
};
pub mod eps {
pub const SAFE_DIV: f64 = 1e-15;
pub const SSA_FLOOR: f64 = 1e-30;
pub const UNDERFLOW: f64 = -700.0;
pub const OVERFLOW: f64 = 700.0;
pub const LOG_FLOOR: f64 = 1e-300;
pub const SQRT_FLOOR: f64 = 0.0;
pub const DENSITY_FLOOR: f64 = 1e-20;
pub const PROB_FLOOR: f64 = 1e-15;
#[must_use]
pub fn midpoint(a: f64, b: f64) -> f64 {
(b - a).mul_add(0.5, a)
}
}
pub const PHARMA_FOCE: Tolerance = Tolerance {
name: "pharma_foce",
abs_tol: 1e-6,
rel_tol: 1e-4,
justification: "FOCE likelihood: per-subject gradient accumulation; Newton step convergence",
};
pub const PHARMA_VPC: Tolerance = Tolerance {
name: "pharma_vpc",
abs_tol: 1e-4,
rel_tol: 1e-3,
justification: "VPC Monte Carlo: 1000+ sims; dominated by sampling variance",
};
pub const PHARMA_NCA: Tolerance = Tolerance {
name: "pharma_nca",
abs_tol: 1e-8,
rel_tol: 1e-6,
justification: "trapezoidal AUC: exact for linear interpolation; f64 accumulation",
};
pub const PHARMA_POP_PK: Tolerance = Tolerance {
name: "pharma_pop_pk",
abs_tol: 1e-10,
rel_tol: 1e-10,
justification: "F*Dose/CL: three f64 multiplies/divides; deterministic PRNG parity",
};
pub const PHARMA_HILL: Tolerance = Tolerance {
name: "pharma_hill",
abs_tol: 1e-6,
rel_tol: 1e-5,
justification: "f32 log/exp cast: ~7 sig digits; typical Hill n in [0.5, 4]",
};
pub const PHARMA_MICHAELIS_MENTEN: Tolerance = Tolerance {
name: "pharma_michaelis_menten",
abs_tol: 1e-6,
rel_tol: 1e-4,
justification: "Euler integration O(dt); per-patient stochastic Vmax; f64 accumulation",
};
pub const PHARMA_SCFA: Tolerance = Tolerance {
name: "pharma_scfa",
abs_tol: 1e-12,
rel_tol: 1e-12,
justification: "Vmax*S/(Km+S): two f64 multiplies, one divide; no accumulation",
};
pub const SIGNAL_FFT: Tolerance = Tolerance {
name: "signal_fft",
abs_tol: 1e-10,
rel_tol: 1e-10,
justification: "radix-2 FFT: O(n log n) butterfly; twiddle factor precision",
};
pub const SIGNAL_QRS: Tolerance = Tolerance {
name: "signal_qrs",
abs_tol: 1e-3,
rel_tol: 1e-2,
justification: "streaming filter bank; adaptive threshold; dominated by signal noise",
};
#[cfg(test)]
#[path = "tests.rs"]
mod tests;