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

Cannot serialize small floats in msgpack float format

Open romankarlstetter opened this issue 1 year ago • 1 comments

Bug description I stumbled over #1018, since I was not expecting my float values to be serialized as int64.

Furthermore, there currently does not seem to be a way to force serialization of a float to the float msgpack format. I find this a bit inconsistent API-wise, because there's pack_fix_int32 (and others) for integers, which does force a certain serialization width.

Version: msgpack-cxx 6.1.0, but the problem is probably present since cpp-4.1.2.

Regarding the spec, I interpreted the following such that for "an object of a certain source type, serializers SHOULD use the format among the formats for that specific source type which represents the data in the smallest number of bytes."

If an object can be represented in multiple possible output formats, serializers SHOULD use the format which represents the data in the smallest number of bytes.

To Reproduce

msgpack::packer<std::stringstream> packer(s);
packer.pack_float(2);

Expected behavior I want to be able to pack a float in the msgpack float representation.

This is also maybe related to #1070.

romankarlstetter avatar Mar 22 '24 15:03 romankarlstetter

It is intentional design. See #1070 and #1017.

Regarding the spec, I interpreted the following such that for "an object of a certain source type, serializers SHOULD use the format among the formats for that specific source type which represents the data in the smallest number of bytes."

If an object can be represented in multiple possible output formats, serializers SHOULD use the format which represents the data in the smallest number of bytes.

We don't assume certain source type.

If you want to enforce float packing, you can create PR for that. I think that pack_fix_int64() is a good example:

https://github.com/msgpack/msgpack-c/blob/e9e06a546c447ba7f04e3eec285071ad9cf5044f/include/msgpack/v1/pack.hpp#L821-L828

It always use MessagePack 64bit signed integer format even if the value is not require 64bit.

Similarlly, you can add pack_fix_float() and pack_fix_double(). When you would create the PR, please add tests too.

Test code locations are after

https://github.com/msgpack/msgpack-c/blob/e9e06a546c447ba7f04e3eec285071ad9cf5044f/test/msgpack_basic.cpp#L148-L200

and

https://github.com/msgpack/msgpack-c/blob/e9e06a546c447ba7f04e3eec285071ad9cf5044f/test/msgpack_basic.cpp#L238-L294

redboltz avatar Mar 23 '24 02:03 redboltz