Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

f16 serialization #60

Closed
NazarKostetskiy opened this issue Aug 28, 2022 · 3 comments
Closed

f16 serialization #60

NazarKostetskiy opened this issue Aug 28, 2022 · 3 comments
Assignees
Labels

Comments

@NazarKostetskiy
Copy link

NazarKostetskiy commented Aug 28, 2022

Hi. I'm trying to serialize f16, but I'm stuck on it.
I'm using serde feature, but when I use f16_var.serialize(serializer) I'm getting unexpected results.

Example:
Value: 1.0
Serialization result: "15360"`

If I convert the value to f32 and serialize it, everything is fine, I will get "1.0"` as expected.

Running on Ubuntu 20 under WSL

@starkat99
Copy link
Owner

Currently f16 is being serialized as a u16, which works for binary formats, but not so much for string serialization. Will just need to do custom serialization implementation rather then using derive.

@gauteh
Copy link

gauteh commented Dec 1, 2022

Same issue in JSON:

        let f = half::f16::from_f32(45.0);
        println!("{}", json::to_string(&f).unwrap());

20896

@starkat99
Copy link
Owner

Added serde adapters to support this, since serde has design limitations preventing different serialization for different output formats. Use #[serde(serialize_with="f16::serialize_as_f32")] in derive macros of types containing f16 to see float values in JSON or other string output formats.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants