Search code examples
iosobjective-ccif-statementglsles

Float modulo operator that includes the upper value


I need an efficient glsl code that doesn't have if-else statement. Currently the algorithm is like this :

lowp float a;
if (a > 1.0) 
   a -= 1.0;
else if (a < 0.0) 
   a += 1.0;

It's similar with a floating point modulo operator :

a = mod(a,1.0);

only if a == 1, it should return 1, instead of 0.

How to do this without if-else though?

Thanks


Solution

  • Here is a mathematic representation that might do what you need - without using branching statements

    For inputs between [-1, 2.0]

    a = a + 1 - max(trunc(a), ceil(a))
    
    Input   Output
    0.3     0.3
    -0.3    0.7
    0.7     0.7
    1.3     0.3
    1.7     0.7
    1       1.0