Code Monkey home page Code Monkey logo

Comments (1)

redboltz avatar redboltz commented on June 18, 2024

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

from msgpack-c.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.