In addition to Math.PI not being able to store the exact value of pi,
the computation of trig functions involves approximations. For example,
one definition of a cosine is in terms of an infinite series, and a
computer can't calculate an infinite number of terms. Even if the
argument passed to Math.cos() is exact, the result generally won't be.
 
- Gordon

________________________________

From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of Jon Bradley
Sent: Sunday, September 16, 2007 2:49 PM
To: [email protected]
Subject: Re: [flexcoders] Math.cos...?



That's pretty much it. 

To a computer Math.cos(Math.PI/2) is not 0. It's really close to 0,
because PI is an infinite sequence and a computer can "only" store it as
a double precision floating point number (ie, a fixed value).

What you get back from this calculation is the error bound of the
computer basically, which you can then use for  numerical calculations,
ie, MathLib.ERROR_BOUND = Math.cos(Math.PI/2). Then you can feasibly use
if you need numerical accuracy.

IE, if result == MathLib.ERROR_BOUND, result = 0.

Numerical accuracy in AS2 is not equivalent to that of AS3. I ran into
this while porting the Mersenne Twister algorithm to AS2 - I couldn't
even store 2^32 as a hex value in AS2 (0x100000000, which equals
4294967296).

At least we can be somewhat numerically accurate now...

good luck,

jon


On Sep 16, 2007, at 5:15 PM, Troy Gilbert wrote:


        > Why does Math.cos(Math.PI/2) not return zero?
        
        Round-off error in the Math libs? It does return a value very
close to
        0 (1.7xe-17).
        
        Troy.


 

Reply via email to