From 1a967597e84e93fef7726639f02fbbd2df928420 Mon Sep 17 00:00:00 2001 From: EmmanuelMess Date: Tue, 14 Sep 2021 12:39:19 -0300 Subject: [PATCH] Fix roundings with lround() --- src/playlist/playlistdelegates.cpp | 4 +++- src/playlist/playlistfilterparser.cpp | 3 ++- src/widgets/ratingwidget.cpp | 6 ++++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/playlist/playlistdelegates.cpp b/src/playlist/playlistdelegates.cpp index 454ae3270..22bca5080 100644 --- a/src/playlist/playlistdelegates.cpp +++ b/src/playlist/playlistdelegates.cpp @@ -21,6 +21,8 @@ #include "config.h" +#include + #include #include #include @@ -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); diff --git a/src/playlist/playlistfilterparser.cpp b/src/playlist/playlistfilterparser.cpp index 224bf5f5e..38d9958b8 100644 --- a/src/playlist/playlistfilterparser.cpp +++ b/src/playlist/playlistfilterparser.cpp @@ -21,6 +21,7 @@ #include "config.h" #include +#include #include #include @@ -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(element.toDouble() * 10.0 + 0.5))); + return cmp_->Matches(QString::number(lround(element.toDouble() * 10.0))); } private: QScopedPointer cmp_; diff --git a/src/widgets/ratingwidget.cpp b/src/widgets/ratingwidget.cpp index c34e9db6b..dab99de54 100644 --- a/src/widgets/ratingwidget.cpp +++ b/src/widgets/ratingwidget.cpp @@ -20,6 +20,8 @@ #include "ratingwidget.h" +#include + #include #include #include @@ -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(lround(rating * 2.0)), kStarCount * 2); painter->drawPixmap(QRect(pos, size), stars_[star], QRect(QPoint(0, 0), size)); }