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`).
master
Michael Grupp 2019-04-10 17:31:47 +02:00 committed by Andre Gaschler
parent c999c3012b
commit 897762675c
1 changed files with 3 additions and 2 deletions

View File

@ -21,6 +21,7 @@
#include <string>
#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();