Crate floating_bar[][src]

This library provides the floating bar type, which allows for efficient representation of rational numbers without loss of precision. It is based on Inigo Quilez’s blog post exploring the concept.

Examples

use floating_bar::r32;

let done = 42;
let total = 100;
let progress = r32::new(done, total);
println!("{}", progress);

Structure

The floating bar types follow a general structure:

  • the sign bit
  • the denominator size field (always log2 of the type’s total size)
  • the fraction field (uses all the remaining bits)

More concretely, they are represented like this:

s = sign, d = denominator size, f = fraction field

r32: sdddddffffffffffffffffffffffffff
r64: sddddddfffffffffffffffffffffffffffffffffffffffffffffffffffffffff

The fraction field stores both the numerator and the denominator as separate values. Their exact size at any given moment depends on the size field, which gives the position of the partition (the “bar”) from the right between the two values.

The denominator has an implicit 1 bit which goes in front of the actual value stored. Thus, a size field of zero has an implicit denominator value of 1, making it compatible with integers.

Features

denormals

This enables denormal values. When the value of the denominator takes up the whole fraction field, the numerator will take an implicit value of 1.

Due to the performance penalty of calculating with denormal values, this is disabled by default.

NaN’s

Unfortunately, it’s possible to have invalid values with this format. Invalid values are those which have a denominator size larger than the number of bits in the fraction field, and are represented as NaN.

This library focuses on the numeric value of the format and is meant to limit the propagation of NaNs. Any operation that could give an undefined value (e.g. when overflowing or dividing by zero) will panic instead of returning a NaN. Effort is put in to not clobber possible payload values in NaNs, but no guarantees are made.

Structs

r32

The 32-bit floating bar type.

r64

The 64-bit floating bar type.

Enums

ParseRatioErr

An error which can be returned when parsing a rational number.