LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Why does -0.8 not equal -0.8 ?????

I have a while loop, with start, stop, & step digital controls, if Start = -0.2, stop -0.8 and step = 0.05, when the prog gets to increment = stop = -0.8, it produces a false, when it is showing that they are equal! See prog attached.
0 Kudos
Message 1 of 4
(2,806 Views)
Because they're not equal. It has to do with binary representation of floating point numbers. Go to LabVIEW FAQ for more details and techniques on how number comparison can be correctly performed.
0 Kudos
Message 2 of 4
(2,806 Views)
Hello,

The reason this is happening is because you are using floating point numbers. Floating point numbers are not always exactly represented. To prove this to yourself you can do two things. First, put an indicator at the output of the Subtract function. Then, set the "digits of precision" of all your controls and indicators to something large - say 20. What you will notice is that -0.8 is really something like -0.800000000000000044000000.

The lesson from all this is to either use integers when comparing to stop a loop, or use >= and =< instead of just =.

Thanks,
Doug Norman
0 Kudos
Message 3 of 4
(2,806 Views)
If you subtract the two values you're comparing, add an indicator to the subtract output, and change the format of the indicator (by right-clicking on it) to display 20 decimal points, you'll see that the numbers aren't what you expect.
The attached VI shows one way to implement your loop.
0 Kudos
Message 4 of 4
(2,806 Views)