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

#797 in Algorithms

Download history 7/week @ 2024-01-05 12/week @ 2024-01-12 4/week @ 2024-01-19 1/week @ 2024-01-26 6/week @ 2024-02-02 32/week @ 2024-02-09 31/week @ 2024-02-16 28/week @ 2024-02-23 46/week @ 2024-03-01 38/week @ 2024-03-08 34/week @ 2024-03-15 32/week @ 2024-03-22 54/week @ 2024-03-29 27/week @ 2024-04-05 37/week @ 2024-04-12 24/week @ 2024-04-19

147 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–355KB