-
Notifications
You must be signed in to change notification settings - Fork 162
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
Decoding of 32 bit floats is not precise #230
Comments
I think this library behaves correctly, you see the precision loss of the float64 ⇒ float32 conversion. > 59.48.toFixed(50)
"59.47999999999999687361196265555918216705322265625000"
> Math.fround(59.48).toFixed(50)
"59.47999954223632812500000000000000000000000000000000" |
I see what you're saying with the conversion but I really need encoding/decoding to round trip correctly so that would suggest to me that the |
This is a typical IEEE 754 rounding error. In short, there's no way to precisely represent the float number 59.48 in 32-bit binary form. The closest floating point number that can be represented is 59.47999954223633. |
I am trying to understand why I get unexpected values decoding 32-bit floating point values coming from my back-end.
This is true
But looking at the intermediate value, the encoding is producing a 64-bit floating point representation
[203, 64, 77, 189, 112, 163, 215, 10, 61]
.So far, all is good. The problem starts when I post my raw data to my back-end and that does the serialization. That has been told the type up-front and encodes the input as a 32-bit floating point number ->
[202, 66, 109, 235, 133]
. That should be fine, and when decoding on the back-end, I get the exact value back.However, when decoding using this library like
decode([202, 66, 109, 235, 133])
what I actually get is59.47999954223633
.I believe this is a bug because the library can safely round-trip floating point numbers without a loss of precision so it seems like there is an issue in the handling of the 32-bit msgpack representation of this number.
The text was updated successfully, but these errors were encountered: