LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Return a wrong compare result

Solved!
Go to solution

THANG NGUYEN wrote:

I really have trouble with this, cause sin(pi) is not 0. It will affect to many calculation results.


No, it will not unless you do it wrong. In LabVIEW, sin(pi)=1.22460635382237730000E-16, which is zero for all practical purpose.

 

A difference in the 15th decimal digit is a few orders of magnitude less than the diamater of a human hair compared to the distance to the moon., it will NOT affect your results in any practical way. It is one of the realities of floating point math. Every good programmer is aware of this limitation. Even if you would use 10000 bits for your representation, you're just pushing the error deeper, without ever eliminating it. Pi has an infinite number of decimal (or binary) digits.

 

Floating points numbers are always quantized to the last bit in the binary mantissa and they often don't correspond to exact digits in decimal representation.

For example it is not possible to represent 0.1 exactly in DBL. The closest binary value is 0.100000000000000006, so that's wat the computer is forced to use.

 

If you are not happy with this you can write your own infinite precision arithmetic or even implement symbolic math. 😄

Message 11 of 21
(2,012 Views)
Well, it can be an issue depending on the sizes of your calculations.  If you multiply that small error by a large number, it can very well become a significant error in a final calculation.  I doubt that most of us are working in that realm (this user doesn't appear to be from his code snippet).
0 Kudos
Message 12 of 21
(2,007 Views)

If it is important for you, maybe you can use a formula and create a "switch"  like if(x=pi) then.....else......=sin(z). It it is onlya  tip. Have no idea if it will work ;). And testing is up to you



Besides which, my opinion is that Express VIs Carthage must be destroyed deleted
(Sorry no Labview "brag list" so far)
0 Kudos
Message 13 of 21
(1,993 Views)
Entering if (x=pi) is still trying to use the Equal operator on floats, which is the fundamental problem. Changing the language won't fix the problem.
0 Kudos
Message 14 of 21
(1,986 Views)
I wrote my own VI to compare it for this special case. The minimum value is not affect to the calculation but it affect to the compare result if I use the compare equal VI.
0 Kudos
Message 15 of 21
(1,980 Views)
Use 'In Range and Coerce' VI in the comparison palette. Set a reasonable range (6 digits?) and compare...there is a true output if you are within range, so basically it is an 'equals?' with a small amount of leeway.
Rob K
Measurements Mechanical Engineer (C-Series, USB X-Series)
National Instruments
CompactRIO Developers Guide
CompactRIO Out of the Box Video
0 Kudos
Message 16 of 21
(1,945 Views)
Can you please tell more specific about it? How can I set the range digit to compare?
0 Kudos
Message 17 of 21
(1,935 Views)

Sure, take a look at this, it is mostly self explanatory, set your range limits to what you want, in this example this would give you an accuracy of 3.141592xxx, so 6 digits of precision.  Add more accuracy if you wish by extending the digits of pi, but make sure the last digit you enter is both above and below the actual value...ie the 6th and 7th digit in pi is 26, so you need a lower bound ending in 2, 1, or zero in the 6th digit, and for the upper bound you need a value of 3 or higher in the 6th digit.

 

As a final note, your bounds are included in the comparison(it treats them as a 'greater than or equal to' or 'less than or equal to')

 

8.6 VI is attached as well.

 

Message Edited by Rob_K on 12-22-2008 01:25 PM
Rob K
Measurements Mechanical Engineer (C-Series, USB X-Series)
National Instruments
CompactRIO Developers Guide
CompactRIO Out of the Box Video
Download All
0 Kudos
Message 18 of 21
(1,921 Views)
In Rob's example it is a less than, not a less than or equal to.  The default behavior is to include the lower limit, but exculde the upper limit.  If you right-click on the object, you can select which bounds should be included in the comparison.
Message Edited by Matthew Kelton on 12-22-2008 02:33 PM
0 Kudos
Message 19 of 21
(1,914 Views)

To expand on Christian's comments, this is one of the reasons why some types of applications only (or by default) utilize fixed-point math. You get to define the precision required and you don't have to worry about this type of representational error.

 

BTW: one place where this situation has bit me more than once is when comparing analysis done in LV (DBL precision math) with analysis done in Excel (SGL precision math).

 

Mike...


Certified Professional Instructor
Certified LabVIEW Architect
LabVIEW Champion

"... after all, He's not a tame lion..."

For help with grief and grieving.
Message 20 of 21
(1,892 Views)