Merge pull request #768 from fo40225/fix_msvc_fpfast

Fix msvc /fp:fast test failure
This commit is contained in:
Christopher Dunn 2018-05-08 00:30:14 -05:00 committed by GitHub
commit 2cc9b24f0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 5 deletions

View File

@ -68,6 +68,25 @@
#define snprintf _snprintf
#endif
#if defined(_MSC_VER) && !defined(__clang__)
#if _MSC_VER >= 1900 && defined(_WIN64)
#include <math.h>
#define isnan _isnanf
#else
#include <float.h>
#define isnan _isnan
#endif
#elif __cplusplus >= 201103L
#include <cmath>
#define isnan std::isnan
#else
#include <math.h>
#if !define(isnan)
// IEEE standard states that NaN values will not compare to themselves
#define isnan(x) (x!=x)
#endif
#endif
#if defined(_MSC_VER) && _MSC_VER >= 1400 // VC++ 8.0
// Disable warning about strdup being deprecated.
#pragma warning(disable : 4996)
@ -148,8 +167,8 @@ JSONCPP_STRING valueToString(double value, bool useSpecialFloats, unsigned int p
}
} else {
// IEEE standard states that NaN values will not compare to themselves
if (value != value) {
if (isnan(value)) {
len = snprintf(buffer, sizeof(buffer), useSpecialFloats ? "NaN" : "null");
} else if (value < 0) {
len = snprintf(buffer, sizeof(buffer), useSpecialFloats ? "-Infinity" : "-1e+9999");

View File

@ -18,6 +18,7 @@
#include <sstream>
#include <string>
#include <iomanip>
#include <cmath>
// Make numeric limits more convenient to talk about.
// Assumes int type in 32 bits.
@ -972,8 +973,8 @@ JSONTEST_FIXTURE(ValueTest, integers) {
JSONTEST_ASSERT_EQUAL(Json::UInt64(1) << 63, val.asUInt64());
JSONTEST_ASSERT_EQUAL(Json::UInt64(1) << 63, val.asLargestUInt());
JSONTEST_ASSERT_EQUAL(uint64ToDouble(Json::UInt64(1) << 63), val.asDouble());
JSONTEST_ASSERT_EQUAL(float(uint64ToDouble(Json::UInt64(1) << 63)),
val.asFloat());
JSONTEST_ASSERT_EQUAL(float(Json::UInt64(1) << 63), val.asFloat());
JSONTEST_ASSERT_EQUAL(true, val.asBool());
JSONTEST_ASSERT_STRING_EQUAL("9.2233720368547758e+18",
normalizeFloatingPointStr(JsonTest::ToJsonString(val.asString())));
@ -2405,7 +2406,7 @@ JSONTEST_FIXTURE(CharReaderAllowSpecialFloatsTest, issue209) {
JSONTEST_ASSERT_STRING_EQUAL("", errs);
JSONTEST_ASSERT_EQUAL(3u, root.size());
double n = root["a"].asDouble();
JSONTEST_ASSERT(n != n);
JSONTEST_ASSERT(std::isnan(n));
JSONTEST_ASSERT_EQUAL(std::numeric_limits<double>::infinity(), root.get("b", 0.0));
JSONTEST_ASSERT_EQUAL(-std::numeric_limits<double>::infinity(), root.get("c", 0.0));
}