RE: [flexcoders] Math.cos...?

2007-09-17 Thread Gordon Smith
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: 'flexcoders@yahoogroups.com'
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: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On Behalf Of Guido
Sent: Monday, September 17, 2007 1:46 PM
To: flexcoders@yahoogroups.com
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: flexcoders@yahoogroups.com [mailto:flexcoders@yahoogroups.com 
<http://yahoogroups.com> ] On Behalf Of Jon Bradley
Sent: Sunday, September 16, 2007 2:49 PM
    To: flexcoders@yahoogroups.com
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 (0x1, 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.











 


RE: [flexcoders] Math.cos...?

2007-09-17 Thread Gordon Smith
That would be a bad idea because it would lead to discontinuities. The
function should be smooth at pi/2, pi, etc.
 
- Gordon



From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of Guido
Sent: Monday, September 17, 2007 1:46 PM
To: flexcoders@yahoogroups.com
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: flexcoders@yahoogroups.com
[mailto:flexcoders@yahoogroups.com <http://yahoogroups.com> ] On Behalf
Of Jon Bradley
Sent: Sunday, September 16, 2007 2:49 PM
To: flexcoders@yahoogroups.com
    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 (0x1, 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.











 


Re: [flexcoders] Math.cos...?

2007-09-17 Thread Jon Bradley

On Sep 17, 2007, at 4:49 PM, Mike Krotscheck wrote:

I may take you up on this challenge- I’ve got this strange  
masochistic urge to see if ML has the same restriction. I seem to  
recall proving e^i to an arbitrary significant digit, so Pi  
shouldn’t be too much different.


You could do that. But you cannot store PI or any other infinite  
sequence in the machine from which to perform calculations on.  
Getting a specific digit of PI, or something similar (say 22/7), is  
different than using it in a calculation.


- j



RE: [flexcoders] Math.cos...?

2007-09-17 Thread Mike Krotscheck
I may take you up on this challenge- I've got this strange masochistic
urge to see if ML has the same restriction. I seem to recall proving e^i
to an arbitrary significant digit, so Pi shouldn't be too much
different.

 

Michael Krotscheck

Senior Developer

 


RESOURCE INTERACTIVE

<http://www.resource.com/> www.resource.com <http://www.resource.com> 

[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]> 



From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of Samuel R. Neff
Sent: Sunday, September 16, 2007 11:30 PM
To: flexcoders@yahoogroups.com
Subject: RE: [flexcoders] Math.cos...?

 

 

It's not sloppy, it's just how floating point numbers work.  Try the
same thing in other programming languages and you still will not get
zero (unless they round the output).  For example, .NET reports the
result as 

6.12303176911189E-17.  

 

Sam

---
We're Hiring! Seeking a passionate developer to join our team building
Flex based products. Position is in the Washington D.C. metro area. If
interested contact [EMAIL PROTECTED]
  

 

 



From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of Mike Krotscheck
Sent: Sunday, September 16, 2007 5:19 PM
To: flexcoders@yahoogroups.com
Subject: RE: [flexcoders] Math.cos...?

The documentation's actually fairly clear on this: "The cosine of a 90
degree angle is zero, but because of the inherent inaccuracy of decimal
calculations using binary numbers, Flash Player will report a number
extremely close to, but not exactly equal to, zero."

 

Nevertheless, it seems... sloppy to me.

 

Michael Krotscheck

Senior Developer

 




From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of Troy Gilbert
Sent: Sunday, September 16, 2007 5:15 PM
To: flexcoders@yahoogroups.com
Subject: Re: [flexcoders] Math.cos...?

 

> 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.

 



Re: [flexcoders] Math.cos...?

2007-09-17 Thread Troy Gilbert
ML == MathLab? Well, of course MathLab would go to great trouble to minimize
this issue as much as possible, including doing its calculations in such a
way that it preserves fractional representations and evaluates by truly
combining expressions and doing simplification, as opposed to rote
calculation to a numerical value then substitution.

I also wouldn't be surprised if ML didn't explicitly handle common Pi
constants. While it absolutely wouldn't be an optimization for standard trig
functions (it doesn't take many lookups on modern CPU's before just letting
the floating-point unit do its thing to be faster -- and if you start
choosing 'shortcuts' you'll never make everyone happy).

Troy.


On 9/17/07, Mike Krotscheck <[EMAIL PROTECTED]> wrote:
>
>  I may take you up on this challenge- I've got this strange masochistic
> urge to see if ML has the same restriction. I seem to recall proving e^i to
> an arbitrary significant digit, so Pi shouldn't be too much different.
>
>
>
> *Michael Krotscheck*
>
> Senior Developer
>
>
>
>
> *RESOURCE INTERACTIVE*
>
> <http://www.resource.com/>www.resource.com
>
> [EMAIL PROTECTED]
>   --
>
> *From:* flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] *On
> Behalf Of *Samuel R. Neff
> *Sent:* Sunday, September 16, 2007 11:30 PM
> *To:* flexcoders@yahoogroups.com
> *Subject:* RE: [flexcoders] Math.cos...?
>
>
>
>
>
> It's not sloppy, it's just how floating point numbers work.  Try the same
> thing in other programming languages and you still will not get zero (unless
> they round the output).  For example, .NET reports the result as
>
> 6.12303176911189E-17.
>
>
>
> Sam
>
> ---
> We're Hiring! Seeking a passionate developer to join our team building
> Flex based products. Position is in the Washington D.C. metro area. If
> interested contact [EMAIL PROTECTED]
>
>
>
>
>
>  ------
>
> *From:* flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] *On
> Behalf Of *Mike Krotscheck
> *Sent:* Sunday, September 16, 2007 5:19 PM
> *To:* flexcoders@yahoogroups.com
> *Subject:* RE: [flexcoders] Math.cos...?
>
> The documentation's actually fairly clear on this: "The cosine of a 90
> degree angle is zero, but because of the inherent inaccuracy of decimal
> calculations using binary numbers, Flash Player will report a number
> extremely close to, but not exactly equal to, zero."
>
>
>
> Nevertheless, it seems… sloppy to me.
>
>
>
> *Michael Krotscheck*
>
> Senior Developer
>
>
>
>   --
>
> *From:* flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] *On
> Behalf Of *Troy Gilbert
> *Sent:* Sunday, September 16, 2007 5:15 PM
> *To:* flexcoders@yahoogroups.com
> *Subject:* Re: [flexcoders] Math.cos...?
>
>
>
> > 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.
>
> 
>


Re: [flexcoders] Math.cos...?

2007-09-17 Thread Guido
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]> 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:* flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] *On
> Behalf Of *Jon Bradley
> *Sent:* Sunday, September 16, 2007 2:49 PM
> *To:* flexcoders@yahoogroups.com
> *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 (0x1, 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.
>
>
>   
>


RE: [flexcoders] Math.cos...?

2007-09-17 Thread Gordon Smith
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: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of Jon Bradley
Sent: Sunday, September 16, 2007 2:49 PM
To: flexcoders@yahoogroups.com
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 (0x1, 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.


 


RE: [flexcoders] Math.cos...?

2007-09-16 Thread Samuel R. Neff
 
It's not sloppy, it's just how floating point numbers work.  Try the same
thing in other programming languages and you still will not get zero (unless
they round the output).  For example, .NET reports the result as 
6.12303176911189E-17.  
 
Sam

---
We're Hiring! Seeking a passionate developer to join our team building Flex
based products. Position is in the Washington D.C. metro area. If interested
contact [EMAIL PROTECTED]
  

 

  _  

From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of Mike Krotscheck
Sent: Sunday, September 16, 2007 5:19 PM
To: flexcoders@yahoogroups.com
Subject: RE: [flexcoders] Math.cos...?



The documentation's actually fairly clear on this: "The cosine of a 90
degree angle is zero, but because of the inherent inaccuracy of decimal
calculations using binary numbers, Flash Player will report a number
extremely close to, but not exactly equal to, zero."

 

Nevertheless, it seems. sloppy to me.

 

Michael Krotscheck

Senior Developer

 


  _  

From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of Troy Gilbert
Sent: Sunday, September 16, 2007 5:15 PM
To: flexcoders@yahoogroups.com
Subject: Re: [flexcoders] Math.cos...?

 

> 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.



RE: [flexcoders] Math.cos...?

2007-09-16 Thread Mike Krotscheck
The documentation's actually fairly clear on this: "The cosine of a 90
degree angle is zero, but because of the inherent inaccuracy of decimal
calculations using binary numbers, Flash Player will report a number
extremely close to, but not exactly equal to, zero."

 

Nevertheless, it seems... sloppy to me.

 

Michael Krotscheck

Senior Developer

 


RESOURCE INTERACTIVE

<http://www.resource.com/> www.resource.com <http://www.resource.com> 

614 621 2888  main

 

  
614 410 3195  direct

412 726 8087  mobile

[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]> 



From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of Troy Gilbert
Sent: Sunday, September 16, 2007 5:15 PM
To: flexcoders@yahoogroups.com
Subject: Re: [flexcoders] Math.cos...?

 

> 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.

 



Re: [flexcoders] Math.cos...?

2007-09-16 Thread Jon Bradley

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 (0x1, 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.




Re: [flexcoders] Math.cos...?

2007-09-16 Thread Troy Gilbert
> 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.


[flexcoders] Math.cos...?

2007-09-16 Thread Mike Krotscheck
Why does Math.cos(Math.PI/2) not return zero?

 

Michael Krotscheck

Senior Developer

 


RESOURCE INTERACTIVE

 www.resource.com  

614 621 2888  main

 

  
614 410 3195  direct

412 726 8087  mobile

[EMAIL PROTECTED]