Skip to content

Commit

Permalink
cabana/chart: fix value overflow (#29924)
Browse files Browse the repository at this point in the history
fix value overflow
  • Loading branch information
deanlee authored Sep 15, 2023
1 parent 05db624 commit 6a72d53
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions tools/cabana/chart/chart.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <QRandomGenerator>
#include <QRubberBand>
#include <QScreen>
#include <QtMath>
#include <QWindow>

#include "tools/cabana/chart/chartswidget.h"
Expand Down Expand Up @@ -365,7 +364,7 @@ void ChartView::updateAxisY() {
axis_y->setRange(min_y, max_y);
axis_y->setTickCount(tick_count);

int n = qMax(int(-qFloor(std::log10((max_y - min_y) / (tick_count - 1)))), 0) + 1;
int n = std::max(int(-std::floor(std::log10((max_y - min_y) / (tick_count - 1)))), 0) + 1;
int max_label_width = 0;
QFontMetrics fm(axis_y->labelsFont());
for (int i = 0; i < tick_count; i++) {
Expand All @@ -383,15 +382,15 @@ void ChartView::updateAxisY() {
std::tuple<double, double, int> ChartView::getNiceAxisNumbers(qreal min, qreal max, int tick_count) {
qreal range = niceNumber((max - min), true); // range with ceiling
qreal step = niceNumber(range / (tick_count - 1), false);
min = qFloor(min / step);
max = qCeil(max / step);
min = std::floor(min / step);
max = std::ceil(max / step);
tick_count = int(max - min) + 1;
return {min * step, max * step, tick_count};
}

// nice numbers can be expressed as form of 1*10^n, 2* 10^n or 5*10^n
qreal ChartView::niceNumber(qreal x, bool ceiling) {
qreal z = qPow(10, qFloor(std::log10(x))); //find corresponding number of the form of 10^n than is smaller than x
qreal z = std::pow(10, std::floor(std::log10(x))); //find corresponding number of the form of 10^n than is smaller than x
qreal q = x / z; //q<10 && q>=1;
if (ceiling) {
if (q <= 1.0) q = 1;
Expand Down

0 comments on commit 6a72d53

Please sign in to comment.