Cast to int vs floor

OgreSwamp picture OgreSwamp · Jul 21, 2010 · Viewed 83.5k times · Source

Is there any difference between these:

float foo1 = (int)(bar / 3.0);
float foo2 = floor(bar / 3.0);

As I understand both cases have the same result. Is there any difference in the compiled code?

Answer

James Curran picture James Curran · Jul 21, 2010

Casting to an int will truncate toward zero. floor() will truncate toward negative infinite. This will give you different values if bar were negative.