From c9f83f6e7e59cd5ad16b24402a1bb410c558dc84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Mon, 10 Feb 2020 13:53:29 +0100 Subject: [PATCH] [apps/statistics] Fix bar width computation There was a float -> double conversion lack of precision, which resulted in akward results Scenario: Data V N Then display the histogram: the second bar is [1.9;2[ but 1 1 contains the 2 value 2 1 --- apps/statistics/histogram_controller.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/apps/statistics/histogram_controller.cpp b/apps/statistics/histogram_controller.cpp index cb302ce5b..b26b33c39 100644 --- a/apps/statistics/histogram_controller.cpp +++ b/apps/statistics/histogram_controller.cpp @@ -247,9 +247,14 @@ void HistogramController::initBarParameters() { assert(selectedSeriesIndex() >= 0 && m_store->sumOfOccurrences(selectedSeriesIndex()) > 0); preinitXRangeParameters(); m_store->setFirstDrawnBarAbscissa(m_store->xMin()); - float barWidth = m_store->xGridUnit(); - if (barWidth <= 0.0f) { - barWidth = 1.0f; + double barWidth = m_store->xGridUnit(); + if (barWidth <= 0.0) { + barWidth = 1.0; + } else { + // Truncate the bar width, as we convert from float to double + const double precision = 7; // TODO factorize? This is an experimental value, the same as in Expression;;Epsilon() + const double logBarWidth = std::floor(std::log10(barWidth)); + barWidth = ((int)(barWidth * std::pow(10.0, precision - logBarWidth))) * std::pow(10.0, -precision + logBarWidth); } m_store->setBarWidth(barWidth); }