Re: sine and cosine functions
Re: sine and cosine functions
- Subject: Re: sine and cosine functions
- From: ehsan saffari <email@hidden>
- Date: Fri, 27 Feb 2004 10:44:31 -0600
On Fri, 27 Feb 2004 08:34:54 -0700, Doug McNutt
<email@hidden> wrote:
>
At 08:25 -0600 2/27/04, ehsan saffari wrote:
>
>Here are native AS functions for sine & cosine, based on a Guidebook
>
>module published by Apple. Input in degrees.
>
>
I ran this test on some Control Data software in the early 70's. They said
>
their sine/cosine routines couldn't possibly be wrong because they were
>
used to send a man to the moon. They were wrong too. A simple Taylor
>
series expansion needs a whole lot more iterations for angles for which
>
the answer is nearly unity. Chebycheff polynomials are a whole lot better
>
and the hardware implementation in the chip is even better than that.
>
>
It's possible that my AppleScript (1.8.3) is not setting the list
>
correctly as an lvalue but my G4 is booted up in Yellow Dog right now.
Thanks Doug, Deivy & Emmanuel
My mistake! The error your test script reports is due to my leaving out
an 'else' in the sine_of function (also pointed out by Deivy). So here's
the corrected version.
Math & More Math can be found at
http://www.osaxen.com
The guide book module from apple is Module SRT0260, contact me offlist if
you want a copy, i don't have a url handy.
Speedwise, the script below and identical version using satimage's sin &
cos, both take less than 1 sec to complete. The script that i use these
for, calls the sine & cosine functions over 900 times in 1 run, to
calculate the phases of the moon for 1 calendar year. Our men will be
safe going to the moon, as long as the oxygen tank doesn't blow up first.
there should be no errors, this time for sure!
------------------------------------------------
set angle to 0
set increment to 1
set limit to 0.2
repeat while angle < 180
set small to (cosine_of(angle)) ^ 2 + (sine_of(angle)) ^ 2 - 1
set tester to small
if small < 0 then
set tester to -small
end if
if tester > limit then
set aline to "angle = " & (angle as text) & return & "error = " &
(small as text)
display dialog aline
end if
set angle to angle + increment
end repeat
on sine_of(x)
if x is in {-360, -180, 0, 180, 360} then
set answer to 0
else
set x to (x - (((x / 360) div 1) * 360)) * (pi / 180)
set {answer, numerator, denominator, factor} to {0, x, 1, -(x ^ 2)}
repeat with i from 3 to 40 by 2
set answer to answer + numerator / denominator
set numerator to numerator * factor
set denominator to denominator * i * (i - 1)
end repeat
end if
return answer
end sine_of
on cosine_of(x)
if x < 0 then set x to -x
if x is in {90, 270} then
set answer to 0
else if x = 0 then
set answer to 1
else
set x to (x - (((x / 360) div 1) * 360)) * (pi / 180)
set {answer, numerator, denominator, factor} to {0, 1, 1, -(x ^ 2)}
repeat with i from 2 to 40 by 2
set answer to answer + numerator / denominator
set numerator to numerator * factor
set denominator to denominator * i * (i - 1)
end repeat
end if
return answer
end cosine_of
------------------------------------------------
cheers
ehsan
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.