computable 0.1.0

Computable real numbers with guaranteed correctness via interval refinement
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
//! Accuracy and quality analysis for Computable arithmetic.
//!
//! This compares Computable results against f64 to demonstrate:
//! - Bound tightness (width) at various precisions
//! - Accuracy of computable vs float results
//! - Protection against catastrophic cancellation
//! - Correctness of transcendental functions (sin, pi)
//!
//! Run with: cargo run --example analysis --release

// Share bench utilities (balanced_sum, etc.) without putting them in the library.
// We only use #[path] here because this is support code for benchmarks and examples,
// not first-class library code, and we want to keep it out of the public API.
#[path = "../benches/common.rs"]
mod common;

use std::num::NonZeroU32;
use std::time::Instant;

use computable::{
    Binary, Bounds, Computable, FiniteBounds, XBinary, XUsize, pi, pi_bounds_at_precision,
};
use num_bigint::BigInt;
use num_traits::{One, Signed, Zero};
use rand::rngs::StdRng;
use rand::{Rng, SeedableRng};

use common::balanced_sum;

// ---------------------------------------------------------------------------
// Helpers (example-specific; balanced_sum comes from benches/common.rs)
// ---------------------------------------------------------------------------

fn try_finite_bounds(bounds: &Bounds) -> Option<FiniteBounds> {
    match (bounds.small(), bounds.large()) {
        (XBinary::Finite(lower), XBinary::Finite(upper)) => {
            Some(FiniteBounds::new(lower.clone(), upper))
        }
        _ => None,
    }
}

fn finite_binary(value: &XBinary) -> Binary {
    match value {
        XBinary::Finite(binary) => binary.clone(),
        XBinary::NegInf | XBinary::PosInf => panic!("expected finite bounds"),
    }
}

// ---------------------------------------------------------------------------
// Analyses
// ---------------------------------------------------------------------------

fn complex_analysis(rng: &mut StdRng) {
    const SAMPLE_COUNT: usize = 5_000;

    let inputs: Vec<(f64, f64, f64, f64)> = (0..SAMPLE_COUNT)
        .map(|_| {
            (
                rng.gen_range(-10.0..10.0),
                rng.gen_range(-10.0..10.0),
                rng.gen_range(-10.0..10.0),
                rng.gen_range(-10.0..10.0),
            )
        })
        .collect();

    let float_start = Instant::now();
    let mut float_total = 0.0;
    for (a, b, c, d) in &inputs {
        let mixed = (a + b) * (c - d);
        let squared = c * c + d * d;
        float_total += a * b + squared + mixed;
    }
    let float_duration = float_start.elapsed();

    let comp_start = Instant::now();
    let terms: Vec<Computable> = inputs
        .iter()
        .map(|&(a, bv, cv, d)| {
            let a_c = Computable::constant(Binary::from_f64(a).unwrap());
            let b_c = Computable::constant(Binary::from_f64(bv).unwrap());
            let c_c = Computable::constant(Binary::from_f64(cv).unwrap());
            let d_c = Computable::constant(Binary::from_f64(d).unwrap());
            let mixed = (a_c.clone() + b_c.clone()) * (c_c.clone() - d_c.clone());
            let squared = c_c.clone() * c_c + d_c.clone() * d_c;
            a_c * b_c + squared + mixed
        })
        .collect();
    let total = balanced_sum(terms);
    let bounds = total.bounds().expect("bounds should succeed");
    let finite = try_finite_bounds(&bounds).expect("bounds should be finite");
    let comp_duration = comp_start.elapsed();

    let midpoint = finite.midpoint();
    let float_binary = Binary::from_f64(float_total).unwrap();
    let error = float_binary.sub(&midpoint).magnitude();

    println!("== Complex Expression Analysis ==");
    println!("samples: {SAMPLE_COUNT}");
    println!("float time:      {float_duration:?}");
    println!("computable time: {comp_duration:?}");
    println!(
        "slowdown factor: {:.1}x",
        comp_duration.as_secs_f64() / float_duration.as_secs_f64()
    );
    println!(
        "float value:         {}",
        Binary::from_f64(float_total).unwrap()
    );
    println!("computable midpoint: {midpoint}");
    println!("computable width:    {}", bounds.width());
    println!("abs(float - midpoint): {error}");
}

fn summation_analysis(rng: &mut StdRng) {
    const SAMPLE_COUNT: usize = 200_000;

    let inputs: Vec<f64> = (0..SAMPLE_COUNT)
        .map(|_| rng.gen_range(-1.0e-6..1.0e-6))
        .collect();
    let computable_inputs: Vec<Computable> = inputs
        .iter()
        .map(|&v| Computable::constant(Binary::from_f64(v).unwrap()))
        .collect();
    let base = 2_i64.pow(30) as f64;

    // Float: sum with base
    let float_start = Instant::now();
    let mut float_total = base;
    for &v in &inputs {
        float_total += v;
    }
    let float_duration = float_start.elapsed();

    // Computable: sum with base
    let comp_start = Instant::now();
    let mut terms = Vec::with_capacity(computable_inputs.len() + 1);
    terms.push(Computable::constant(Binary::from_f64(base).unwrap()));
    terms.extend(computable_inputs.iter().cloned());
    let total = balanced_sum(terms);
    let bounds = total.bounds().expect("bounds should succeed");
    let finite = try_finite_bounds(&bounds).expect("bounds should be finite");
    let comp_duration = comp_start.elapsed();

    let midpoint = finite.midpoint();

    // True sum without base (computable, no cancellation)
    let baseless_sum_computable = {
        let sum = balanced_sum(computable_inputs.clone());
        let b = sum.bounds().expect("bounds should succeed");
        let f = try_finite_bounds(&b).expect("bounds should be finite");
        f.midpoint()
    };
    let baseless_sum_float: f64 = inputs.iter().sum();

    // Precision loss: (sum_with_base - base) vs true baseless sum
    let float_minus_base = float_total - base;
    let comp_minus_base = midpoint.sub(&Binary::from_f64(base).unwrap());

    let float_precision_loss = Binary::from_f64(float_minus_base)
        .unwrap()
        .sub(&baseless_sum_computable)
        .magnitude();
    let comp_precision_loss = comp_minus_base.sub(&baseless_sum_computable).magnitude();

    println!("== Summation (Catastrophic Cancellation) Analysis ==");
    println!("samples: {SAMPLE_COUNT}");
    println!("base value: {}", Binary::from_f64(base).unwrap());
    println!("float time:      {float_duration:?}");
    println!("computable time: {comp_duration:?}");
    println!(
        "slowdown factor: {:.1}x",
        comp_duration.as_secs_f64() / float_duration.as_secs_f64()
    );
    println!(
        "float value:         {}",
        Binary::from_f64(float_total).unwrap()
    );
    println!("computable midpoint: {midpoint}");
    println!("computable width:    {}", bounds.width());
    println!();
    println!("After removing base value:");
    println!(
        "  sum without base (float):      {}",
        Binary::from_f64(baseless_sum_float).unwrap()
    );
    println!("  sum without base (computable): {baseless_sum_computable}");
    println!(
        "  (sum+base)-base  (float):      {}",
        Binary::from_f64(float_minus_base).unwrap()
    );
    println!("  (sum+base)-base  (computable): {comp_minus_base}");
    println!("  float precision loss:      {float_precision_loss}");
    println!("  computable precision loss: {comp_precision_loss}");
}

fn integer_roots_analysis(rng: &mut StdRng) {
    const SAMPLE_COUNT: usize = 1_000;

    let inputs: Vec<(u64, NonZeroU32)> = (0..SAMPLE_COUNT)
        .map(|i| {
            let value = rng.gen_range(2..1000) as u64;
            let n = NonZeroU32::new((i % 5) as u32 + 2).unwrap();
            (value, n)
        })
        .collect();

    let float_start = Instant::now();
    let mut float_total = 0.0f64;
    for &(value, n) in &inputs {
        float_total += (value as f64).powf(1.0 / n.get() as f64);
    }
    let float_duration = float_start.elapsed();

    let epsilon = XUsize::Finite(0);

    let comp_start = Instant::now();
    let terms: Vec<Computable> = inputs
        .iter()
        .map(|&(value, n)| {
            Computable::constant(Binary::new(BigInt::from(value), BigInt::from(0))).nth_root(n)
        })
        .collect();
    let total = balanced_sum(terms);
    let bounds = total
        .refine_to_default(epsilon)
        .expect("refine_to should succeed");
    let finite = try_finite_bounds(&bounds).expect("bounds should be finite");
    let comp_duration = comp_start.elapsed();

    let midpoint = finite.midpoint();
    let error = Binary::from_f64(float_total)
        .unwrap()
        .sub(&midpoint)
        .magnitude();

    println!("== Integer Roots Analysis ==");
    println!("samples: {SAMPLE_COUNT}");
    println!("epsilon: 1");
    println!("root degrees: 2 (sqrt), 3 (cbrt), 4, 5, 6");
    println!("float time:      {float_duration:?}");
    println!("computable time: {comp_duration:?}");
    println!(
        "slowdown factor: {:.1}x",
        comp_duration.as_secs_f64() / float_duration.as_secs_f64()
    );
    println!(
        "float value:         {}",
        Binary::from_f64(float_total).unwrap()
    );
    println!("computable midpoint: {midpoint}");
    println!("computable width:    {}", bounds.width());
    println!("abs(float - midpoint): {error}");
}

fn inv_analysis(rng: &mut StdRng) {
    const SAMPLE_COUNT: usize = 100;
    const PRECISION_BITS: usize = 256;

    let inputs: Vec<f64> = (0..SAMPLE_COUNT)
        .map(|_| rng.gen_range(0.1..100.0))
        .collect();

    let float_start = Instant::now();
    let float_sum: f64 = inputs.iter().map(|x| 1.0 / x).sum();
    let float_duration = float_start.elapsed();

    let epsilon = XUsize::Finite(PRECISION_BITS);

    let comp_start = Instant::now();
    let terms: Vec<Computable> = inputs
        .iter()
        .map(|&x| Computable::constant(Binary::from_f64(x).unwrap()).inv())
        .collect();
    let total = balanced_sum(terms);
    let bounds = total
        .refine_to_default(epsilon)
        .expect("refine_to should succeed");
    let finite = try_finite_bounds(&bounds).expect("bounds should be finite");
    let comp_duration = comp_start.elapsed();

    let midpoint = finite.midpoint();
    let error = Binary::from_f64(float_sum)
        .unwrap()
        .sub(&midpoint)
        .magnitude();

    println!("== Inverse (1/x) Analysis ==");
    println!("samples: {SAMPLE_COUNT}");
    println!("target precision: {PRECISION_BITS} bits");
    println!("float time:      {float_duration:?}");
    println!("computable time: {comp_duration:?}");
    println!(
        "slowdown factor: {:.1}x",
        comp_duration.as_secs_f64() / float_duration.as_secs_f64()
    );
    println!(
        "float value:         {}",
        Binary::from_f64(float_sum).unwrap()
    );
    println!("computable midpoint: {midpoint}");
    println!("computable width:    {}", bounds.width());
    println!("abs(float - midpoint): {error}");
}

fn sin_analysis(rng: &mut StdRng) {
    const SAMPLE_COUNT: usize = 100;
    const PRECISION_BITS: usize = 128;

    let inputs: Vec<f64> = (0..SAMPLE_COUNT)
        .map(|i| {
            if i < SAMPLE_COUNT / 3 {
                rng.gen_range(-1.0..1.0)
            } else if i < 2 * SAMPLE_COUNT / 3 {
                rng.gen_range(-3.15..3.15)
            } else {
                rng.gen_range(-100.0..100.0)
            }
        })
        .collect();

    let float_start = Instant::now();
    let float_sum: f64 = inputs.iter().map(|x| x.sin()).sum();
    let float_duration = float_start.elapsed();

    let epsilon = XUsize::Finite(PRECISION_BITS);

    let comp_start = Instant::now();
    let terms: Vec<Computable> = inputs
        .iter()
        .map(|&x| Computable::constant(Binary::from_f64(x).unwrap()).sin())
        .collect();
    let total = balanced_sum(terms);
    let bounds = total
        .refine_to_default(epsilon)
        .expect("refine_to should succeed");
    let comp_duration = comp_start.elapsed();

    println!("== Sine Analysis ==");
    println!(
        "samples: {} (1/3 small, 1/3 medium, 1/3 large)",
        SAMPLE_COUNT
    );
    println!("target precision: {PRECISION_BITS} bits");
    println!("float time:      {float_duration:?}");
    println!("computable time: {comp_duration:?}");
    println!(
        "slowdown factor: {:.1}x",
        comp_duration.as_secs_f64() / float_duration.as_secs_f64()
    );
    println!(
        "float value:         {}",
        Binary::from_f64(float_sum).unwrap()
    );

    match try_finite_bounds(&bounds) {
        Some(finite) => {
            let midpoint = finite.midpoint();
            let error = Binary::from_f64(float_sum)
                .unwrap()
                .sub(&midpoint)
                .magnitude();
            println!("computable midpoint: {midpoint}");
            println!("computable width:    {}", bounds.width());
            println!("abs(float - midpoint): {error}");
        }
        None => {
            println!("computable bounds: infinite (cannot compute midpoint)");
        }
    }
}

fn pi_analysis() {
    let precision_bits: &[usize] = &[32, 64, 128, 256, 512, 1024];

    println!("== Pi Refinement Analysis ==");
    println!();
    let pi_f64 = Binary::from_f64(std::f64::consts::PI).unwrap();

    for &bits in precision_bits {
        let epsilon = XUsize::Finite(bits);

        let start = Instant::now();
        let bounds = pi()
            .refine_to_default(epsilon)
            .expect("pi refinement should succeed");
        let duration = start.elapsed();

        print!(
            "  {bits:>4} bits: {duration:>12?}  width: {:<30}",
            bounds.width()
        );
        if let Some(finite) = try_finite_bounds(&bounds) {
            let mid = finite.midpoint();
            let error = mid.sub(&pi_f64).magnitude();
            println!("  |mid - pi_f64|: {error}");
        } else {
            println!("  (infinite bounds)");
        }
    }

    // pi_bounds_at_precision
    println!();
    println!("== pi_bounds_at_precision ==");
    println!();
    for &bits in precision_bits {
        let start = Instant::now();
        let (lower, upper) = pi_bounds_at_precision(bits);
        let duration = start.elapsed();
        let width = upper.sub(&lower);
        let sum = lower.add(&upper);
        let mid = Binary::new(sum.mantissa().clone(), sum.exponent() - BigInt::one());
        println!(
            "  {bits:>4} bits: {duration:>12?}  width: {:<30}  midpoint: {mid}",
            width
        );
    }

    // Arithmetic with pi
    let epsilon = XUsize::Finite(64);

    println!();
    println!("== Pi Arithmetic ==");
    println!();

    for (label, expected_f64, build_expr) in [
        (
            "2pi",
            2.0 * std::f64::consts::PI,
            Box::new(|| Computable::constant(Binary::new(BigInt::from(2), BigInt::from(0))) * pi())
                as Box<dyn Fn() -> Computable>,
        ),
        (
            "pi/2",
            std::f64::consts::FRAC_PI_2,
            Box::new(|| {
                Computable::constant(Binary::new(BigInt::from(1), BigInt::from(-1))) * pi()
            }),
        ),
        (
            "pi^2",
            std::f64::consts::PI * std::f64::consts::PI,
            Box::new(|| pi() * pi()),
        ),
        ("1/pi", 1.0 / std::f64::consts::PI, Box::new(|| pi().inv())),
    ] {
        let start = Instant::now();
        let expr = build_expr();
        let bounds = expr
            .refine_to_default(epsilon)
            .expect("pi arithmetic should succeed");
        let duration = start.elapsed();
        let finite = try_finite_bounds(&bounds).expect("bounds should be finite");
        let mid = finite.midpoint();
        let expected = Binary::from_f64(expected_f64).unwrap();
        println!("  {label:<5}: {duration:>10?}  midpoint: {mid}");
        println!("         expected: {expected}");
    }

    // sin(n*pi) — should be ~0
    let epsilon = XUsize::Finite(32);

    println!();
    println!("== sin(n * pi) — should contain 0 ==");
    println!();

    for multiplier in [1u64, 2, 10, 100] {
        let start = Instant::now();
        let n_pi = if multiplier == 1 {
            pi()
        } else {
            Computable::constant(Binary::new(BigInt::from(multiplier), BigInt::from(0))) * pi()
        };
        let bounds = n_pi
            .sin()
            .refine_to_default(epsilon)
            .expect("sin(n*pi) should succeed");
        let duration = start.elapsed();

        let lower = finite_binary(bounds.small());
        let upper = finite_binary(&bounds.large());
        let contains_zero = lower.mantissa().is_negative() || lower.mantissa().is_zero();
        println!(
            "  sin({multiplier:>3}*pi): {duration:>10?}  bounds: [{lower}, {upper}]  width: {}  contains 0: {contains_zero}",
            bounds.width()
        );
    }

    // High precision
    println!();
    println!("== High Precision Pi ==");
    println!();

    for &bits in &[2048usize, 4096, 8192] {
        let epsilon = XUsize::Finite(bits);
        let start = Instant::now();
        let bounds = pi()
            .refine_to_default(epsilon)
            .expect("high precision pi should succeed");
        let duration = start.elapsed();
        println!(
            "  {bits:>5} bits (~{} digits): {duration:>10?}  width: {}",
            (bits as f64 * 0.301).round() as u64,
            bounds.width()
        );
    }
}

fn main() {
    let mut rng = StdRng::seed_from_u64(7);

    complex_analysis(&mut rng);
    println!();
    summation_analysis(&mut rng);
    println!();
    integer_roots_analysis(&mut rng);
    println!();
    inv_analysis(&mut rng);
    println!();
    sin_analysis(&mut rng);
    println!();
    pi_analysis();
}