From 1ecdcfc29cdba5e1d7e0190a4651be60ff5885f0 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Wed, 28 Oct 2015 20:15:25 +0300 Subject: [PATCH] Fix PieceAvailabilityBar::intToFloatVector(). Closes #3937. --- src/gui/properties/pieceavailabilitybar.cpp | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/gui/properties/pieceavailabilitybar.cpp b/src/gui/properties/pieceavailabilitybar.cpp index ac746f168..d035b3339 100644 --- a/src/gui/properties/pieceavailabilitybar.cpp +++ b/src/gui/properties/pieceavailabilitybar.cpp @@ -70,23 +70,17 @@ std::vector PieceAvailabilityBar::intToFloatVector(const std::vector for (int x = 0; x < reqSize; ++x) { - // don't use previously calculated value "ratio" here!!! - // float cannot save irrational number like 7/9, if this number will be rounded up by std::ceil - // give you x2 == pieces.size(), and index out of range: pieces[x2] - // this code is safe, so keep that in mind when you try optimize more. - // tested with size = 3000000ul - // R - real - const float fromR = (x * vecin.size()) / (float)reqSize; - const float toR = ((x + 1) * vecin.size()) / (float)reqSize; + const float fromR = x * ratio; + const float toR = (x + 1) * ratio; // C - integer int fromC = fromR;// std::floor not needed int toC = std::ceil(toR); + if (toC > vecin.size()) + --toC; // position in pieces table - // libtorrent::bitfield::m_size is unsigned int(31 bits), so qlonglong is not needed - // tested with size = 3000000ul int x2 = fromC; // little speed up for really big pieces table, 10K+ size @@ -98,7 +92,7 @@ std::vector PieceAvailabilityBar::intToFloatVector(const std::vector // case when calculated range is (15.2 >= x < 15.7) if (x2 == toCMinusOne) { if (vecin[x2]) { - value += (toR - fromR) * vecin[x2]; + value += ratio * vecin[x2]; } ++x2; }