Here is what the Ecmascript spec (http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf <http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf> ) has to say about methods like Math.cos():
The behaviour of the functions acos, asin, atan, atan2, cos, exp, log, pow, sin, and sqrt is not precisely specified here except to require specific results for certain argument values that represent boundary cases of interest. For other argument values, these functions are intended to compute approximations to the results of familiar mathematical functions, but some latitude is allowed in the choice of approximation algorithms. The general intent is that an implementer should be able to use the same mathematical library for ECMAScript on a given hardware platform that is available to C programmers on that platform. Although the choice of algorithms is left to the implementation, it is recommended (but not specified by this standard) that implementations use the approximation algorithms for IEEE 754 arithmetic contained in fdlibm, the freely distributable mathematical library from Sun Microsystems (fdlibmcomment@ <mailto:[EMAIL PROTECTED]> sunpro.eng.sun.com). This specification also requires specific results for certain argument values that represent boundary cases of interest. Note the phrases "intended to compute approximations" and "approximation algorithms". The "certain arguments values that represent boundary cases of interest" are specified as follows: cos (x) Returns an implementation-dependent approximation to the cosine of x. The argument is expressed in radians. • If x is NaN, the result is NaN. • If x is +0, the result is 1. • If x is −0, the result is 1. • If x is +∞, the result is NaN. • If x is −∞, the result is NaN. - Gordon ________________________________ From: Gordon Smith Sent: Monday, September 17, 2007 2:49 PM To: '[email protected]' Subject: RE: [flexcoders] Math.cos...? That would be a bad idea because it would lead to discontinuities. The function should be smooth at pi/2, pi, etc. - Gordon ________________________________ From: [email protected] [mailto:[EMAIL PROTECTED] On Behalf Of Guido Sent: Monday, September 17, 2007 1:46 PM To: [email protected] Subject: Re: [flexcoders] Math.cos...? Math.cos() (and every other trig function) should check the argument being PI (or any other of its own constants) before calculating, since it would better reflect the mathematical function and maybe even save up on some performance. On 9/17/07, Gordon Smith <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]> > wrote: 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] <http://yahoogroups.com> ] 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.

