rounding down

Started by
4 comments, last by timw 18 years, 8 months ago
I was thinking, what would be faster for taking the floor of a float float f = bla; f = (float)((int)f); or float f = bla; f = floor(f); never thought much of it, but it's such a common slow operation, I was wondering what people thought. no fixed point suggestions either lol. thanks, Tim
Advertisement
One would make the assumption that the floor() function will be as-fast or faster than the casting method (because if the casting method was the fastest - that would be what the floor function would implement).

Also, another reason to use the floor() function it is much eaiser to read what is tring to be done.

Finally - this is premature optimisation. You probably shouldn't be worrying about it.
you might not be able to convert your float to an int, even if it is a single precision

also, floor is different from truncating [conversion to an int], for negative [non-integer] numbers floor will give you a lower number, truncating will give you a more positive number.
Quote:more positive
Higher ;)
Also, on some compilers, floor() is implemented as a compiler intrinsic, which means that it will take calls to floor() and replace it with the assembly necessary to perform the rounding without the overhead of a function call.
wow guys thanks for the quick replies. I wouldn't call it premature tho. because its something we've all done millions of time lol. just wondering. I'm not concerned with negative numbers, but thanks for clearing it up.

thanks

Tim

This topic is closed to new replies.

Advertisement