main/
main.rs

1#![allow(incomplete_features)]
2#![feature(generic_const_exprs)]
3
4use fp::Num;
5
6fn main() {
7    // There are many ways to create a new fixed-point number
8    let options: [fp::I32<10, 5>; 4] = [
9        fp::I32::from_f32(3.125).unwrap(),
10        fp::I32::from_f64(3.125).unwrap(),
11        fp::I32::new(100).unwrap(),
12        100i32.logical_shr::<5>().set_bits().unwrap(),
13    ];
14    assert!(options.iter().min() == options.iter().max()); // all equal
15
16    // Arithmetic works pretty much seamlessly
17    let a = 12i32.set_bits::<5>().unwrap();
18    let b = (-1i32).set_bits::<1>().unwrap();
19    let _ = a + b; // type is I32<6, 0>
20
21    // Addition is associative in value, but not in type
22    let _: fp::I32<6, 0> = a + (b + b);
23    let _: fp::I32<7, 0> = (a + b) + b;
24
25    let x = fp::I32::<21, 20>::from_f32(0.497).unwrap();
26    let y = x / fp::I32::<32, 0>::new(12).unwrap();
27    let z = x + (-y);
28    println!("{} {}", x.raw(), x.into_f64());
29    println!("{} {}", z.raw(), z.into_f64());
30}