Long floats do not deserialize exactly when float_roundtrip is not enabled · Issue #707 · serde-rs/json (original) (raw)

This program:

fn main() -> Result<(), Box<serde_json::error::Error>> {
    let x0 = 122.416294033786585;
    let s = "122.416294033786585";
    let x1: f64 = serde_json::from_str(s)?;
    println!("{}\n{}\nEqual: {}\n", x0, x1, x0 == x1);
    Ok(())
}

Prints:

122.41629403378658
122.4162940337866
Equal: false

I understand that both etc658 and etc66 can only be approximated by f64, and the closest f64 numbers (as 64 bits and as precise decimal) are:

0x405E9AA48FBB2888: 122.4162940337865848050569184124469757080078125
0x405E9AA48FBB2889: 122.4162940337865990159116336144506931304931640625

Still, I would expect x0 and x1, both f64 values, to be equal. Whatever rounding algorithm is good for the Rust compiler is good for serde_json, right?