ugorji / go

idiomatic codec and rpc lib for msgpack, cbor, json, etc. msgpack.org[Go]
MIT License
1.83k stars 294 forks source link

Fix the noFrac code to handle `0`. #365

Closed csilvers closed 2 years ago

csilvers commented 2 years ago

In f4b40f6 code was added to allow reading a (non-fraction-containing) float64 into a uint64 struct. This code depends on a new function noFrac64 to decide if the cast is legal. (There is also a noFrac32.) However, noFrac64 and noFrac32 do not return the correct result when the input is 0.0. In that case, subtracting the bias yields a negative number (which is then interpreted as a very large positive number).

I don't know if there's a way to fix this without adding a new comparison function (like replacing the subtraction of the bias with a bitwise operation of some sort or another), but this fix is simple enough. I didn't see any tests for this code so I didn't add a test for it.

Fixes #364