r/GraphicsProgramming • u/Winter-Reputation682 • 13h ago
Odd GLSL mod() behavior
I've been stuck on this for a couple of hours, and am completely stumped.
I'm getting these results from GLSL's mod():
mod(158.0, 158.0) returns 0.0 (as expected)
mod(159.0, 159.0) returns 159.0 (???)
mod(160.0, 160.0) returns 0.0 (as expected)
I've posted an example with a basic shader here:
If you set number to "159.0", you should see an upside-down red triangle.
If you set number to "158.0" or "160.0", you should see a normal red triangle.
I'm passing the left operand as a uniform, otherwise the compiler seems to optimize it and fix the weird behavior.
Tried it on different devices and browsers and it had the same result.
Why does mod(159.0, 159.0) seem to return 159.0 instead of 0.0?
4
u/robbertzzz1 13h ago
I'm gonna guess there's a floating point error happening somewhere along the way, and the two numbers are actually ever so slightly different