From 24213c458953b4e1f7c9d11af763ee69e92072e3 Mon Sep 17 00:00:00 2001 From: Albert Lv Date: Sat, 4 Feb 2023 10:32:05 +0800 Subject: [PATCH] fix: unpack failed when value bettwen -1.0 to 1.0 --- msgpack/include/msgpack/msgpack.hpp | 4 ++-- msgpack/tests/type_packing_tests.cpp | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/msgpack/include/msgpack/msgpack.hpp b/msgpack/include/msgpack/msgpack.hpp index a300d5d..7376269 100644 --- a/msgpack/include/msgpack/msgpack.hpp +++ b/msgpack/include/msgpack/msgpack.hpp @@ -889,7 +889,7 @@ void Unpacker::unpack_type(float &value) { if (bits[31]) { mantissa *= -1; } - uint8_t exponent = 0; + int8_t exponent = 0; for (auto i = 0U; i < 8; ++i) { exponent += bits[i + 23] << i; } @@ -928,7 +928,7 @@ void Unpacker::unpack_type(double &value) { if (bits[63]) { mantissa *= -1; } - uint16_t exponent = 0; + int16_t exponent = 0; for (auto i = 0U; i < 11; ++i) { exponent += bits[i + 52] << i; } diff --git a/msgpack/tests/type_packing_tests.cpp b/msgpack/tests/type_packing_tests.cpp index a80f21a..1fccceb 100644 --- a/msgpack/tests/type_packing_tests.cpp +++ b/msgpack/tests/type_packing_tests.cpp @@ -147,6 +147,15 @@ TEST_CASE("Float type packing") { unpacker.process(x); REQUIRE(x == test_num); } + for (auto i = -5; i < 5; ++i) { + float test_num = float(i) * std::pow(0.1, std::abs(i)); + packer.clear(); + packer.process(test_num); + float x = 0.0f; + unpacker.set_data(packer.vector().data(), packer.vector().size()); + unpacker.process(x); + REQUIRE(x == test_num); + } for (auto i = -5; i < 5; ++i) { double test_num = 5.0 + double(i) * 12345.67 / 4.56; @@ -157,6 +166,16 @@ TEST_CASE("Float type packing") { unpacker.process(x); REQUIRE(x == test_num); } + + for (auto i = -5; i < 5; ++i) { + double test_num = double(i) * std::pow(0.1, std::abs(i)); + packer.clear(); + packer.process(test_num); + double x = 0.0; + unpacker.set_data(packer.vector().data(), packer.vector().size()); + unpacker.process(x); + REQUIRE(x == test_num); + } } TEST_CASE("String type packing") {