Skip to content

Commit

Permalink
fix double to string precision (std::numeric_limits<double>::digits10…
Browse files Browse the repository at this point in the history
… + 2)
  • Loading branch information
guolinke committed Jan 4, 2017
1 parent dd42597 commit 21ee594
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
8 changes: 4 additions & 4 deletions include/LightGBM/utils/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ inline static std::string ArrayToString(const std::vector<T>& arr, char delimite
return std::string("");
}
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];
for (size_t i = 1; i < arr.size(); ++i) {
str_buf << delimiter;
Expand All @@ -262,7 +262,7 @@ inline static std::string ArrayToString(const std::vector<T>& arr, size_t n, cha
return std::string("");
}
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];
for (size_t i = 1; i < std::min(n, arr.size()); ++i) {
str_buf << delimiter;
Expand Down Expand Up @@ -312,7 +312,7 @@ inline static std::string Join(const std::vector<T>& strs, const char* delimiter
return std::string("");
}
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];
for (size_t i = 1; i < strs.size(); ++i) {
str_buf << delimiter;
Expand All @@ -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);
end = std::min(end, static_cast<size_t>(strs.size()));
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];
for (size_t i = start + 1; i < end; ++i) {
str_buf << delimiter;
Expand Down
4 changes: 2 additions & 2 deletions src/io/tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ std::string Tree::ToString() {

std::string Tree::ToJSON() {
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 << "\"tree_structure\":" << NodeToJSON(0) << std::endl;
Expand All @@ -162,7 +162,7 @@ std::string Tree::ToJSON() {

std::string Tree::NodeToJSON(int index) {
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) {
// non-leaf
str_buf << "{" << std::endl;
Expand Down
2 changes: 1 addition & 1 deletion tests/python_package_test/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test(self):
pred_from_model_file = bst.predict(X_test)
self.assertEqual(len(pred_from_matr), len(pred_from_model_file))
for preds in zip(pred_from_matr, pred_from_model_file):
self.assertAlmostEqual(*preds, places=15)
self.assertEqual(*preds)


print("----------------------------------------------------------------------")
Expand Down

0 comments on commit 21ee594

Please sign in to comment.