Fix roundings with lround()

This commit is contained in:
EmmanuelMess
2021-09-14 12:39:19 -03:00
committed by Jonas Kvinge
parent d5b0794b00
commit 1a967597e8
3 changed files with 9 additions and 4 deletions

View File

@@ -21,6 +21,8 @@
#include "config.h"
#include <cmath>
#include <QtGlobal>
#include <QApplication>
#include <QObject>
@@ -539,7 +541,7 @@ QString RatingItemDelegate::displayText(const QVariant &value, const QLocale&) c
if (value.isNull() || value.toDouble() <= 0) return QString();
// Round to the nearest 0.5
const double rating = double(int(value.toDouble() * RatingPainter::kStarCount * 2 + 0.5)) / 2;
const double rating = double(lround(value.toDouble() * RatingPainter::kStarCount * 2)) / 2;
return QString::number(rating, 'f', 1);

View File

@@ -21,6 +21,7 @@
#include "config.h"
#include <algorithm>
#include <cmath>
#include <QList>
#include <QMap>
@@ -180,7 +181,7 @@ class RatingComparatorDecorator : public SearchTermComparator {
public:
explicit RatingComparatorDecorator(SearchTermComparator *cmp) : cmp_(cmp) {}
bool Matches(const QString &element) const override {
return cmp_->Matches(QString::number(static_cast<int>(element.toDouble() * 10.0 + 0.5)));
return cmp_->Matches(QString::number(lround(element.toDouble() * 10.0)));
}
private:
QScopedPointer<SearchTermComparator> cmp_;

View File

@@ -20,6 +20,8 @@
#include "ratingwidget.h"
#include <cmath>
#include <QStyleOptionFrame>
#include <QStylePainter>
#include <QIcon>
@@ -91,7 +93,7 @@ double RatingPainter::RatingForPos(const QPoint pos, const QRect rect) {
if (raw > 1) return 1;
// Round to the nearest 0.1
return double(int(raw * kStarCount * 2 + 0.5)) / (kStarCount * 2);
return double(lround(raw * kStarCount * 2)) / (kStarCount * 2);
}
@@ -103,7 +105,7 @@ void RatingPainter::Paint(QPainter *painter, const QRect rect, double rating) co
rating *= kStarCount;
// Draw the stars
const int star = qBound(0, int(rating * 2.0 + 0.5), kStarCount * 2);
const int star = qBound(0, static_cast<int>(lround(rating * 2.0)), kStarCount * 2);
painter->drawPixmap(QRect(pos, size), stars_[star], QRect(QPoint(0, 0), size));
}