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 @@
#include <vector>
#include <sstream>
#include <cstdint>
#include <algorithm>
namespace LightGBM {
......@@ -140,35 +141,24 @@ inline static const char* Atof(const char* p, double* out) {
// Return signed and scaled floating point result.
*out = sign * (frac ? (value / scale) : (value * scale));
} else {
if (*p == 'n' || *p == 'N') {
++p;
if (!(*p == 'a' || *p == 'A')) {
Log::Stderr("meet error while parsing string to float, expect a nan here");
}
++p;
if (!(*p == 'n' || *p == 'N')) {
Log::Stderr("meet error while parsing string to float, expect a nan here");
}
++p;
// default convert nan to 0
size_t cnt = 0;
while (*(p + cnt) != '\0' && *(p + cnt) != ' '
&& *(p + cnt) != '\t' && *(p + cnt) != ','
&& *(p + cnt) != '\n' && *(p + cnt) != '\r'
&& *(p + cnt) != ':') {
++cnt;
}
std::string tmp_str(p, cnt);
std::transform(tmp_str.begin(), tmp_str.end(), tmp_str.begin(), ::tolower);
if (tmp_str == std::string("na") || tmp_str == std::string("nan")) {
*out = 0;
} else if (*p == 'i' || *p == 'I') {
++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
} else if( tmp_str == std::string("inf") || tmp_str == std::string("infinity")) {
*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 == ' ') {
......
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