10-21-2017 04:48 AM
Hi everyone,
1.when I run VI for integer I have no problem but when I use decimial number and integer (for example start decimial, stop integer) problem begin.
2. when I start at 0.7 as can be seen in VI, loop stop at 1 as it is supposed to be. but very interestingly, when I start the VI at 0.6, comparision doesnt work properly. I cannot understant what kind of problem there is. I tried changing number type (single, double, extented... small changing at problem I saw but it is still continue.
thank you for your kindly help.
Solved! Go to Solution.
10-21-2017 08:16 AM
Classic issue with floating point inaccuracies. Every computer language has this issue. Most values cannot be accurately represented in a computer with a binary representation. And these inaccuracies tend to pile up. So the real lesson here is to not use Equal for floating point values. In your example, use Greater Or Equal to stop the loop.
10-21-2017 08:17 AM
When start: 0,7
step:0,1
Stop:1 there is no problem but
start: 0.6,
step:0.1,
stop:1 the loop still running at x2 =1 or bigger .when I investigate the VI "highlight execution" the "equal" comparasion still produce "false"
10-21-2017 08:29 AM
I havent heard anythink about binary representation problem at computer. Thank you for your help
10-21-2017 08:29 AM
Again, this is due to floating point inaccuracies. Have an indicator showing your current value and change the display to show 20 decimal places and you will see what I mean.
10-21-2017 09:17 AM
The basic problem is that computers use a binary number of a certain number of bits (typically 16, 32, or 64) to represent numeric values. For integers, it is simple -- numbers from 0..65535 (unsigned) or from -32768..32767 (signed) can be represented by a 16-bit number, and for ("reasonably-sized") larger number, you can use more bits (or more bytes). How do you do fractions? Well, if you can represent them as a power of 2 (i.e. 0.5, 0.25, 0.125), you could come up with a representation that could include a "binary decimal point placement". Alternatively, if you were dealing, say, with currency, and knew everything was always going to be "to the nearest penny", you could "invent" a representation that was simply 100 times larger (so $1.23 would be represented by 123) and have a "currency type" that "acts like" an integer and allows an "exact" representation.
Otherwise, you need to expand your fraction as a binary "power of 2". Just as 1/3 has no (finite) decimal representation (0.3333333...), most fractions have no finite binary representation. Floating Point specifications detail how many "binary digits" are in the fraction, which determines its precision.
So if you add 0.1 to itself 10 times, depending on the precision and on "rounding issues" that may arise, you can get exactly 1.0 (if round-up and round-down cause the "imprecisions" in the last bit to fortuitously cancel out), but there is no guarantee.
Here's how to Have Your Cake and Eat It, Too -- if you want to do a "Floating Comparison for Equality", you need to replace it with a "Floating Comparison for Very-Close-To-Equality". Instead of testing for "A == B" (which is equivalent, mathematically, for saying "A-B == 0"), test for "abs(A-B) < epsilon", where you define "epsilon", the level of precision that makes sense for your situation. If you are adding 0.1 to itself 10 times, you could make epsilon 0.001, and your comparison would stop after 10 steps. Give it a try.
Bob Schor
10-21-2017 02:43 PM
Dear Bob,
I want to apply continously sequental source voltage from keithley source meter unit and I must use integers and decimial numbers. similiar problem will I have? if yes, what is your advise?
Thank you for all.
10-21-2017 07:14 PM
Bob and CrossRulz already gave you the answer. Use greater than or equal to to define when to stop.
10-22-2017 04:44 AM
yes, they gave me the answer and I accept them as a solution but I ask other thing but it releated with the answer. I dont only make comparision in labview! Releated with the answers I ask, do integer or decimal number sweeping usage need any careful point at device programing?
10-22-2017 08:22 AM
@CETIN wrote:
Releated with the answers I ask, do integer or decimal number sweeping usage need any careful point at device programing?
Integer values (I32, U32, etc) do not require the extra care that floating point numbers (SGL, DBL, EXT, etc) do. Integer values are precisely defined. Floating Point has accuracy errors.
Another option for the sweep values is to use the Ramp Pattern VI to define an array of values to use in your sweep and then use a FOR loop autoindexing on the generated array. Then you do not have to worry about the special comparisons.