float_comparison/
float_comparison.rs1use adele_ring::{AlgebraicNumber, Channels, RnsRational, SymbolicExpr, TowerValue};
6
7fn 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 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 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 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 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 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}