Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot serialize small floats in msgpack float format #1111

Open
romankarlstetter opened this issue Mar 22, 2024 · 1 comment
Open

Cannot serialize small floats in msgpack float format #1111

romankarlstetter opened this issue Mar 22, 2024 · 1 comment

Comments

@romankarlstetter
Copy link

romankarlstetter commented Mar 22, 2024

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.

@redboltz
Copy link
Contributor

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:

template <typename Stream>
inline packer<Stream>& packer<Stream>::pack_fix_int64(int64_t d)
{
char buf[9];
buf[0] = static_cast<char>(0xd3u); _msgpack_store64(&buf[1], d);
append_buffer(buf, 9);
return *this;
}

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

#if !defined(_MSC_VER) || _MSC_VER >=1800
BOOST_AUTO_TEST_CASE(simple_buffer_float)
{
vector<float> v;
v.push_back(0.0);
v.push_back(-0.0);
v.push_back(1.0);
v.push_back(-1.0);
v.push_back(1.1f);
v.push_back(-1.1f);
v.push_back(numeric_limits<float>::min());
v.push_back(numeric_limits<float>::max());
v.push_back(nanf("tag"));
if (numeric_limits<float>::has_infinity) {
v.push_back(numeric_limits<float>::infinity());
v.push_back(-numeric_limits<float>::infinity());
}
if (numeric_limits<float>::has_quiet_NaN) {
v.push_back(numeric_limits<float>::quiet_NaN());
}
if (numeric_limits<float>::has_signaling_NaN) {
v.push_back(numeric_limits<float>::signaling_NaN());
}
for (unsigned int i = 0; i < kLoop; i++) {
v.push_back(static_cast<float>(msgpack_rand()));
v.push_back(static_cast<float>(-msgpack_rand()));
}
for (unsigned int i = 0; i < v.size() ; i++) {
msgpack::sbuffer sbuf;
float val1 = v[i];
msgpack::pack(sbuf, val1);
msgpack::object_handle oh =
msgpack::unpack(sbuf.data(), sbuf.size());
float val2 = oh.get().as<float>();
if (std::isnan(val1))
BOOST_CHECK(std::isnan(val2));
else if (std::isinf(val1))
BOOST_CHECK(std::isinf(val2));
else
BOOST_CHECK(fabs(val2 - val1) <= kEPS);
// check for compact storing of float
if (val1 == val1 && val1 >= float(std::numeric_limits<int64_t>::min()) && val1 <= float(std::numeric_limits<int64_t>::max()) && val1 == float(int64_t(val1)))
BOOST_REQUIRE_EQUAL(sbuf.size(),1);
else
BOOST_REQUIRE_EQUAL(sbuf.data()[0],char(0xca));
}
}
#endif // !defined(_MSC_VER) || _MSC_VER >=1800

and

#if !defined(_MSC_VER) || _MSC_VER >=1800
BOOST_AUTO_TEST_CASE(simple_buffer_double)
{
vector<double> v;
v.push_back(0.0);
v.push_back(-0.0);
v.push_back(1.0);
v.push_back(-1.0);
v.push_back(1.1);
v.push_back(-1.1);
v.push_back(numeric_limits<double>::min());
v.push_back(numeric_limits<double>::max());
v.push_back(nanf("tag"));
if (numeric_limits<double>::has_infinity) {
v.push_back(numeric_limits<double>::infinity());
v.push_back(-numeric_limits<double>::infinity());
}
if (numeric_limits<double>::has_quiet_NaN) {
v.push_back(numeric_limits<double>::quiet_NaN());
}
if (numeric_limits<double>::has_signaling_NaN) {
v.push_back(numeric_limits<double>::signaling_NaN());
}
for (unsigned int i = 0; i < kLoop; i++) {
v.push_back(msgpack_rand());
v.push_back(-msgpack_rand());
}
for (unsigned int i = 0; i < kLoop; i++) {
v.push_back(msgpack_rand());
v.push_back(-msgpack_rand());
}
for (unsigned int i = 0; i < v.size() ; i++) {
msgpack::sbuffer sbuf;
double val1 = v[i];
msgpack::pack(sbuf, val1);
msgpack::object_handle oh =
msgpack::unpack(sbuf.data(), sbuf.size());
double val2 = oh.get().as<double>();
if (std::isnan(val1))
BOOST_CHECK(std::isnan(val2));
else if (std::isinf(val1))
BOOST_CHECK(std::isinf(val2));
else
BOOST_CHECK(fabs(val2 - val1) <= kEPS);
// check for compact storing of double
if (val1 == val1 && val1 >= double(std::numeric_limits<int64_t>::min()) && val1 <= double(std::numeric_limits<int64_t>::max()) && val1 == double(int64_t(val1)))
BOOST_REQUIRE_EQUAL(sbuf.size(),1);
else
BOOST_REQUIRE_EQUAL(uint8_t(sbuf.data()[0]),uint8_t(0xcb));
}
}
#endif // !defined(_MSC_VER) || _MSC_VER >=1800

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants