Crate fpa [] [src]

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:

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

Examples

// 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.);
}
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());
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);
}

Structs

Q

Fixed point number

Functions

I10F6

Checked cast

I10F22

Checked cast

I11F5

Checked cast

I11F21

Checked cast

I12F4

Checked cast

I12F20

Checked cast

I13F3

Checked cast

I13F19

Checked cast

I14F2

Checked cast

I14F18

Checked cast

I15F1

Checked cast

I15F17

Checked cast

I16F16

Checked cast

I17F15

Checked cast

I18F14

Checked cast

I19F13

Checked cast

I1F7

Checked cast

I1F15

Checked cast

I1F31

Checked cast

I20F12

Checked cast

I21F11

Checked cast

I22F10

Checked cast

I23F9

Checked cast

I24F8

Checked cast

I25F7

Checked cast

I26F6

Checked cast

I27F5

Checked cast

I28F4

Checked cast

I29F3

Checked cast

I2F6

Checked cast

I2F14

Checked cast

I2F30

Checked cast

I30F2

Checked cast

I31F1

Checked cast

I3F5

Checked cast

I3F13

Checked cast

I3F29

Checked cast

I4F4

Checked cast

I4F12

Checked cast

I4F28

Checked cast

I5F3

Checked cast

I5F11

Checked cast

I5F27

Checked cast

I6F2

Checked cast

I6F10

Checked cast

I6F26

Checked cast

I7F1

Checked cast

I7F9

Checked cast

I7F25

Checked cast

I8F8

Checked cast

I8F24

Checked cast

I9F7

Checked cast

I9F23

Checked cast

Type Definitions

I10F6

Fixed point number

I10F22

Fixed point number

I11F5

Fixed point number

I11F21

Fixed point number

I12F4

Fixed point number

I12F20

Fixed point number

I13F3

Fixed point number

I13F19

Fixed point number

I14F2

Fixed point number

I14F18

Fixed point number

I15F1

Fixed point number

I15F17

Fixed point number

I16F16

Fixed point number

I17F15

Fixed point number

I18F14

Fixed point number

I19F13

Fixed point number

I1F7

Fixed point number

I1F15

Fixed point number

I1F31

Fixed point number

I20F12

Fixed point number

I21F11

Fixed point number

I22F10

Fixed point number

I23F9

Fixed point number

I24F8

Fixed point number

I25F7

Fixed point number

I26F6

Fixed point number

I27F5

Fixed point number

I28F4

Fixed point number

I29F3

Fixed point number

I2F6

Fixed point number

I2F14

Fixed point number

I2F30

Fixed point number

I30F2

Fixed point number

I31F1

Fixed point number

I3F5

Fixed point number

I3F13

Fixed point number

I3F29

Fixed point number

I4F4

Fixed point number

I4F12

Fixed point number

I4F28

Fixed point number

I5F3

Fixed point number

I5F11

Fixed point number

I5F27

Fixed point number

I6F2

Fixed point number

I6F10

Fixed point number

I6F26

Fixed point number

I7F1

Fixed point number

I7F9

Fixed point number

I7F25

Fixed point number

I8F8

Fixed point number

I8F24

Fixed point number

I9F7

Fixed point number

I9F23

Fixed point number