skip to content

A Rust device stores a sensor's `f64` reading as `reading as u8`; what does it store for 300.7, -4.2 and NaN, and how should it guard the cast?

level: seniorimportance: should knowfreq 40%

answer

  1. toward zero, then clamp
  2. NaN has to become something
  3. every comparison with NaN is false
  4. round before you range-check

basics

~10 s

It stores 255, 0 and 0: a float-to-integer as cast truncates toward zero, saturates at the target's range and turns NaN into 0. A guard must round, then reject non-finite and out-of-range values explicitly.

solid answer

~40 s

Since Rust 1.45, `as` from a float to an integer is defined: it rounds toward zero, clamps values outside the target range to its minimum or maximum (infinities included), and maps NaN to 0. So 300.7 becomes 255, -4.2 becomes 0, NaN becomes 0, and 42.9 becomes 42, not 43. The cast never fails, which means a broken sensor reporting NaN is stored as a plausible reading of 0. A guard written as `if r < 0.0 || r > 255.0 { reject }` does not help, because every ordered comparison with NaN is false and NaN passes it. Round first if you want nearest rather than truncation, then accept only `(0.0..=255.0).contains(&rounded)`, which is false for NaN and both infinities, and return an `Option` or error for the rest.

code

rust · 24 lines
rust
fn to_level(reading: f64) -> u8 {
    reading as u8
}

fn to_level_checked(reading: f64) -> Option<u8> {
    let rounded = reading.round();
    if (0.0..=255.0).contains(&rounded) {
        Some(rounded as u8)
    } else {
        None // out of range, infinite or NaN
    }
}

fn main() {
    for r in [42.9, 300.7, -4.2, f64::NAN, f64::INFINITY] {
        println!("{r:>5} -> {:>3}  {:?}", to_level(r), to_level_checked(r));
    }
    let r = f64::NAN;
    if r < 0.0 || r > 255.0 {
        println!("rejected");
    } else {
        println!("accepted NaN as {}", r as u8);
    }
}

go deeper

for a junior

Recall the three rules: truncate toward zero, clamp to the type's range, and NaN becomes 0, so the cast never fails.

for a middle

Explain why NaN slips past comparison guards, every ordered comparison with it being false, and write the range check so NaN fails it.

for a senior

Argue that a defined result is not a correct one: a dead sensor stored as 0 corrupts downstream alarms, so conversions return an error, and the 1.45 history explains why.

for a principal

Decide where untrusted floats are validated, once at the boundary into a typed value, so no later code relies on as to clean up bad input.

## The rule for float-to-integer `as` A cast with `as` from `f32` or `f64` to an integer type follows four rules from the Reference: 1. The float is **rounded toward zero** (truncated): `42.9 as u8` is `42` and `-4.2 as i32` is `-4`. 2. A value **above** the target's maximum, including `INFINITY`, **saturates** to the maximum. 3. A value **below** the minimum, including `NEG_INFINITY`, saturates to the minimum. 4. **NaN** becomes `0`. Applied to a `u8` sensor level: | Reading | `reading as u8` | Why | |---|---|---| | `42.9` | `42` | truncated toward zero | | `300.7` | `255` | saturated at `u8::MAX` | | `-4.2` | `0` | saturated at `u8::MIN` | | `f64::NAN` | `0` | NaN maps to zero | | `f64::INFINITY` | `255` | saturated | This behaviour arrived in **Rust 1.45**. Before it, an out-of-range float-to-integer cast was undefined behaviour, a long-standing soundness hole in safe code. The fix chose saturation because every input then has a defined result, and the cast stays infallible. The unsafe method `f64::to_int_unchecked` keeps the old fast path for code that has proven the value is finite and in range; violating that is undefined behaviour. ## Why the defined result is still a bug here Saturation makes the cast safe, not correct. A sensor that fails often reports **NaN** (a `0.0 / 0.0` in a calibration step, a filter fed an infinity, a decoder that marks missing samples as NaN), and the cast stores it as `0`, a perfectly ordinary reading. A spike to 300.7 is stored as 255 with no sign that it was clipped. Downstream code, an alarm threshold or a chart, cannot tell a real 0 from a dead sensor. ## The NaN trap in guards **NaN** is the IEEE 754 "not a number" value. Every ordered comparison involving it is false: `NaN < 0.0`, `NaN > 255.0` and even `NaN == NaN` are all `false`. So this guard, which looks complete, lets NaN through: ```rust fn accepts_nan(r: f64) -> bool { !(r < 0.0 || r > 255.0) // true for NaN: both comparisons are false } ``` rustc helps with one form of the mistake: comparing against `f64::NAN` directly, as in `r == f64::NAN`, triggers the warn-by-default `invalid_nan_comparisons` lint, which suggests `is_nan()`. A range guard written with `<` and `>` gets no warning. ## A guard that holds - **Decide on rounding first.** `as` truncates, so if 42.9 should store as 43, call `reading.round()` before checking and casting. - **Write the check so NaN fails it.** `(0.0..=255.0).contains(&rounded)` uses `>=` and `<=`, which are false for NaN, so NaN, both infinities and out-of-range values are all rejected in one test. - **Return the failure.** An `Option<u8>` or a domain error lets the caller log a sensor fault instead of storing a fake level. - **Keep the cast after the check.** Once the value is known to be in range and integral, `rounded as u8` is exact. ```rust fn to_level_checked(reading: f64) -> Option<u8> { let rounded = reading.round(); if (0.0..=255.0).contains(&rounded) { Some(rounded as u8) } else { None // out of range, infinite or NaN } } ``` With this version, 42.9 gives `Some(43)`, while 300.7, -4.2, NaN and infinity all give `None`. Note the order of operations. Checking the raw reading against `0.0..=255.0` before rounding would reject 255.3 and -0.4, although both round to valid levels (255 and 0). Rounding first and checking the rounded value keeps the accepted set and the stored set identical. If the device should instead clamp out-of-range readings on purpose, say a gauge that pins at full scale, write `reading.clamp(0.0, 255.0)` explicitly and still reject NaN first: `clamp` returns NaN for a NaN input, which the cast would then turn into 0. ## Related float behaviour worth knowing - Float division by zero does not panic: `1.0 / 0.0` is `inf`, `0.0 / 0.0` is NaN. Only integer division panics on zero. - `f64::max` and `f64::min` ignore a single NaN argument and return the other one, which can hide a NaN in a running maximum. - Ordering floats for a sort is a separate subject (`partial_cmp` returns `None` for NaN, and `total_cmp`, stable since 1.62, gives a total order); it belongs with the comparison traits.

  • What did `300.7 as u8` do before Rust 1.45?
    It was undefined behaviour: the language did not define a result for an out-of-range float-to-integer cast, and optimised code could produce arbitrary values. Rust 1.45 defined the cast as saturating with NaN mapping to 0; the unsafe `to_int_unchecked` method, stable since 1.44, remains the unchecked fast path.
  • Why does a running maximum computed with `f64::max` hide a NaN reading?
    `f64::max` ignores a NaN argument when the other is a number and returns that number, so a NaN in the stream simply disappears from the result. That is convenient for skipping gaps but wrong when a NaN signals a sensor fault; check `is_nan()` explicitly where a fault must be reported.

  • NaN as u8 panics at run time.
  • 300.7 as u8 wraps around to 44.
  • as rounds a float to the nearest integer, so 42.9 becomes 43.
  • if r < 0.0 || r > 255.0 rejects NaN.
  • Float-to-integer as casts are still undefined behaviour when out of range.
  • Dividing an f64 by 0.0 panics like integer division.

Assumes Rust 1.99; float-to-integer as casts have saturated, with NaN mapping to 0, since Rust 1.45, and out-of-range casts were undefined behaviour before it.