1 unstable release

Uses old Rust 2015

0.1.0 Apr 1, 2017

#47 in #fixed-point

Download history 72/week @ 2023-12-12 83/week @ 2023-12-19 53/week @ 2023-12-26 43/week @ 2024-01-02 90/week @ 2024-01-09 77/week @ 2024-01-16 59/week @ 2024-01-23 44/week @ 2024-01-30 64/week @ 2024-02-06 95/week @ 2024-02-13 84/week @ 2024-02-20 96/week @ 2024-02-27 123/week @ 2024-03-05 89/week @ 2024-03-12 120/week @ 2024-03-19 117/week @ 2024-03-26

470 downloads per month
Used in 12 crates (3 directly)

MIT/Apache

74KB
2K SLoC

fpa

Fixed Point Arithmetic

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


lib.rs:

Fixed point arithmetic

A fixed point number is an alternative representation for a real number. IEEE floats, f32 and f64, being the standard format in processors with Floating Point Units (FPU). You should consider using fixed numbers on systems where there's no FPU and performance is critical as fixed point arithmetic is faster than software emulated IEEE float arithmetic. Do note that fixed point numbers tend to be more prone to overflows as they operate in ranges much smaller than floats.

The fixed point numbers exposed in this library use the following naming convention: IxFy, where x is the number of bits used for the integer part and y is the number of bits used for the fractional part.

Unlike IEEE floats, fixed points numbers have fixed precision. One can exchange range for precision by selecting different values for x and y:

  • Range: [-2 ^ (y - 1), 2 ^ (y - 1) - 2 ^ (-x)]
  • Precision: 2 ^ (-x)

For example, the type I1F7 has range [-1, 0.9921875] and precision 0.0078125.

Examples

  • Casts
// https://crates.io/crates/cast
extern crate cast;
extern crate fpa;

use cast::f64;
// 32-bit fixed point number, 16 bits for the integer part and 16 bits for
// the fractional part
use fpa::I16F16;

fn main() {
    // casts an integer into a fixed point number (infallible)
    let q = I16F16(1i8);

    // casts the fixed point number into a float (infallible)
    let f = f64(q);

    assert_eq!(f, 1.);
}
  • Arithmetic
use fpa::I16F16;

// NOTE the `f64` -> `I16F16` cast is fallible because of NaN and infinity
assert_eq!(I16F16(1.25_f64).unwrap() + I16F16(2.75_f64).unwrap(),
           I16F16(4_f64).unwrap());

assert_eq!(I16F16(2_f64).unwrap() / I16F16(0.5_f64).unwrap(),
           I16F16(4_f64).unwrap());

assert_eq!(I16F16(2_f64).unwrap() * I16F16(0.5_f64).unwrap(),
           I16F16(1_f64).unwrap());
  • Trigonometry
extern crate cast;
extern crate fpa;

use cast::f64;
use fpa::I2F30;

fn main() {
    let (r, _) = I2F30(0.3_f64).unwrap().polar(I2F30(0.4_f64).unwrap());
    assert!((f64(r) - 0.5).abs() < 1e-5);
}

Dependencies

~195KB