From 897762675caffdb8f21d07ff01824528b56c2e8b Mon Sep 17 00:00:00 2001 From: Michael Grupp Date: Wed, 10 Apr 2019 17:31:47 +0200 Subject: [PATCH] Use fixed precision for string histogram buckets. (#1563) `absl::StrCat/StrAppend` convert numeric values to strings with inconsistent decimal precision, which can lead to ugly formatting of the histogram. This can be fixed by using `StrAppendFormat` with `%f`, which uses 6 decimals (printf default and as it was when we had `std::to_string`). --- cartographer/common/histogram.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cartographer/common/histogram.cc b/cartographer/common/histogram.cc index e887208..d54a9b5 100644 --- a/cartographer/common/histogram.cc +++ b/cartographer/common/histogram.cc @@ -21,6 +21,7 @@ #include #include "absl/strings/str_cat.h" +#include "absl/strings/str_format.h" #include "cartographer/common/port.h" #include "glog/logging.h" @@ -59,8 +60,8 @@ std::string Histogram::ToString(const int buckets) const { } } total_count += count; - absl::StrAppend(&result, "\n[", lower_bound, ", ", upper_bound, - i + 1 == buckets ? "]" : ")"); + absl::StrAppendFormat(&result, "\n[%f, %f%c", lower_bound, upper_bound, + i + 1 == buckets ? ']' : ')'); constexpr int kMaxBarChars = 20; const int bar = (count * kMaxBarChars + values_.size() / 2) / values_.size();