From 895af262266b9851146f22531c34da29a81787fd Mon Sep 17 00:00:00 2001 From: Jesse Beder Date: Sat, 13 Apr 2013 13:10:36 -0500 Subject: [PATCH] Fix floating point precision on input. --- include/yaml-cpp/node/convert.h | 1 + test/new-api/nodetests.cpp | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/include/yaml-cpp/node/convert.h b/include/yaml-cpp/node/convert.h index 1010d7e..d73f284 100644 --- a/include/yaml-cpp/node/convert.h +++ b/include/yaml-cpp/node/convert.h @@ -78,6 +78,7 @@ namespace YAML struct convert {\ static Node encode(const type& rhs) {\ std::stringstream stream;\ + stream.precision(std::numeric_limits::digits10 + 1);\ stream << rhs;\ return Node(stream.str());\ }\ diff --git a/test/new-api/nodetests.cpp b/test/new-api/nodetests.cpp index 4b2240c..e1140f8 100644 --- a/test/new-api/nodetests.cpp +++ b/test/new-api/nodetests.cpp @@ -498,6 +498,14 @@ namespace Test YAML_ASSERT_THROWS(node.begin()->begin()->IsDefined(), YAML::InvalidNode); return true; } + + TEST FloatingPrecision() + { + const double x = 0.123456789; + YAML::Node node = YAML::Node(x); + YAML_ASSERT(node.as() == x); + return true; + } } void RunNodeTest(TEST (*test)(), const std::string& name, int& passed, int& total) { @@ -560,6 +568,7 @@ namespace Test RunNodeTest(&Node::ForceInsertIntoMap, "force insert into map", passed, total); RunNodeTest(&Node::ResetNode, "reset node", passed, total); RunNodeTest(&Node::DereferenceIteratorError, "dereference iterator error", passed, total); + RunNodeTest(&Node::FloatingPrecision, "floating precision", passed, total); std::cout << "Node tests: " << passed << "/" << total << " passed\n"; return passed == total;