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));

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.

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.