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

Number loses precision on f32s #1004

Closed
Gelbpunkt opened this issue Mar 27, 2023 · 1 comment · Fixed by #1005
Closed

Number loses precision on f32s #1004

Gelbpunkt opened this issue Mar 27, 2023 · 1 comment · Fixed by #1005
Labels

Comments

@Gelbpunkt
Copy link

Gelbpunkt commented Mar 27, 2023

Hi there, I've gone through a couple of issues but haven't found anything that quite matches my problem, so if this is a duplicate, feel free to close this and direct me elsewhere :)

The following small snippet replicates my issue:

use serde::{Deserialize, Serialize};
use serde_json;

#[derive(Deserialize, Serialize, Debug, Clone)]
struct Test(f32);

fn main() {
    let a = Test(5.55);
    let value = serde_json::to_value(a.clone()).unwrap();
    println!("{value:?}");
    let serialized = serde_json::to_string(&value).unwrap();
    println!("{serialized}");
    let serialized = serde_json::to_string(&a).unwrap();
    println!("{serialized}");
}

// Number(5.550000190734863)
// 5.550000190734863
// 5.55

I would expect the serialized values to be identical (and serialized to 5.55), but they aren't. This happens both with and without arbitrary_precision and float_roundtrip enabled. I would've expected this to be precisely 5.55, especially with arbitrary_precision enabled. When changing the type to an f64, it works as expected.

@Gelbpunkt Gelbpunkt changed the title Number loses precision on floats Number loses precision on f32s Mar 27, 2023
@dtolnay
Copy link
Member

dtolnay commented Mar 27, 2023

Good catch, this is a bug.

@dtolnay dtolnay added the bug label Mar 27, 2023
crapStone added a commit to Calciumdibromid/CaBr2 that referenced this issue Mar 30, 2023
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [serde_json](https://github.com/serde-rs/json) | dependencies | patch | `1.0.94` -> `1.0.95` |

---

### Release Notes

<details>
<summary>serde-rs/json</summary>

### [`v1.0.95`](https://github.com/serde-rs/json/releases/tag/v1.0.95)

[Compare Source](serde-rs/json@v1.0.94...v1.0.95)

-   Preserve f32 precision when serializing f32 -> serde_json::Value -> JSON string in "arbitrary_precision" mode ([#&#8203;1004](serde-rs/json#1004), [#&#8203;1005](serde-rs/json#1005))

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNS4yNC41IiwidXBkYXRlZEluVmVyIjoiMzUuMjQuNiJ9-->

Co-authored-by: cabr2-bot <cabr2.help@gmail.com>
Co-authored-by: crapStone <crapstone01@gmail.com>
Reviewed-on: https://codeberg.org/Calciumdibromid/CaBr2/pulls/1835
Reviewed-by: crapStone <crapstone@noreply.codeberg.org>
Co-authored-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
Co-committed-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

2 participants