Skip to content

Commit 2cc9b24

Browse files
authored
Merge pull request open-source-parsers#768 from fo40225/fix_msvc_fpfast
Fix msvc /fp:fast test failure
2 parents 0221111 + 6e5e9be commit 2cc9b24

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

src/lib_json/json_writer.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,25 @@
6868
#define snprintf _snprintf
6969
#endif
7070

71+
#if defined(_MSC_VER) && !defined(__clang__)
72+
#if _MSC_VER >= 1900 && defined(_WIN64)
73+
#include <math.h>
74+
#define isnan _isnanf
75+
#else
76+
#include <float.h>
77+
#define isnan _isnan
78+
#endif
79+
#elif __cplusplus >= 201103L
80+
#include <cmath>
81+
#define isnan std::isnan
82+
#else
83+
#include <math.h>
84+
#if !define(isnan)
85+
// IEEE standard states that NaN values will not compare to themselves
86+
#define isnan(x) (x!=x)
87+
#endif
88+
#endif
89+
7190
#if defined(_MSC_VER) && _MSC_VER >= 1400 // VC++ 8.0
7291
// Disable warning about strdup being deprecated.
7392
#pragma warning(disable : 4996)
@@ -148,8 +167,8 @@ JSONCPP_STRING valueToString(double value, bool useSpecialFloats, unsigned int p
148167
}
149168

150169
} else {
151-
// IEEE standard states that NaN values will not compare to themselves
152-
if (value != value) {
170+
171+
if (isnan(value)) {
153172
len = snprintf(buffer, sizeof(buffer), useSpecialFloats ? "NaN" : "null");
154173
} else if (value < 0) {
155174
len = snprintf(buffer, sizeof(buffer), useSpecialFloats ? "-Infinity" : "-1e+9999");

src/test_lib_json/main.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <sstream>
1919
#include <string>
2020
#include <iomanip>
21+
#include <cmath>
2122

2223
// Make numeric limits more convenient to talk about.
2324
// Assumes int type in 32 bits.
@@ -972,8 +973,8 @@ JSONTEST_FIXTURE(ValueTest, integers) {
972973
JSONTEST_ASSERT_EQUAL(Json::UInt64(1) << 63, val.asUInt64());
973974
JSONTEST_ASSERT_EQUAL(Json::UInt64(1) << 63, val.asLargestUInt());
974975
JSONTEST_ASSERT_EQUAL(uint64ToDouble(Json::UInt64(1) << 63), val.asDouble());
975-
JSONTEST_ASSERT_EQUAL(float(uint64ToDouble(Json::UInt64(1) << 63)),
976-
val.asFloat());
976+
JSONTEST_ASSERT_EQUAL(float(Json::UInt64(1) << 63), val.asFloat());
977+
977978
JSONTEST_ASSERT_EQUAL(true, val.asBool());
978979
JSONTEST_ASSERT_STRING_EQUAL("9.2233720368547758e+18",
979980
normalizeFloatingPointStr(JsonTest::ToJsonString(val.asString())));
@@ -2405,7 +2406,7 @@ JSONTEST_FIXTURE(CharReaderAllowSpecialFloatsTest, issue209) {
24052406
JSONTEST_ASSERT_STRING_EQUAL("", errs);
24062407
JSONTEST_ASSERT_EQUAL(3u, root.size());
24072408
double n = root["a"].asDouble();
2408-
JSONTEST_ASSERT(n != n);
2409+
JSONTEST_ASSERT(std::isnan(n));
24092410
JSONTEST_ASSERT_EQUAL(std::numeric_limits<double>::infinity(), root.get("b", 0.0));
24102411
JSONTEST_ASSERT_EQUAL(-std::numeric_limits<double>::infinity(), root.get("c", 0.0));
24112412
}

0 commit comments

Comments
 (0)