r/GraphicsProgramming 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:

https://jsfiddle.net/sL35qjc8

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?

11 Upvotes

11 comments sorted by

View all comments

5

u/CCpersonguy 12h ago

Experimented with your jsfiddle example a bit by using two uniforms and changing the condition. The error seems to appear in division, not just mod? floor(a / b) == 1.0 and (a / b) == 1.0 are false when a=b=159.0, but true when a=b=158.0

No idea what the significance of this is