Actually, that, as it is written will not work at all.
Try it! You will see some odd results!!
I have tried it and, on the MinGW port of gcc 3.4.5 with Windows XP SP2, there were no odd results as you claimed. Kindly provide your test program, input, output and expected output.
My own test was:
Code:
#include <stdio.h>
#include <assert.h>
int round_nearest(float f);
int main(void)
{
assert(round_nearest(0.0f) == 0);
assert(round_nearest(0.001f) == 0);
assert(round_nearest(0.499f) == 0);
assert(round_nearest(0.5f) == 1);
assert(round_nearest(0.501f) == 1);
assert(round_nearest(0.999f) == 1);
assert(round_nearest(1.0f) == 1);
assert(round_nearest(1.001f) == 1);
assert(round_nearest(1.499f) == 1);
assert(round_nearest(1.5f) == 2);
assert(round_nearest(1.501f) == 2);
assert(round_nearest(1.999f) == 2);
assert(round_nearest(2.0f) == 2);
assert(round_nearest(2.001f) == 2);
return 0;
}
int round_nearest(float f)
{
return (int)(f + 0.5f);
}