Skip to main content

floats/
floats.rs

1use common_range_tools::{IncDecCpCmp, Intersector, NumberIncDecCpCmp};
2
3fn main() {
4    let l = NumberIncDecCpCmp::defaults();
5    // f32 Increment examples
6    assert_eq!(l.inc(&0.2, &0.5), Some(0.7));
7    assert_eq!(l.inc(&1.7, &-0.5), None);
8    assert_eq!(l.inc(&f32::INFINITY, &0.5), None);
9    assert_eq!(l.inc(&f32::INFINITY, &f32::INFINITY), None);
10    assert_eq!(l.inc(&1.0, &f32::INFINITY), Some(f32::INFINITY));
11    assert_eq!(l.inc(&1.0, &f32::NEG_INFINITY), None);
12
13    // f32 Decrement examples
14    assert_eq!(l.dec(&0.5, &0.5), Some(-0.0));
15    assert_eq!(l.dec(&1.7, &-0.5), None);
16    assert_eq!(l.dec(&f32::INFINITY, &0.5), None);
17    assert_eq!(l.dec(&f32::INFINITY, &f32::INFINITY), None);
18    assert_eq!(l.dec(&1.0, &f32::INFINITY), Some(f32::NEG_INFINITY));
19    assert_eq!(l.dec(&1.0, &f32::NEG_INFINITY), None);
20
21    // This sets the step and rebound value to 0.1
22    for r in Intersector::num_sr_from(0.1, &[1.0..=3.1, 2.5..=4.1, 1.9..=7.64]) {
23        print!("Common Range: {}->{}\n", r.start(), r.end());
24    }
25
26    // The resulting output will be:
27    //  Common Range: 1->1.7999999999999998
28    //  Common Range: 1.9->2.4
29    //  Common Range: 2.5->3.1
30    //  Common Range: 3.2->4.1
31    //  Common Range: 4.199999999999999->7.64
32}