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

#785 in Algorithms

Download history 8/week @ 2023-12-11 12/week @ 2023-12-18 7/week @ 2023-12-25 15/week @ 2024-01-08 6/week @ 2024-01-15 3/week @ 2024-01-22 12/week @ 2024-02-05 42/week @ 2024-02-12 21/week @ 2024-02-19 40/week @ 2024-02-26 47/week @ 2024-03-04 32/week @ 2024-03-11 34/week @ 2024-03-18 37/week @ 2024-03-25

153 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–350KB