Skip to main content

Crate unsigned_float

Crate unsigned_float 

Source
Expand description

Unsigned floating-point formats for values that can never be negative.

This crate provides compact unsigned float newtypes with IEEE-like exponent and mantissa fields, but no sign bit. The missing sign bit can be spent on precision or range, removes negative zero, and makes total ordering a raw unsigned integer comparison.

The ergonomic aliases Uf8, Uf16, and Uf32 point at the default concrete layouts Uf8E4M4, Uf16E5M11, and Uf32E8M24. Alternate layouts such as Uf8E5M3 and Uf16E6M10 are exported as distinct types so their range and precision tradeoffs stay explicit. With the f128 feature enabled, Uf64 is also available and promotes through nightly primitive f128.

§Conversions

Explicit constructors such as Uf8::from_f32 encode the input into the target format. Negative native values become NaN, and overflow becomes infinity.

Use TryFrom when invalid or unrepresentable inputs should be rejected:

use unsigned_float::{ConversionError, Uf16};

assert_eq!(Uf16::try_from(42_u32), Ok(Uf16::from_f32(42.0)));
assert_eq!(Uf16::try_from(-1_i32), Err(ConversionError::Negative));

§Exponents

Use PowUf to raise native floats to unsigned-float exponents:

use unsigned_float::{PowUf, Uf16};

let root = 9.0_f32.powuf(Uf16::from_f32(0.5));
assert_eq!(root, 3.0);

PowUf uses exact kernels for common exponent shapes such as zero, one, one-half, and small integers, then falls back to libm for the general fractional case. Same-layout and cross-layout UF8 exponentiation uses generated lookup tables and returns the UF8 layout of the base.

Pow1mUf evaluates (1 - u)^a directly. This keeps the complement operation explicit and lets UF8 use generated lookup tables without materializing 1 - u as a separately rounded value.

Structs§

Uf8E4M4
An 8-bit unsigned float with 4 exponent bits and 4 mantissa bits.
Uf8E5M3
An 8-bit unsigned float with 5 exponent bits and 3 mantissa bits.
Uf16E5M11
A 16-bit unsigned float with 5 exponent bits and 11 mantissa bits.
Uf16E6M10
A 16-bit unsigned float with 6 exponent bits and 10 mantissa bits.
Uf32E8M24
A 32-bit unsigned float with 8 exponent bits and 24 mantissa bits.
Uf64E11M52
A 64-bit unsigned float with 11 exponent bits and 52 mantissa bits.

Enums§

ConversionError
Error returned by fallible conversions into unsigned float types.

Traits§

Pow1mUf
Extension trait for evaluating (1 - self)^rhs.
PowUf
Extension trait for raising native floats to unsigned-float exponents.

Type Aliases§

Uf8
Default 8-bit unsigned float layout.
Uf16
Default 16-bit unsigned float layout.
Uf32
Default 32-bit unsigned float layout.
Uf64
Default 64-bit unsigned float layout. Requires the f128 feature.