キャンセル
次の結果を表示 
次の代わりに検索 
もしかして: 

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 件の賞賛
メッセージ1/10
4,711件の閲覧回数

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 件の賞賛
メッセージ2/10
4,708件の閲覧回数

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

0 件の賞賛
メッセージ3/10
4,692件の閲覧回数

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 件の賞賛
メッセージ4/10
4,676件の閲覧回数

Problem.png

 

Cheers

メッセージ5/10
4,671件の閲覧回数

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

 

 

range 2.png

 

 

Range 1.png

 

0 件の賞賛
メッセージ6/10
4,663件の閲覧回数

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

0 件の賞賛
メッセージ7/10
4,659件の閲覧回数

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

0 件の賞賛
メッセージ8/10
4,655件の閲覧回数

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 件の賞賛
メッセージ9/10
4,654件の閲覧回数

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

メッセージ10/10
4,650件の閲覧回数