int mod(int a, int b) { return a - ((a / b) * b); }
GLSL acts like the C language.
In C,C++, Java (that's the languages I have observed with this behaviour) an int divided by int ALWAYS return an integer.
4/3 = 1 (not 1.(3) ). That has to do with type casting. If both arguments are integers none gets promoted to a float or double, hence the return value is an integer aswell.
Previous statement wasn't wrong, just not exactly related. When people are talking about GLSL, it is assumed that any numbers are floats, so did I.
It is true that integer division always results in integer result rounded down. You can cast values at any time by prefixing them with desired cast type (or use function-like syntax):
float myfloat = (float)myint
float myfloat = float(myint)
float mod ( int a, int b ) { return (float)a - ( ( (float)a / (float)b ) * (float)b ); }
Last line is what you should probably use. You can omit some of the explicit typecasts, but that only would result in implicit typecast. Implicit things may not work the way you expect it, and it's side-effects could be evil, so make sure you avoid it.
float mod ( int a, int b ) { return (float)a - ( ( (float)a / (float)b ) * (float)b ); }
quote]
Added this to grid.lua and got:
ERROR: 0:23: ')' : syntax error parse error
Further playing around and found that I didn't need any of this code to run the script.
Don't know what the original intent was. Just part of the magic I guess.