Skip to content

Commit e5cf3b8

Browse files
committed
Fix #289 negative zero roundtrip (double only)
1 parent 79e81fe commit e5cf3b8

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

include/rapidjson/internal/dtoa.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "itoa.h" // GetDigitsLut()
2323
#include "diyfp.h"
24+
#include "ieee754.h"
2425

2526
RAPIDJSON_NAMESPACE_BEGIN
2627
namespace internal {
@@ -193,6 +194,9 @@ inline char* Prettify(char* buffer, int length, int k) {
193194

194195
inline char* dtoa(double value, char* buffer) {
195196
if (value == 0) {
197+
Double d(value);
198+
if (d.Sign())
199+
*buffer++ = '-'; // -0.0, Issue #289
196200
buffer[0] = '0';
197201
buffer[1] = '.';
198202
buffer[2] = '0';

test/unittest/readertest.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ static void TestParseDouble() {
187187
Reader reader; \
188188
ASSERT_EQ(kParseErrorNone, reader.Parse<fullPrecision ? kParseFullPrecisionFlag : 0>(s, h).Code()); \
189189
EXPECT_EQ(1u, h.step_); \
190+
internal::Double e(x), a(h.actual_); \
191+
EXPECT_EQ(e.Sign(), a.Sign()); \
190192
if (fullPrecision) { \
191193
EXPECT_EQ(x, h.actual_); \
192194
if (x != h.actual_) \
@@ -197,6 +199,7 @@ static void TestParseDouble() {
197199
}
198200

199201
TEST_DOUBLE(fullPrecision, "0.0", 0.0);
202+
TEST_DOUBLE(fullPrecision, "-0.0", -0.0); // For checking issue #289
200203
TEST_DOUBLE(fullPrecision, "1.0", 1.0);
201204
TEST_DOUBLE(fullPrecision, "-1.0", -1.0);
202205
TEST_DOUBLE(fullPrecision, "1.5", 1.5);

test/unittest/writertest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ TEST(Writer, String) {
9393

9494
TEST(Writer, Double) {
9595
TEST_ROUNDTRIP("[1.2345,1.2345678,0.123456789012,1234567.8]");
96-
96+
TEST_ROUNDTRIP("[-0.0]"); // Issue #289
9797
}
9898

9999
TEST(Writer, Transcode) {

0 commit comments

Comments
 (0)