Skip to content
This repository was archived by the owner on Dec 1, 2023. It is now read-only.

Floats do not roundtrip correctly #113

Closed
jamii opened this issue May 28, 2015 · 5 comments
Closed

Floats do not roundtrip correctly #113

jamii opened this issue May 28, 2015 · 5 comments

Comments

@jamii
Copy link

jamii commented May 28, 2015

let string = format!("{}", Json::F64(0.107));
let json = Json::from_str(&string).unwrap();
let float = json.as_f64().unwrap();
println!("{:?}", 0.107);
println!("{}", float);
assert_eq!(float, 0.107);

Other implementations eg in v8 get this right. The default rust printer also gets it right.

@alexcrichton
Copy link
Contributor

Running this on nightly gives:

0.107
0.10700000000000001

@alexcrichton
Copy link
Contributor

Er oops, commented too soon, running this on nightly gives:

0.107
0.10700000000000001
thread '<main>' panicked at 'assertion failed: `(left == right)` (left: `0.10700000000000001`, right: `0.107`)', src/main.rs:13

Which is to say that these are indeed different (the floating point printing routines were recently greatly improved) but it appears the parsing code is just a little off.

@jamii
Copy link
Author

jamii commented May 29, 2015

Looks like the std library has the same behaviour - http://is.gd/H6W6oe

@jamii
Copy link
Author

jamii commented May 29, 2015

It's kinda weird that the compiler doesn't appear to be using the same float parser.

@jamii
Copy link
Author

jamii commented May 29, 2015

Dupe of rust-lang/rust#7648

@jamii jamii closed this as completed May 29, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants