- Community Home
- :
- Discussion Forums
- :
- Most Active Software Boards
- :
- LabVIEW
- :
- Easier way to do this math

Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

Highlighted

12-11-2013 04:54 PM - edited 12-11-2013 04:54 PM

Options

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report to a Moderator

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?

Solved! Go to Solution.

Solution

Accepted by topic author GregFreeman

08-27-2015 04:09 PM

12-11-2013 05:05 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report to a Moderator

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

12-11-2013 05:11 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report to a Moderator

Or use LabVIEW's 'wrap angle' VI.

12-11-2013 05:13 PM - edited 12-11-2013 05:13 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report to a Moderator

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.

12-11-2013 05:17 PM - edited 12-11-2013 05:17 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report to a Moderator

It's a mechanical system so actuators phase is limited