tiny-msgpack icon indicating copy to clipboard operation
tiny-msgpack copied to clipboard

Support for Long and Float specific number types

Open mattbishop opened this issue 7 years ago • 3 comments

Msgpack implementations on other platforms usually expect number formats to be in specific families like int and float. They do not do well when a value can be either int or float. Javascript's number type cannot be reliably pinned to be a float or an int, so tiny-msgpack (and msgpack-lite) make a best-guess effort to pick the right msgpack family.

This is not always acceptable for cross-platform message formats that use msgpack. This PR brings in support for specifying the number more specifically. One can encode a Float instance and it will be encoded as float-32 or float-64, even if the value is an int, like 1.0.

Similarly, if one has an int that is greater than 32 bits in length then you can now pass in a Long instance and see it encoded as either uint64 or int-64. This change adds a dependency on the long library, which is a pretty safe dependency.

If you don't want to accept this PR, please let me know and I can release a new version of tiny-msgpack with these changes for my own purposes.

mattbishop avatar Oct 18 '18 23:10 mattbishop

@mattbishop rather than forcing people to use a third-party library for their big numbers, I'd rather support the official BigInt number type, which is available in node v10.4.0+.

As for 32-bit floats, perhaps tiny-msgpack could support a more general way of specifying specific types of any kind (for example, if you wanted to encode a 32-bit integer even though technically an 8-bit slot would be sufficient for a particular case). Perhaps something like:

const { encode, as }  = require('tiny-msgpack');

const uint8array = encode({ foo: as.u8(123) });

JoshuaWise avatar Oct 30 '18 03:10 JoshuaWise

I love the idea of BigInt better than Long. 👍

As for float, it may not be obvious what type of float should be used. My approach was to just signify the number should be treated as in the float family. The lib would figure out the right size. How about as.float(123)?

mattbishop avatar Oct 30 '18 17:10 mattbishop

BigInts are now supported for 64-bit integers (in version 2.0, just released). The problem of deterministic/specific typing still remains.

JoshuaWise avatar Feb 05 '23 05:02 JoshuaWise