LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

problem at value comparision

Solved!
Go to solution

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.

0 Kudos
Message 1 of 11
(4,038 Views)
Solution
Accepted by CETIN

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.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 2 of 11
(4,003 Views)

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"                                            

 

0 Kudos
Message 3 of 11
(4,002 Views)

 I havent heard anythink about binary representation problem at computer. Thank you for your help

0 Kudos
Message 4 of 11
(4,000 Views)

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.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 5 of 11
(3,999 Views)

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

0 Kudos
Message 6 of 11
(3,993 Views)

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.

0 Kudos
Message 7 of 11
(3,971 Views)

Bob and CrossRulz already gave you the answer.  Use greater than or equal to to define when to stop.

0 Kudos
Message 8 of 11
(3,954 Views)

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?

0 Kudos
Message 9 of 11
(3,927 Views)

@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.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 10 of 11
(3,916 Views)