
Originally Posted by
SkripT
Hi all, this time i have a little problem with qRound. Based on the tutorial:
So, I think that the result of, for example, 256*2.22 (=568.32) should be 569 but the result that qRound gives me is 568. In my point of view this is not rounded up to the next integer. Anybody could explain me why? Thanks
I ran into similar problems and discovered, after searching the topic in Google, that there is an entire sub-branch of C++ devoted to rounding algorithms. Someone offered this:
round(x) = floor( x + 0.5f);
round(x) = floor( x + 0.5f);
To copy to clipboard, switch view to plain text mode
I borrowed this method from one of the many offerings:
float homestead::Round(float Value, int NumPlaces) {
int k;
float Factor;
float Temp;
Factor = 1.0;
for (k = 0; k < NumPlaces; k++)
Factor = Factor * 10.0;
Temp = Value * Factor + 0.5;
return Temp / Factor;
}
float homestead::Round(float Value, int NumPlaces) {
int k;
float Factor;
float Temp;
Factor = 1.0;
for (k = 0; k < NumPlaces; k++)
Factor = Factor * 10.0;
Temp = Value * Factor + 0.5;
return Temp / Factor;
}
To copy to clipboard, switch view to plain text mode
because it allows me to specifiy the number of roundoff places.
Bookmarks