Commit b028ac5f authored by Guolin Ke's avatar Guolin Ke
Browse files

refine logic for na, nan and inf processing.

parent 670a1d70
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <vector> #include <vector>
#include <sstream> #include <sstream>
#include <cstdint> #include <cstdint>
#include <algorithm>
namespace LightGBM { namespace LightGBM {
...@@ -140,35 +141,24 @@ inline static const char* Atof(const char* p, double* out) { ...@@ -140,35 +141,24 @@ inline static const char* Atof(const char* p, double* out) {
// Return signed and scaled floating point result. // Return signed and scaled floating point result.
*out = sign * (frac ? (value / scale) : (value * scale)); *out = sign * (frac ? (value / scale) : (value * scale));
} else { } else {
if (*p == 'n' || *p == 'N') { size_t cnt = 0;
++p; while (*(p + cnt) != '\0' && *(p + cnt) != ' '
if (!(*p == 'a' || *p == 'A')) { && *(p + cnt) != '\t' && *(p + cnt) != ','
Log::Stderr("meet error while parsing string to float, expect a nan here"); && *(p + cnt) != '\n' && *(p + cnt) != '\r'
} && *(p + cnt) != ':') {
++p; ++cnt;
if (!(*p == 'n' || *p == 'N')) { }
Log::Stderr("meet error while parsing string to float, expect a nan here"); std::string tmp_str(p, cnt);
} std::transform(tmp_str.begin(), tmp_str.end(), tmp_str.begin(), ::tolower);
++p; if (tmp_str == std::string("na") || tmp_str == std::string("nan")) {
// default convert nan to 0
*out = 0; *out = 0;
} else if (*p == 'i' || *p == 'I') { } else if( tmp_str == std::string("inf") || tmp_str == std::string("infinity")) {
++p;
if (!(*p == 'n' || *p == 'N')) {
Log::Stderr("meet error while parsing string to float, expect a inf here");
}
++p;
if (!(*p == 'f' || *p == 'F')) {
Log::Stderr("meet error while parsing string to float, expect a inf here");
}
++p;
// default inf
*out = sign * 1e308; *out = sign * 1e308;
} else {
if (*p != '\0') {
Log::Stderr("Meet unknow characters while parsing string to float");
} }
else {
Log::Stderr("Unknow token %s in data file", tmp_str.c_str());
} }
p += cnt;
} }
while (*p == ' ') { while (*p == ' ') {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment