LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

too many nested if?

Hi all,

 

Please take a look at the code and tell me does the code contain too many nested if?  It is not done yet, but it is supposed to be a subvi that convert one type of floating point format to another type.  There are many exceptions that I need to check.  Also, I am converting from c to labview, so in c the program has a lot of if, elseif, else, etc in it.

 

Should I make it a state machine instead?  However, it seems slower to do a state machine.  Please give me some sugestion. 

 

Also, how do I make all the condition more obvious?  Since all the case structures are nested, it is hard to see the big picture unlike text base. 

 

Yik

------------------------------------------------------------------

Kudos and Accepted as Solution are welcome!
0 Kudos
Message 1 of 7
(3,461 Views)
Given your description I would be inclined to create a state machine. It would be easier to understand and it can be documented quite well with a state transition diagram. I doubt you will experience much, if any, hit in performance.


Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 2 of 7
(3,456 Views)

Could you give a link describing the TI FP specs?

 

You only need to deal with the "exceptions". You can build an array of the various booleans and convert it to a number. Now use a case structure and wire the number to it. Set the radix of the case display to binary and add as many unique cases as you need. Use the default case for the bulk of the work.

Message 3 of 7
(3,429 Views)

If this is a piece of code that is going into a tight loop to be executed many times I would consider sticking to nested case statements.  Keep things tidy and it is not difficult to follow.  What I would do, however, is bag all of those comparisons outside the case statements and just wire the value directly.  =0? becomes a case structure with two cases 0,Default.  Same for other cases.  Be sure to show the radix for the case display and it is neatly documented as well.  When I am porting C code, I typically use the code I am replicating as a free label, one case may get if (x==0) another gets else and so on.

 

If the code will be used at a leisurely pace, you won't notice the performance hit that you will take by going to a state machine.  It would be easier to add cases later, but this sounds like a well-formed problem so that may not be a consideration.

 

At any rate, if your code starts to look like this, then please go to a state machine else I would go with whatever feels intuitive to you.

Message 4 of 7
(3,424 Views)

Hi all,

 

I have attached the c code that I am trying to convert (starting on page 71).  I ended up using the state machine.  I created a case for each condition, and inside these cases, I would do my condition check.  I ended up having to use a case structure inside each case.  Not sure is that the best way or not.  I should be able to post some code up for review tomorrow.

 

Yik

 

------------------------------------------------------------------

Kudos and Accepted as Solution are welcome!
0 Kudos
Message 5 of 7
(3,408 Views)

Attached is my state machine.  Please take a look to see can I simplify it or make it better.

 

Yik

------------------------------------------------------------------

Kudos and Accepted as Solution are welcome!
0 Kudos
Message 6 of 7
(3,354 Views)

Looking at your picture one extra time, it looks like you're checking if a Single is a special case of sorts (as +/- inf), if that's the case you can simply do a comparison with the the Inf-constant (and the others) from the numeric pallette.

 

/Y

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 7 of 7
(3,335 Views)