json icon indicating copy to clipboard operation
json copied to clipboard

Arbitrary precision numbers with differing formats but identical values are not considered equal

Open atampy25 opened this issue 2 years ago • 0 comments

When using the arbitrary_precision feature, numbers are (from what I can tell) internally stored as Strings. This results in cases such as this:

use serde_json::{from_str, Value};

fn main() {
    let a: Value = from_str("0.00005").unwrap();
    let b: Value = from_str("0.5e-4").unwrap();

    assert_eq!(a, b); // Panics when arbitrary_precision feature is enabled!
}

One would assume that because 0.00005 and 0.5e-4 are the same number they would be equal, and this is indeed what happens when compiling the above code with features = []. When arbitrary precision is enabled, however, the comparison fails because the number format is not normalised when parsing.

I can see this being intended behaviour because of performance concerns (for example) during parsing, but I would argue that users generally expect equality to work for numbers based on the actual number value - this bug was discovered because of formatting conventions for numbers being different to each other.

atampy25 avatar Feb 02 '23 09:02 atampy25