Skip to main content

engineering/
engineering.rs

1//! Exact structural-engineering fractions — no float drift in combination checks.
2//!
3//! Run with: `cargo run --example engineering`
4
5use adele_ring::{Channels, RnsRational};
6
7fn main() {
8    let ch = Channels::standard(32);
9    let f = |p: i64, q: i64| RnsRational::from_fraction(p, q, ch.clone());
10
11    println!("== adele-ring :: exact engineering arithmetic ==\n");
12
13    // Stacked plate thicknesses in inches: 3/8 + 1/4 + 5/16.
14    let stack = f(3, 8).add(&f(1, 4)).add(&f(5, 16));
15    println!("3/8 + 1/4 + 5/16 in   = {} in   (= {:.6} in)", stack, stack.to_f64());
16
17    // Safety factor 1/1.5 = 2/3 exactly.
18    let safety = f(1, 1).div(&f(3, 2));
19    println!("1 / 1.5               = {}        (= {:.6})", safety, safety.to_f64());
20
21    // Load ratio: 47 kips / 70 kips — stays exact through combination checks.
22    let load_ratio = f(47, 70);
23    let with_margin = load_ratio.add(&f(1, 10)); // add a 0.1 utilization margin
24    println!(
25        "47/70 + 1/10          = {}    (= {:.6})",
26        with_margin,
27        with_margin.to_f64()
28    );
29
30    // AISC-style web area: t_w * d  =  (5/16) * (24/1) in^2.
31    let t_w = f(5, 16);
32    let d = f(24, 1);
33    let web_area = t_w.mul(&d);
34    println!("(5/16) * 24           = {} in^2   (= {:.6} in^2)", web_area, web_area.to_f64());
35
36    println!("\n-- base awareness --");
37    for (p, q) in [(1, 6), (1, 8), (47, 70)] {
38        let r = f(p, q);
39        println!(
40            "{p}/{q}: natural base = {}, terminates in base 10 = {}, period in base 10 = {}",
41            r.natural_base(),
42            r.exact_in_base(10),
43            r.termination_period_in_base(10)
44        );
45    }
46
47    // Demonstrate the classic float failure that adele-ring avoids.
48    let exact = f(1, 10).add(&f(1, 5)); // 0.1 + 0.2
49    let naive = 0.1_f64 + 0.2_f64;
50    println!("\n0.1 + 0.2 exact       = {}   (f64 gives {:.17})", exact, naive);
51    assert_eq!(exact, f(3, 10));
52}