Skip to main content

Module numeric

Module numeric 

Source
Expand description

The one place a numeric conversion that can lose information is written.

Every function here is total and states what it does at the edge of the target type: integers become the nearest float, a float narrows to the nearest float of the smaller width, and a float becomes an integer by truncation toward zero, saturating at the type’s bounds, with NaN as zero. Callers use these in place of as, so no conversion in the workspace needs a lint suppression, and each one is a thin wrapper that inlines away.

Functions§

f32_eq
IEEE equality of two f32 values: -0.0 equals 0.0, NaN equals nothing.
f32_to_i64
f32 truncated toward zero into i64, saturating; NaN is zero.
f64_eq
IEEE equality of two f64 values: -0.0 equals 0.0, NaN equals nothing.
f64_to_f32
f64 narrowed to the nearest f32; out-of-range magnitudes become infinities and NaN stays NaN.
f64_to_i64
f64 truncated toward zero into i64, saturating; NaN is zero.
f64_to_u32
f64 truncated toward zero into u32, saturating; NaN and negative values are zero.
f64_to_u64
f64 truncated toward zero into u64, saturating; NaN and negative values are zero.
f64_to_usize
f64 truncated toward zero into usize, saturating; NaN and negative values are zero.
i64_to_f64
An i64 as f64; exact up to 2^53 in magnitude, rounded to nearest beyond.
low_u8
The low eight bits of value; the rest are discarded on purpose.
low_u16
The low sixteen bits of value; the rest are discarded on purpose.
u64_to_f64
A count as f64; exact up to 2^53, rounded to nearest beyond.
u64_to_f32_unit
A 24-bit sample bits scaled to [0, 1); exact in f32.
u64_to_u32
A u64 as u32, saturating at u32::MAX.
usize_to_f32
A count as f32; exact up to 2^24, rounded to nearest beyond.
usize_to_f64
A count as f64; exact up to 2^53, rounded to nearest beyond.
usize_to_u32
An index as u32, saturating at u32::MAX.