Skip to main content

Module coerce

Module coerce 

Source
Expand description

Safe numeric conversion helpers.

Only contains helpers for conversions where std’s From/TryFrom traits don’t exist. For integer↔integer conversions, use From/TryFrom directly (with ?From<TryFromIntError> is implemented for RError).

Functions§

f64_to_i32
f64 → i32 — truncation toward zero, then check range.
f64_to_i64
f64 → i64 — truncation toward zero (R as.integer() semantics). Fails for NaN, ±Inf, or values outside i64 range.
f64_to_u32
f64 → u32 — truncation toward zero, then check range.
f64_to_u64
f64 → u64 — truncation toward zero, then check non-negative.
f64_to_usize
f64 → usize — truncation toward zero, then check non-negative.
i64_to_f64
i64 → f64 — always produces a valid f64, but may lose precision for |v| > 2^53. This matches R semantics where as.double() on an integer always succeeds.
u64_to_f64
u64 → f64 — always produces a valid f64, but may lose precision for v > 2^53.
usize_to_f64
usize → f64 — always produces a valid f64, but may lose precision for v > 2^53.