Modulus or something


To cycle a value between -1 to +1, for an saw lfo or something you could go:

X += 0.01;
if (X > 1) {X -= 2;}

I suppose you could use modulus or something to avoid the if, but can you do that with floats? Would:

X += 0.01;
X = X % 2; 
X -= 1;

render the same result?

And what about something like this:

X += 0.01;
X = X - ((X & 1) * 2);

The and expression should only return true if X is equal to or larger than 1.

And which is most efficient?


Why don’t you test it? Get the time, cycle through that code 10000000 times, and then get the time again. Subtract the two times to get the time elapsed and then compare the results.

(btw, I don’t think you can modulus floats in C++, but I could be wrong.)


modulo floats sure don’t sound right to me. float division has no remainder.


you can use fmod() to get the remainder from a float division


I think the first one is the best. Because we can do that without a branch by using conditional move statement. (Probably compiler do that)

If there are 4 LFOs, we can use vector statements.

[quote]x = vec_add(x, phase);
x = vec_sub(x, vec_and(all_two, vec_cmpge(x, all_one)));[/quote]
This is for Altivec, we can do the same thing by SSE.

We should consider not to use real function calls and branches. :wink:



Ok! Thanks you.