On 1. Mar 2022, at 10:41, Jeremy O'Donoghue <[email protected]> wrote:
> 
> One thing I would really like to see, and haven’t found so far, is a reliable 
> mechanism to determine the preferred encoding for floats. Currently my 
> implementations require the caller to decide on the float encoding (float16, 
> float32, float64) as I haven’t been able to find a reference that explains 
> how to straightforwardly determine the most efficient *encoding* for a float 
> without loss of precision.

Ready for copying:

https://github.com/cabo/cbor-ruby/blob/master/ext/cbor/packer.h#L138

(Don’t be confused by the “msgpack” prefixes; this code was adapted from the 
msgpack implementation, and to enable updates from upstream, the renaming is 
done with a bunch of #defines in renamer.h.)

Note the magic lines

 float fv = v;
  if (fv == v) {     … encode as binary16 or binary32 instead of binary64

Which do all the work here.

Unfortunately C doesn’t have a portable way to say the same for binary16, so 
there is some more extensive code following.

Grüße, Carsten

_______________________________________________
COSE mailing list
[email protected]
https://www.ietf.org/mailman/listinfo/cose

Reply via email to