...did someone remove Fadey's post? Weird.
Anyway, I wouldn't necessarily call +0.5 evil, but I don't know the exact situation of your fps counter. Perhaps give us the particular numbers that are causing a value <= 60.0 to be rounded to 61? Of course, floating-point math is annoying as hell once you get into the details of it. There is one particular case (for doubles) where +0.5 does what most would consider is incorrect behavior:
1 2 3 4 5 6 7 8 9
|
// Example program
#include <iostream>
#include <cmath>
int main()
{
std::cout << static_cast<int>(0.49999999999999994 + 0.5) << "\n";
std::cout << std::round(0.49999999999999994) << "\n";
}
| |
0.49999999999999994 + 0.5 turns out to be
exactly 1.0 in IEEE 754 double-precision binary floating-point format. std::round accounts for this. Of course, if it
really matters whether or not 0.49999... gets rounded to 0.0 or 1.0, I would consider a re-design of what you're doing. In most applications, this would just be considered noise, and it wouldn't matter.
Slightly related: If we're trying to round, for example, 0.55 to either 0.5 or 0.6 (rounding something to a certain number of significant digits, but not necessarily to an integer, and ignoring floating-point problems), +0.5 should not be done, rather you should look at the digit before the digit you're trying to round off, and round down (towards zero) if the digit is even, or round up (away from zero) if the digit is odd (or vice-versa, it's arbitrary).
For example,
0.65 would get rounded towards zero to 0.6 since 6 is even.
0.75 would get rounded away from zero to 0.8 since 7 is odd.
-0.75 would get rounded away from zero to -0.8 since 7 is odd.
The purpose of this is to keep the statistical bias as close to possible to 0, assuming the random numbers you get are uniform enough. This would be applicable in an engineering/drafting scenario.
Of course, this gets complicated for actual floating-point numbers, since the number you're trying to round to probably isn't exactly representable in floating-point math, and floating-point numbers are not uniformly distributed...