Re: sine and cosine functions
Re: sine and cosine functions
- Subject: Re: sine and cosine functions
- From: Deivy Petrescu <email@hidden>
- Date: Fri, 27 Feb 2004 10:07:31 -0500
At 8:25 AM -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.
In my unscientific tests, they are faster than using the same functions
from 'Math' or 'More Math' osaxen.
Where did you find them?
----------------------------
on cosine_of(x) -- degrees
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
----------------------------
cosine_of(x) above is fine, but you forgot an
else in the sine_of(x) handler bellow.
To check routines with sine and cosine run them against the following identity:
(sin(x))^2+(cos(x))^2=1 for all x.
If it satisfies that for any (two or three random) number, you should be fine.
May be you could improve the speed by checking
for conditions a bit more general. For instance
cos(0)=1, you do not need to compute it. And
cos(-90)=0 as well, so no need to compute it
----------------------------
on sine_of(x) -- degrees
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
----------------------------
cheers
ehsan
p.s hope boo can see this!
--
Regards
Saudagues
Deivy
http://www.dicas.com
_______________________________________________
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.