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

fix: unpack failed when value between -1.0 to 1.0 #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions msgpack/include/msgpack/msgpack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
19 changes: 19 additions & 0 deletions msgpack/tests/type_packing_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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") {
Expand Down