Convert if statement to lerp

I know that I can convert if (x<3) to Lerp(x, 3, t)

but how do I convert if(x>3 && x<4) ?

Except those aren’t the same.

The usual example people give to convert between if statements and a lerp is using a step() function, like this:

float foo = a;
if (x < 3)
  foo = b;
float foo = lerp(a, b, step(x, 3))

Except there’s a huge problem with this, the step() function compiles to an if statement. In the worst case the lerp() example compiles into slower code, and in the best case it compiles into nearly identical code as the original if statement does. So there’s literally no point. I say nearly identical because step() compiles to (3 >= x) ? 1 : 0.

If you want to simplify things down to a single line, you can use a ternary statement.

float foo = (x < 3) ? b : a;
1 Like

that makes sense, thank you