6 releases

0.1.5 Mar 6, 2020
0.1.4 Apr 22, 2019
0.1.1 Mar 30, 2019
0.1.0 Oct 24, 2018

#580 in Math

Download history 34/week @ 2024-06-16 27/week @ 2024-06-23 11/week @ 2024-06-30 12/week @ 2024-07-07 136/week @ 2024-07-14 51/week @ 2024-07-21 47/week @ 2024-07-28 28/week @ 2024-08-04 20/week @ 2024-08-11 20/week @ 2024-08-18 30/week @ 2024-08-25 20/week @ 2024-09-01 20/week @ 2024-09-08 23/week @ 2024-09-15 47/week @ 2024-09-22 58/week @ 2024-09-29

149 downloads per month
Used in 5 crates

MIT/Apache

12KB
213 lines

Open Flash logo

SWF Fixed

crates.io GitHub repository Build status

SWF fixed-point numbers for Rust.

A fixed point number represents a decimal values using evenly-distributed bit patterns (as opposed to floating point numbers where the density increases with the proximity to zero).

A fixed point number can be simply thought as an integer divided by a constant value. It is described by its integer part and fractional part: its mathematical value is integer_part + fractional_part / 2^fractional_bits.

For example, the type Ufixed8p8 is an unsigned fixed point number with an 8 bit integer part and 8 bit fractional part. It can represent the 2^16 values corresponding to u16 / 256, the gap between each value (epsilon) is 1 / 256.

This crate defines the fixed points numbers used by SWF files:

Name Integer part Fractional part Min value Max value Epsilon
Sfixed8P8 i8 u8 -128 128 - 1/256 1 / 256
Ufixed8P8 u8 u8 0 256 - 1/256 1 / 256
Sfixed16P16 i16 u16 -2^15 2^15 - 1/2^16 1 / 2^16
Ufixed16P16 u16 u16 0 2^16 - 1/2^16 1 / 2^16

Usage

use swf_fixed::Sfixed8P8;

fn main() {
  let a = Sfixed8P8::from_epsilons(256);
  let b = Sfixed8P8::from_value(1f32);
  assert_eq!(a, b);
  let sum: Sfixed8P8 = (a + b);
  let sum_value: f32 = sum.into();
  assert_eq!(sum_value, 2.0f32);
}

Contributing

This library is a standard Cargo project. You can test your changes with cargo test.

Prefer non-master branches when sending a PR so your changes can be rebased if needed. All the commits must be made on top of master (fast-forward merge). CI must pass for changes to be accepted.

Dependencies

~110–340KB