LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Specific problem with incrementing in a while loop

Solved!
Go to solution

Hi,

 

I'm pretty new to labview, I'd be grateful if someone could help me out on this one.

 

I'm trying to implement a voltage sweep, by providing a Start Point, Stop point and Step size. I've implemented it using a while loop, where a shift register adds on a step size to the start point and when the value equals the value of the Stop point the loop breaks.

 

The attached VI seems to work with whole numbers e.g. Start 1, Stop 10 and Step Size of 1.

 

However when I use a Start value of 0, Step of 0.1 and Stop of 1, the loop never breaks. I've tried editing the format & precision but still can't get it to work. Could someone please explain why this is happening.

 

What I've also noticed is that if the equal to sign is changed to a greater than or equal to then the loop stops at 1.1 instead of 1. If I specify the followling START of 0, an Increment of 0.1 and a STOP of 0, the loop does stop at 2.

 

I've attached the VI (IncrementStepSizeWhileLoop.vi).

 

Many thanks.

 

ranveerm

0 Kudos
Message 1 of 12
(3,644 Views)

I found out quick that comparing reals is dangerous. Multiply, convert to an integer, and then compare. I recently wrote a polymorphic vi that does this since I seemed to be repeating the same code over and over again.

 

When I first started using LabVIEW a little over a year ago, one of my frustrations was with how difficult LabVIEW makes it to do start/stop/step. That is so fundamental to test and measurement. In the lab, a person is constantly sweeping some parameter.

0 Kudos
Message 2 of 12
(3,638 Views)

This is related to the representation of floating point numbers. See here for more details. Attached modified example is one way to avoid this problem.

Message 3 of 12
(3,630 Views)

The issue you are seeing is not LV but the nature of finite digital computers and the way they represent numbers.

 

In the same way we can not represent the fraction 1/3 in decimal in a fintie form, the number "0.1" cannot be represented in binary. What the computer uses is a value very close to "0.1". You could see this if you put indciators on the wires with the precision set to 7 decimal places or more.

 

In the thread (where Christian write that "1 can not be represented in binary, [No Christian, I just can't stop laughing over that typo]) you can read more on this topic.

 

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 4 of 12
(3,626 Views)
Solution
Accepted by topic author ranveerm

You can calculate the number of iterations needed from the parameters, so a FOR loop is more appropriate and you don't even need a comparison operation.

 

Message Edited by altenbach on 09-12-2008 08:49 AM
Message 5 of 12
(3,597 Views)

Great stuff!

 

Thank you all very much for your help. 

 

ranveerm

0 Kudos
Message 6 of 12
(3,562 Views)

how could i modify this so that my loop counted from 0 to -100 in 0.5 steps then when reaching -100, it counted back down to 0 in 0.5 steps.  This loop would continue until the loop stop was pressed?

Best Regards,

0 Kudos
Message 7 of 12
(3,105 Views)

Hi, i solved my own question with this code:

Untitled.jpg

0 Kudos
Message 8 of 12
(3,094 Views)

Not good.  You have a race condition by using the global variable for Boolean.  The behavior of the code will change depending on whether the Boolean global going into the Select function is read before or after the boolean global is written to in one of the case structures.

 

Also, doing an equals comparison with zero is dangerous when using floating point values.  By the time you getting done adding or subtracting .5, you may not wind up exactly at 0 again.

 

If you run into problems, donate a nickel to Smercuio's retirement fund.

0 Kudos
Message 9 of 12
(3,085 Views)

I added the flat sequence in order for the global to be written to prior to being read by the case selector.  Could this not work.  Basically, i am toggling a switch when it reaches -100 or 0 in order to change the increment between 0.5 and -0.5.  It seems to work ok.  Do you have an alternative solution?  I take your advise on the floating point.  What is the alternative to using floating point?

Best Regards,

0 Kudos
Message 10 of 12
(3,061 Views)