quickstep-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zu...@apache.org
Subject [20/44] incubator-quickstep git commit: Added a method for double parsing with significant number argument. (#216)
Date Wed, 08 Jun 2016 19:19:12 GMT
Added a method for double parsing with significant number argument. (#216)

Project: http://git-wip-us.apache.org/repos/asf/incubator-quickstep/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-quickstep/commit/19aeb6c5
Tree: http://git-wip-us.apache.org/repos/asf/incubator-quickstep/tree/19aeb6c5
Diff: http://git-wip-us.apache.org/repos/asf/incubator-quickstep/diff/19aeb6c5

Branch: refs/heads/master
Commit: 19aeb6c5d5985312978a0dfdca24777bd8d552cd
Parents: d75ae42
Author: Hakan Memisoglu <hakanmemisoglu@gmail.com>
Authored: Sun May 8 08:22:17 2016 -0500
Committer: Zuyu Zhang <zzhang@pivotal.io>
Committed: Wed Jun 8 11:57:42 2016 -0700

----------------------------------------------------------------------
 utility/StringUtil.cpp | 14 ++++++++++++++
 utility/StringUtil.hpp | 18 ++++++++++++++++++
 2 files changed, 32 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/19aeb6c5/utility/StringUtil.cpp
----------------------------------------------------------------------
diff --git a/utility/StringUtil.cpp b/utility/StringUtil.cpp
index 77b0a3a..b38e8f4 100644
--- a/utility/StringUtil.cpp
+++ b/utility/StringUtil.cpp
@@ -22,10 +22,13 @@
 #include <algorithm>
 #include <cctype>
 #include <cinttypes>
+#include <cmath>
 #include <cstddef>
 #include <cstdint>
 #include <cstdio>
 #include <cstdlib>
+#include <iomanip>
+#include <sstream>
 #include <string>
 #include <vector>
 
@@ -128,4 +131,15 @@ string ToZeroPaddedString(std::uint64_t val, int pad_width) {
   return result;
 }
 
+std::string
+DoubleToStringWithSignificantDigits(double val,
+                                    std::uint64_t significant_digits) {
+  std::uint64_t precision_needed =
+      (val >= 1.0) ? significant_digits
+                   : significant_digits + -(std::trunc(std::log10(val)));
+  std::stringstream string_buffer;
+  string_buffer << std::fixed << std::setprecision(precision_needed) <<
val;
+  return string_buffer.str();
+}
+
 }  // namespace quickstep

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/19aeb6c5/utility/StringUtil.hpp
----------------------------------------------------------------------
diff --git a/utility/StringUtil.hpp b/utility/StringUtil.hpp
index 26c04a4..bd138bb 100644
--- a/utility/StringUtil.hpp
+++ b/utility/StringUtil.hpp
@@ -70,6 +70,24 @@ extern bool ParseIntString(const std::string &int_string,
  */
 extern std::string ToZeroPaddedString(std::uint64_t val, int pad_width);
 
+
+/**
+ * @brief Parse double with significant digits.
+ *
+ * @param val The value that will be parsed.
+ * @param significant_digit The number of non-zero digits that will be parsed
+ *        in the fraction part of double.
+ * @return Corresponding string representation of parsed double.
+ *         Ex:
+ *         DoubleToStringWithSignificantDigits(3.124355123, 3)
+ *          => 3.124
+ *         DoubleToStringWithSignificantDigits(0.00000000323411, 3)
+ *         => 0.00000000323
+ **/
+extern std::string
+DoubleToStringWithSignificantDigits(double val,
+                                    std::uint64_t significant_digits);
+
 /** @} */
 
 }  // namespace quickstep


Mime
View raw message