Re: sine and cosine functions
Re: sine and cosine functions
- Subject: Re: sine and cosine functions
- From: Doug McNutt <email@hidden>
- Date: Fri, 27 Feb 2004 08:34:54 -0700
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.
----------------------------
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 cosine_of(x) -- degrees
set answer to 0 -- I had to add this - my mail operations run on OS 9.1
if (x = 90) or (x = 270) then
set answer to 0
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
----------------------------
on sine_of(x) -- degrees
set answer to 0
if (x = 180) or (x = 360) then
set answer to 0
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
--
--> Halloween == Oct 31 == Dec 25 == Christmas <--
_______________________________________________
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.