if(phase > 180.0F) phase = phase-360.0F;
if(phase < -180.0F) phase = 360.0F+phase;
I keep ending up with a nested case structure, or serialized case structures, or serialized selects when trying to do the above math. Is there any way I can calculate phase in a more concise statement?
Solved! Go to Solution.
To wrap the phase I would add 180, divide by 360 with Q&R, subtract 180 from the remainder.
If you know that the input phase is going to be within some reasonable range like (-10 000, +10 000), then just do
A = value + 180 + N*360
B = A modulo 360
output = B - 180
with N large enough that it encompasses the bottom end of the range but not so large that it plus the top of the range will risk overflow.
If the range is too big for that or you really know nothing about the range (in which case even your nested cases wouldn't work), then you can do
A = value + 180
B = A modulo 360 (B is in -359.9999...359.9999)
C = B + 360 (C is in +eps...719.99999)
E = D modulo 360 (D value is in 0....359.999999)
F = D - 180
Taking a second modulo may be faster than adding in a comparison because then there's no branching, and branching is expensive, and you'll need two of them, but only one extra modulo.
... or, umm, the wrap angle vi. Yeah, that would work.