• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML floor vs div

xDGameStudios

GameMaker Staff
GameMaker Dev.
Hi everyone I have a question, even though floor and div return the same result...

x div y == floor(x / y);

Is there any good reason to use one instead of another and performance wise what would be the best option?!
 

slojanko

Member
There is a slight difference in favor of div: 100.000.000 operations took roughly 25.1s for divide&floor and 24.5s for div. I don't have YYC at the moment but you might see different results on that.
 
T

TimothyAllen

Guest
do they return the same result on a negative? I would assume these results:

-3 div 2 = -1
floor(-3 / 2) = -2

But I could be wrong.
 

meseta

Member
floor rounds towards negative infinity; div rounds towards zero.

You'd want to use floor for things that need to behave consistently across zero (like tile movement, and rounding position to the nearest multiple of the tile - if you used div for this, you'd find weird behaviour as everything just left of zero rounds up to zero instead of down to -1)

You'd want to use div for things like rounding speed/acceleration, which need to be symmetrical about zero; if you used floor for this, you'd find motion biased to the negative side.
 

xDGameStudios

GameMaker Staff
GameMaker Dev.
Well I'm using it to draw gui stuff... so coordinates will always be positive... I guess I'll be okay using div then.
 
Top