Round a float to a regular grid of predefined points
The standard ceil()
, floor()
functions don't have a precision, I guess could work around that by adding your own precision - but this may introduce errors - e.g.
double ceil(double v, int p)
{
v *= pow(10, p);
v = ceil(v);
v /= pow(10, p);
}
I guess you could test to see if this is reliable for you?
As long as your grid is regular, just find a transformation from integers to this grid. So let's say your grid is
0.2 0.4 0.6 ...
Then you round by
float round(float f)
{
return floor(f * 5 + 0.5) / 5;
// return std::round(f * 5) / 5; // C++11
}