From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Easier way to do this math

Solved!
Go to solution

if(phase > 180.0F) phase = phase-360.0F;
if(phase < -180.0F) phase = 360.0F+phase;
Phase= 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?

0 Kudos
Message 1 of 5
(3,143 Views)
Solution
Accepted by GregFreeman

To wrap the phase I would add 180, divide by 360 with Q&R, subtract 180 from the remainder.

Message 2 of 5
(3,133 Views)

Or use LabVIEW's 'wrap angle' VI.

Message 3 of 5
(3,128 Views)

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.

Message 4 of 5
(3,126 Views)

It's a mechanical system so actuators phase is limited

0 Kudos
Message 5 of 5
(3,117 Views)