09-12-2008 07:12 AM
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
Solved! Go to Solution.
09-12-2008 07:31 AM
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.
09-12-2008 07:44 AM
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.
09-12-2008 07:47 AM
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
09-12-2008 10:49 AM - edited 09-12-2008 10:49 AM
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.
09-14-2008 07:35 PM
Great stuff!
Thank you all very much for your help.
ranveerm
03-22-2012 07:19 AM
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,
03-22-2012 09:20 AM
Hi, i solved my own question with this code:
03-22-2012 10:48 AM
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.
03-23-2012 03:38 AM
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,