12-19-2008 12:10 PM
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. 😄
12-19-2008 12:26 PM
12-19-2008 01:12 PM
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
12-19-2008 01:38 PM
12-19-2008 01:44 PM
12-22-2008 12:17 PM
12-22-2008 12:45 PM
12-22-2008 01:24 PM - edited 12-22-2008 01:25 PM
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.
12-22-2008 01:32 PM - edited 12-22-2008 01:33 PM
12-22-2008 03:38 PM
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...