You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by ya...@apache.org on 2016/08/21 22:07:44 UTC

mesos git commit: Fixed scalar resources output to print full significant digits.

Repository: mesos
Updated Branches:
  refs/heads/master 883f88954 -> a43924a2d


Fixed scalar resources output to print full significant digits.

Review: https://reviews.apache.org/r/51099/


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/a43924a2
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/a43924a2
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/a43924a2

Branch: refs/heads/master
Commit: a43924a2d3003378231070c8a718f659340a3b37
Parents: 883f889
Author: Jiang Yan Xu <xu...@apple.com>
Authored: Sun Aug 21 15:06:35 2016 -0700
Committer: Jiang Yan Xu <xu...@apple.com>
Committed: Sun Aug 21 15:07:25 2016 -0700

----------------------------------------------------------------------
 src/common/values.cpp         | 21 ++++++++++++++++-----
 src/tests/resources_tests.cpp | 13 +++++++++++++
 src/v1/values.cpp             | 21 ++++++++++++++++-----
 3 files changed, 45 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/a43924a2/src/common/values.cpp
----------------------------------------------------------------------
diff --git a/src/common/values.cpp b/src/common/values.cpp
index cc87d94..cb26627 100644
--- a/src/common/values.cpp
+++ b/src/common/values.cpp
@@ -19,6 +19,7 @@
 #include <algorithm>
 #include <cmath>
 #include <initializer_list>
+#include <limits>
 #include <ostream>
 #include <string>
 #include <utility>
@@ -70,11 +71,21 @@ static double convertToFloating(long long fixedValue)
 
 ostream& operator<<(ostream& stream, const Value::Scalar& scalar)
 {
-  // We discard any additional precision from scalar resources before
-  // writing them to an ostream. This is redundant when the scalar is
-  // obtained from one of the operators below, but user-specified
-  // resource values might contain additional precision.
-  return stream << convertToFloating(convertToFixed(scalar.value()));
+  // Output the scalar's full significant digits and save the old
+  // precision.
+  long precision = stream.precision(std::numeric_limits<double>::digits10);
+
+  // We discard any additional precision (of the fractional part)
+  // from scalar resources before writing them to an ostream. This
+  // is redundant when the scalar is obtained from one of the
+  // operators below, but user-specified resource values might
+  // contain additional precision.
+  stream << convertToFloating(convertToFixed(scalar.value()));
+
+  // Return the stream to its original formatting state.
+  stream.precision(precision);
+
+  return stream;
 }
 
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/a43924a2/src/tests/resources_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/resources_tests.cpp b/src/tests/resources_tests.cpp
index e61bdef..4932d95 100644
--- a/src/tests/resources_tests.cpp
+++ b/src/tests/resources_tests.cpp
@@ -878,6 +878,19 @@ TEST(ResourcesTest, PrintingScalarPrecision)
   stream.str("");
   stream << scalar;
   EXPECT_EQ("cpus(*):1.1", stream.str());
+
+  // Large integers are printed with all digits.
+  scalar.mutable_scalar()->set_value(1000001);
+  stream.str("");
+  stream << scalar;
+  EXPECT_EQ("cpus(*):1000001", stream.str());
+
+  // Even larger value with precision in the fractional part limited
+  // to 3 digits but full precision in the integral part preserved.
+  scalar.mutable_scalar()->set_value(99999999999.9994);
+  stream.str("");
+  stream << scalar;
+  EXPECT_EQ("cpus(*):99999999999.999", stream.str());
 }
 
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/a43924a2/src/v1/values.cpp
----------------------------------------------------------------------
diff --git a/src/v1/values.cpp b/src/v1/values.cpp
index 6e3617e..0f16c1e 100644
--- a/src/v1/values.cpp
+++ b/src/v1/values.cpp
@@ -19,6 +19,7 @@
 #include <algorithm>
 #include <cmath>
 #include <initializer_list>
+#include <limits>
 #include <ostream>
 #include <string>
 #include <utility>
@@ -71,11 +72,21 @@ static double convertToFloating(long long fixedValue)
 
 ostream& operator<<(ostream& stream, const Value::Scalar& scalar)
 {
-  // We discard any additional precision from scalar resources before
-  // writing them to an ostream. This is redundant when the scalar is
-  // obtained from one of the operators below, but user-specified
-  // resource values might contain additional precision.
-  return stream << convertToFloating(convertToFixed(scalar.value()));
+  // Output the scalar's full significant digits and save the old
+  // precision.
+  long precision = stream.precision(std::numeric_limits<double>::digits10);
+
+  // We discard any additional precision (of the fractional part)
+  // from scalar resources before writing them to an ostream. This
+  // is redundant when the scalar is obtained from one of the
+  // operators below, but user-specified resource values might
+  // contain additional precision.
+  stream << convertToFloating(convertToFixed(scalar.value()));
+
+  // Return the stream to its original formatting state.
+  stream.precision(precision);
+
+  return stream;
 }