ugorji / go

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

[CBOR] Support for automatically encoding floats as smallest possible #331

Closed philandstuff closed 4 years ago

philandstuff commented 4 years ago

The CBOR Working Group is working on an update to RFC7049, the original cbor RFC. This update recommends the following (see here):

The preferred encoding for a floating point value is the shortest floating point encoding that preserves its value, e.g., 0xf94580 for the number 5.5, and 0xfa45ad9c00 for the number 5555.5, unless the CBOR-based protocol specifically excludes the use of the shorter floating point encodings. For NaN values, a shorter encoding is preferred if zero-padding the shorter significand towards the right reconstitutes the original NaN value (for many applications, the single NaN encoding 0xf97e00 will suffice).

Currently in CBOR, codec encodes float32 values as 32-bit floats, and float64 values as 64-bit floats; however codec is happy to decode 16- or 32-bit values as float32 and 16-, 32- or 64-bit values as float64.

I propose that we change the CBOR encoding to behave as quoted above: any floating point value will be encoded as the smallest possible representation for that value. Would this change be acceptable?

The driver for this is dhall-lang/dhall-lang#953; in dhall-lang/dhall-lang#958 we also discussed potentially offering funding to whoever implements this via opencollective.