Skip to content
This repository has been archived by the owner on Jun 13, 2019. It is now read-only.

Incorrect conversion of json with float values. #25

Closed
shmuga opened this issue Dec 21, 2017 · 2 comments
Closed

Incorrect conversion of json with float values. #25

shmuga opened this issue Dec 21, 2017 · 2 comments

Comments

@shmuga
Copy link

shmuga commented Dec 21, 2017

Hello,

I'm new to Rust and not sure if I choose correct repo to report you a problem but I found a problem with passing JSON from Rust to Lambda response.

Using such code snippet in Rust println! shows 45.74 but in aws response I have { "val": 45.7400016784668 }. Maybe it's Python problem?

#[macro_use(lambda)]
extern crate crowbar;
#[macro_use]
extern crate cpython;
extern crate serde;
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate serde_json;

#[derive(Debug,Serialize)]
struct WithNumber {
    val: f32,
}

lambda!(|event, context| {
    let initial_str = "45.74";
    let obj = WithNumber { val: initial_str.parse::<f32>().unwrap() };
    println!("{:?}", obj); // prints WithNmber { val: 45.74 }
    Ok(serde_json::to_value(&obj)?)
});
@euank
Copy link
Contributor

euank commented Dec 21, 2017

I think this is a rust thing unrelated to crowbar or python or lambda. Even with the debug format string, rust doesn't print the full precision of a floating point number.

If you run this playground, you'll see those extra digits of precision are there in rust too if you look for them.

I think the confusion is entirely from rust's f32 display and debug implementation both implicitly rounding floating point numbers for printing by default.

@shmuga
Copy link
Author

shmuga commented Dec 22, 2017

Heh, thanks a lot, anyway! I thinks this issue could be closed.

@iliana iliana closed this as completed Dec 25, 2017
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

3 participants