Skip to main content

float_comparison/
float_comparison.rs

1//! Side-by-side comparison of exact `adele-ring` results versus naive `f64`.
2//!
3//! Run with: `cargo run --example float_comparison`
4
5use adele_ring::{AlgebraicNumber, Channels, RnsRational, SymbolicExpr, TowerValue};
6
7/// Distance in ULPs between two finite f64 values of the same sign.
8fn ulp_distance(a: f64, b: f64) -> i64 {
9    let ai = a.to_bits() as i64;
10    let bi = b.to_bits() as i64;
11    (ai - bi).abs()
12}
13
14fn row(label: &str, exact: &str, f64_val: f64, exact_val: f64) {
15    let abs_err = (f64_val - exact_val).abs();
16    let ulps = ulp_distance(f64_val, exact_val);
17    println!(
18        "{label:<16} | {exact:<10} | {f64_val:<22.17} | {abs_err:<12.3e} | {ulps}",
19    );
20}
21
22fn main() {
23    let ch = Channels::standard(32);
24    println!("== adele-ring :: exact vs f64 ==\n");
25    println!(
26        "{:<16} | {:<10} | {:<22} | {:<12} | ULPs",
27        "expression", "exact", "f64 result", "abs error"
28    );
29    println!("{}", "-".repeat(78));
30
31    // 0.1 + 0.2 = 3/10
32    let exact = RnsRational::from_fraction(1, 10, ch.clone())
33        .add(&RnsRational::from_fraction(1, 5, ch.clone()));
34    row("0.1 + 0.2", &exact.display(), 0.1 + 0.2, exact.to_f64());
35
36    // 1/3 * 3 = 1
37    let one = RnsRational::from_fraction(1, 3, ch.clone()).mul(&RnsRational::from_int(3, ch.clone()));
38    row("1/3 * 3", &one.display(), (1.0 / 3.0) * 3.0, one.to_f64());
39
40    // sqrt(2) * sqrt(2) = 2  (drops to Integer through the tower)
41    let s2 = TowerValue::Algebraic(AlgebraicNumber::sqrt(2, ch.clone()));
42    let two = s2.mul(&s2);
43    let naive = 2f64.sqrt() * 2f64.sqrt();
44    row("sqrt2 * sqrt2", "2", naive, two.to_f64().unwrap());
45
46    // sin(pi) = 0  (exact identity; f64 gives ~1.2e-16)
47    let sin_pi = TowerValue::Symbolic(SymbolicExpr::Pi).sin();
48    row("sin(pi)", "0", std::f64::consts::PI.sin(), sin_pi.to_f64().unwrap());
49
50    // 1/7 * 7 = 1
51    let one7 = RnsRational::from_fraction(1, 7, ch.clone()).mul(&RnsRational::from_int(7, ch));
52    row("1/7 * 7", &one7.display(), (1.0 / 7.0) * 7.0, one7.to_f64());
53
54    println!("\nEvery `exact` column is bit-for-bit correct; the f64 column drifts.");
55}