This commit is contained in:
chenguoping 2020-05-15 17:35:43 +08:00
parent e175150f5b
commit 779a0ec7df
3 changed files with 61 additions and 3 deletions

View file

@ -194,7 +194,8 @@ class binary_writer
} }
else else
{ {
if (j.m_value.number_float < std::numeric_limits<float>::max() and if (j.m_value.number_float > -std::numeric_limits<float>::min() and
j.m_value.number_float < std::numeric_limits<float>::max() and
static_cast<double>(static_cast<float>(j.m_value.number_float)) == j.m_value.number_float) static_cast<double>(static_cast<float>(j.m_value.number_float)) == j.m_value.number_float)
{ {
oa->write_character(get_cbor_float_prefix(static_cast<float>(j.m_value.number_float))); oa->write_character(get_cbor_float_prefix(static_cast<float>(j.m_value.number_float)));

View file

@ -12143,7 +12143,8 @@ class binary_writer
} }
else else
{ {
if (j.m_value.number_float < std::numeric_limits<float>::max() and if (j.m_value.number_float > -std::numeric_limits<float>::min() and
j.m_value.number_float < std::numeric_limits<float>::max() and
static_cast<double>(static_cast<float>(j.m_value.number_float)) == j.m_value.number_float) static_cast<double>(static_cast<float>(j.m_value.number_float)) == j.m_value.number_float)
{ {
oa->write_character(get_cbor_float_prefix(static_cast<float>(j.m_value.number_float))); oa->write_character(get_cbor_float_prefix(static_cast<float>(j.m_value.number_float)));

View file

@ -834,7 +834,7 @@ TEST_CASE("CBOR")
} }
} }
SECTION("float") SECTION("double-precision float")
{ {
SECTION("3.1415925") SECTION("3.1415925")
{ {
@ -853,6 +853,10 @@ TEST_CASE("CBOR")
CHECK(json::from_cbor(result, true, false) == j); CHECK(json::from_cbor(result, true, false) == j);
} }
}
SECTION("single-precision float")
{
SECTION("0.5") SECTION("0.5")
{ {
double v = 0.5; double v = 0.5;
@ -867,6 +871,58 @@ TEST_CASE("CBOR")
CHECK(json::from_cbor(result) == j); CHECK(json::from_cbor(result) == j);
CHECK(json::from_cbor(result) == v); CHECK(json::from_cbor(result) == v);
} }
SECTION("0.0")
{
double v = 0.0;
json j = v;
// its double-precision binary value is:
// {0xfb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
std::vector<uint8_t> expected = {0xfa, 0x00, 0x00, 0x00, 0x00};
const auto result = json::to_cbor(j);
CHECK(result == expected);
// roundtrip
CHECK(json::from_cbor(result) == j);
CHECK(json::from_cbor(result) == v);
}
SECTION("-0.0")
{
double v = -0.0;
json j = v;
// its double-precision binary value is:
// {0xfb, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
std::vector<uint8_t> expected = {0xfa, 0x80, 0x00, 0x00, 0x00};
const auto result = json::to_cbor(j);
CHECK(result == expected);
// roundtrip
CHECK(json::from_cbor(result) == j);
CHECK(json::from_cbor(result) == v);
}
SECTION("100.0")
{
double v = 100.0;
json j = v;
// its double-precision binary value is:
// {0xfb, 0x40, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
std::vector<uint8_t> expected = {0xfa, 0x42, 0xc8, 0x00, 0x00};
const auto result = json::to_cbor(j);
CHECK(result == expected);
// roundtrip
CHECK(json::from_cbor(result) == j);
CHECK(json::from_cbor(result) == v);
}
SECTION("200.0")
{
double v = 200.0;
json j = v;
// its double-precision binary value is:
// {0xfb, 0x40, 0x69, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
std::vector<uint8_t> expected = {0xfa, 0x43, 0x48, 0x00, 0x00};
const auto result = json::to_cbor(j);
CHECK(result == expected);
// roundtrip
CHECK(json::from_cbor(result) == j);
CHECK(json::from_cbor(result) == v);
}
} }
SECTION("half-precision float (edge cases)") SECTION("half-precision float (edge cases)")