TokTok / hs-msgpack-binary

Haskell implementation of MessagePack / msgpack.org
http://msgpack.org/
Other
16 stars 10 forks source link

Is it really okay to convert msgpack's double to haskell's float? #29

Closed SX91 closed 2 years ago

SX91 commented 7 years ago

I don't think so. Float to Double is perfectly fine, but not vice versa.

First of all, you should not actually use Float in haskell code :) https://wiki.haskell.org/Performance/Floating_point

iphydf commented 7 years ago

No, that's not okay. Let's fix it :).

iphydf commented 7 years ago

Ok, turns out we use Double for ObjectDouble. Where do you see those lossy conversions?

SX91 commented 7 years ago

Look at the MessagePack Float instance.

iphydf commented 7 years ago

I'm looking at it - it looks fine to me. The MessagePack Float instance is something client code can use if they choose to use Float in their types. If client code uses Double, it will use the instance for Double, which does no conversions and generates msgpack double.

You shouldn't be looking at the MessagePack instances, since that's at a higher level. The decoding of msgpack doubles is done in Get.hs, in getDouble.

SX91 commented 7 years ago

You shrink Double value to Float, that's not good. Float to Float is perfectly fine, Float to Double is perfectly fine, Double to Float is not fine at all. Some Float instace should not shrink ObjectDouble's Double to Float IMO.

iphydf commented 7 years ago

So what should the Float instance do instead? Note that the Float instance defines fromObject :: Object -> m Float.

SX91 commented 7 years ago

Extract ObjectFloat's Float. Not ObjectDouble's Double and then shrinking it.

iphydf commented 7 years ago

I don't understand what you mean, since what I understand makes no sense. Can you make a PR for this?

SX91 commented 7 years ago

@iphydf I mean that this https://github.com/TokTok/hs-msgpack/blob/master/src/Data/MessagePack/Class.hs#L144 is not okay

iphydf commented 7 years ago

What should it be doing instead?

SX91 commented 7 years ago

Ignore ObjectFloat constructor (remove that line).

iphydf commented 7 years ago

Ok, make a PR that either fixes it or introduces a test case that's broken by this bug.

iphydf commented 2 years ago

Coercions to some extent are intentional. You're allowed to encode an Int64 and decode it as Int8 which will truncate it.