01-23-2012 06:29 PM
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.
01-23-2012 07:31 PM
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
01-24-2012 12:56 AM
Try increasing the precison of the display in your control/constant and you will be able to verify what Lynn commented on.
01-24-2012 04:40 PM
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
01-24-2012 04:49 PM
Cheers
01-24-2012 05:24 PM
Floating points are rounded differently based on the value they currently hold. Look below.
01-24-2012 05:35 PM
Thanks alot both of you. How can I solve it?
01-24-2012 05:50 PM
Try subtracting half the value of the step value before comparing.
01-24-2012 05:51 PM
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
01-24-2012 06:01 PM
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