Thursday, April 23, 2009

Programming: when (int)x is not lower than x

Spend few hours to learn this simple thing.
Casting float to integer might be a good option to simulate floor function. It costs less even though not that significant. But if you think (int)x <= x, think again...

For x <= 0, (int)x >= x, eg: (int)-1.5 = -1

very simple but annoying if you don't figure it out early. Of course (int) seems to be a better 'escape' than floor(), but you should notice its behavior. 

No comments: