Skip to content

Commit

Permalink
Change isnan handing to only use math.h on windows. Fixes kpu#85.
Browse files Browse the repository at this point in the history
  • Loading branch information
kpu committed Apr 11, 2017
1 parent c8ef1e3 commit f3243ea
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions util/file_piece.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
#include <sys/types.h>
#include <sys/stat.h>

#if defined(_WIN32) || defined(_WIN64)
#include <math.h>
#endif

namespace util {

Expand Down Expand Up @@ -174,19 +176,25 @@ StringPiece FirstToken(StringPiece str) {
return StringPiece(str.data(), i - str.data());
}

// std::isnan is technically C++11 not C++98. But in practice this is a problem for visual studio.
template <class T> inline int CrossPlatformIsNaN(T value) {
#if defined(_WIN32) || defined(_WIN64)
return isnan(value);
#else
return std::isnan(value);
#endif
}

const char *ParseNumber(StringPiece str, float &out) {
int count;
out = kConverter.StringToFloat(str.data(), str.size(), &count);
// std::isnan is C++11, not C++98
using namespace std;
UTIL_THROW_IF_ARG(isnan(out) && str != "NaN" && str != "nan", ParseNumberException, (FirstToken(str)), "float");
UTIL_THROW_IF_ARG(CrossPlatformIsNaN(out) && str != "NaN" && str != "nan", ParseNumberException, (FirstToken(str)), "float");
return str.data() + count;
}
const char *ParseNumber(StringPiece str, double &out) {
int count;
out = kConverter.StringToDouble(str.data(), str.size(), &count);
using namespace std;
UTIL_THROW_IF_ARG(isnan(out) && str != "NaN" && str != "nan", ParseNumberException, (FirstToken(str)), "double");
UTIL_THROW_IF_ARG(CrossPlatformIsNaN(out) && str != "NaN" && str != "nan", ParseNumberException, (FirstToken(str)), "double");
return str.data() + count;
}
const char *ParseNumber(StringPiece str, long int &out) {
Expand Down

0 comments on commit f3243ea

Please sign in to comment.