Commit 21ee5947 authored by Guolin Ke's avatar Guolin Ke
Browse files

fix double to string precision (std::numeric_limits<double>::digits10 + 2)

parent dd425973
...@@ -247,7 +247,7 @@ inline static std::string ArrayToString(const std::vector<T>& arr, char delimite ...@@ -247,7 +247,7 @@ inline static std::string ArrayToString(const std::vector<T>& arr, char delimite
return std::string(""); return std::string("");
} }
std::stringstream str_buf; std::stringstream str_buf;
str_buf << std::setprecision(std::numeric_limits<double>::digits10 + 1); str_buf << std::setprecision(std::numeric_limits<double>::digits10 + 2);
str_buf << arr[0]; str_buf << arr[0];
for (size_t i = 1; i < arr.size(); ++i) { for (size_t i = 1; i < arr.size(); ++i) {
str_buf << delimiter; str_buf << delimiter;
...@@ -262,7 +262,7 @@ inline static std::string ArrayToString(const std::vector<T>& arr, size_t n, cha ...@@ -262,7 +262,7 @@ inline static std::string ArrayToString(const std::vector<T>& arr, size_t n, cha
return std::string(""); return std::string("");
} }
std::stringstream str_buf; std::stringstream str_buf;
str_buf << std::setprecision(std::numeric_limits<double>::digits10 + 1); str_buf << std::setprecision(std::numeric_limits<double>::digits10 + 2);
str_buf << arr[0]; str_buf << arr[0];
for (size_t i = 1; i < std::min(n, arr.size()); ++i) { for (size_t i = 1; i < std::min(n, arr.size()); ++i) {
str_buf << delimiter; str_buf << delimiter;
...@@ -312,7 +312,7 @@ inline static std::string Join(const std::vector<T>& strs, const char* delimiter ...@@ -312,7 +312,7 @@ inline static std::string Join(const std::vector<T>& strs, const char* delimiter
return std::string(""); return std::string("");
} }
std::stringstream str_buf; std::stringstream str_buf;
str_buf << std::setprecision(std::numeric_limits<double>::digits10 + 1); str_buf << std::setprecision(std::numeric_limits<double>::digits10 + 2);
str_buf << strs[0]; str_buf << strs[0];
for (size_t i = 1; i < strs.size(); ++i) { for (size_t i = 1; i < strs.size(); ++i) {
str_buf << delimiter; str_buf << delimiter;
...@@ -329,7 +329,7 @@ inline static std::string Join(const std::vector<T>& strs, size_t start, size_t ...@@ -329,7 +329,7 @@ inline static std::string Join(const std::vector<T>& strs, size_t start, size_t
start = std::min(start, static_cast<size_t>(strs.size()) - 1); start = std::min(start, static_cast<size_t>(strs.size()) - 1);
end = std::min(end, static_cast<size_t>(strs.size())); end = std::min(end, static_cast<size_t>(strs.size()));
std::stringstream str_buf; std::stringstream str_buf;
str_buf << std::setprecision(std::numeric_limits<double>::digits10 + 1); str_buf << std::setprecision(std::numeric_limits<double>::digits10 + 2);
str_buf << strs[start]; str_buf << strs[start];
for (size_t i = start + 1; i < end; ++i) { for (size_t i = start + 1; i < end; ++i) {
str_buf << delimiter; str_buf << delimiter;
......
...@@ -152,7 +152,7 @@ std::string Tree::ToString() { ...@@ -152,7 +152,7 @@ std::string Tree::ToString() {
std::string Tree::ToJSON() { std::string Tree::ToJSON() {
std::stringstream str_buf; std::stringstream str_buf;
str_buf << std::setprecision(std::numeric_limits<double>::digits10 + 1); str_buf << std::setprecision(std::numeric_limits<double>::digits10 + 2);
str_buf << "\"num_leaves\":" << num_leaves_ << "," << std::endl; str_buf << "\"num_leaves\":" << num_leaves_ << "," << std::endl;
str_buf << "\"tree_structure\":" << NodeToJSON(0) << std::endl; str_buf << "\"tree_structure\":" << NodeToJSON(0) << std::endl;
...@@ -162,7 +162,7 @@ std::string Tree::ToJSON() { ...@@ -162,7 +162,7 @@ std::string Tree::ToJSON() {
std::string Tree::NodeToJSON(int index) { std::string Tree::NodeToJSON(int index) {
std::stringstream str_buf; std::stringstream str_buf;
str_buf << std::setprecision(std::numeric_limits<double>::digits10 + 1); str_buf << std::setprecision(std::numeric_limits<double>::digits10 + 2);
if (index >= 0) { if (index >= 0) {
// non-leaf // non-leaf
str_buf << "{" << std::endl; str_buf << "{" << std::endl;
......
...@@ -47,7 +47,7 @@ class TestBasic(unittest.TestCase): ...@@ -47,7 +47,7 @@ class TestBasic(unittest.TestCase):
pred_from_model_file = bst.predict(X_test) pred_from_model_file = bst.predict(X_test)
self.assertEqual(len(pred_from_matr), len(pred_from_model_file)) self.assertEqual(len(pred_from_matr), len(pred_from_model_file))
for preds in zip(pred_from_matr, pred_from_model_file): for preds in zip(pred_from_matr, pred_from_model_file):
self.assertAlmostEqual(*preds, places=15) self.assertEqual(*preds)
print("----------------------------------------------------------------------") print("----------------------------------------------------------------------")
......
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