LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

comparing elements

I found something interesting in the less than comparison tool. It seems to give error in comparing (0.2 with 0.2 and 0.4 with 0.4). In the loop it is always "True" if you compare (0.2<0.2) or (0.4<0.4). But if you enter these values by hand the comparison is "False." Can someone explain why is this happening? I believe that the comparison like (0.2<0.2 or 0.4<0.4) should always be false.

0 Kudos
Message 1 of 10
(4,029 Views)

You are probably seeing the effects of trying to compare floating point numbers which appear to be the same when displayed with a small number of significant figures but which actually differ slightly.  Search for "compare floats" or similar.

 

What you should do is compare with a small tolerance to account for the finite representation in binary.

 

Lynn

0 Kudos
Message 2 of 10
(4,026 Views)

Try increasing the precison of the display in your control/constant and you will be able to verify what Lynn commented on.

0 Kudos
Message 3 of 10
(4,010 Views)

Hi Lynn and Amit

 

Thanks you both for the response. I have attached a labview test code file written in LV8.5. As you will see, this program should print the range from 2 at the top to 0.1 at the bottom with the step of 0.1. But it stops at 0.2 as the while loop stopping condition i.e (0.2 < 0.2) becomes true and terminate the loop there. But in contrary If you use the upper limit as 1, it will continue and reach 0.1 as the lower limit.

 

 

Regards,

 

Mansoor

0 Kudos
Message 4 of 10
(3,994 Views)

Problem.png

 

Cheers

Message 5 of 10
(3,989 Views)

Floating points are rounded differently based on the value they currently hold. Look below.

 

 

range 2.png

 

 

Range 1.png

 

0 Kudos
Message 6 of 10
(3,981 Views)

Thanks alot both of you. How can I solve it?

0 Kudos
Message 7 of 10
(3,977 Views)

Try subtracting half the value of the step value before comparing.

0 Kudos
Message 8 of 10
(3,973 Views)

Generate your ramp using the "ramp by delta" instance of Ramp Pattern.vi on the Signal Generation Palatte. 

 

The BD is unlocked and may give you some ideas about comparing DBLs


"Should be" isn't "Is" -Jay
0 Kudos
Message 9 of 10
(3,972 Views)

In recent versions of LV the Numeric palette has a constant called "Machine Epsilon." This is the smallest value which can be resolved for a specific representation of floating point numbers on that computer. If you adjust the limit by that amount, you should catch the roundoff errors.  Of course in your case you subtract the step multiple times, so you could need a multiple of epsilon because the error accumulates.

 

Lynn

Message 10 of 10
(3,968 Views)