ID:138466
 
All I need at this time is the sine, cosine, and tangent. If they already exist, could you direct me to them? And if they don't... could you somehow implement them?
On 7/27/00 11:39 pm Spuzzum wrote:
All I need at this time is the sine, cosine, and tangent. If they already exist, could you direct me to them? And if they don't... could you somehow implement them?

You can obtain an approximation as follows:

sin x = x-(x^3/3!)+(x^5/5!)-(x^7/7!)+(x^9/9!)...
cos x = 1-(x^2/2!)+(x^4/4!)-(x^6/6!)+(x^8/8!)...
tan x = x+(x^3/3)+(2x^5/15)...

x must be in radians (2pi rads = 360 deg)
! is factorial eg. 2! = 1*2, 3! = 1*2*3 ...
The above approximations are known as binomial series and expand indefinetly, use as many terms as you require for accuracy - I would think the above is sufficient.

I hope this is of help.
In response to Al
You can obtain an approximation as follows:

sin x = x-(x^3/3!)+(x^5/5!)-(x^7/7!)+(x^9/9!)...
cos x = 1-(x^2/2!)+(x^4/4!)-(x^6/6!)+(x^8/8!)...

Okay, I see the pattern...

tan x = x+(x^3/3)+(2x^5/15)...
Did you intend them to be factorials, or are they simply rational expressions?

In BYOND notation, I think that would be:

proc
factorial(N as num) //taken from the docs - what crashed earlier, I suppose (the return (n)-n bug)
if(N<=0) return 1
return .(N-1)*N

sin(num as num)
var/figure = num
figure -= (num**(3/factorial(3))
figure += (num**(5/factorial(5))
figure -= (num**(7/factorial(7))
return figure

cos(num as num)
var/figure = 1
figure -= num**(2/factorial(2))
figure += num**(4/factorial(4))
figure -= num**(6/factorial(6))
return figure

tan(num as num) //confused about the third iteration... the pattern wasn't completely apparent...
var/figure = 2*num //(num + x^3/3) = (x + x^1) = (x + x) = 2x
figure += 2*(num**5/15)
figure += 3*(num**7/35) //???
return figure

inv(num as num)
return 1/num

inv_tan(num)
return inv(tan(num))

inv_sin(num)
return inv(sin(num))

inv_cos(num)
return inv(cos(num))

I could also make up functions that expand as many times as is specified in an argument (eg var/theta_r = sin(theta,iterations), but that isn't a priority now. It is quite easy when I think about it (a for iteration would do the trick in a pinch, at least for sin and cos), but I'll save it for later.

x must be in radians (2pi rads = 360 deg)
! is factorial eg. 2! = 1*2, 3! = 1*2*3 ...
The above approximations are known as binomial series and expand indefinetly, use as many terms as you require for accuracy - I would think the above is sufficient.

I hope this is of help.

Thank you. I suppose I'm right in assuming that the inverse sin is simply 1/sin()? (All I really learned in school about calculating trigonometric functions is how to calculate sine and that by using the other values (sin = cos/tan, that sort of thing... I might be wrong in that example, I kind of, er, forgot ;-).)


Anyhow, I believe this would be a very good internal feature (hint, hint). And if it isn't, I'm assuredly going to make a library for it. =)


In response to Spuzzum
On 7/28/00 5:37 am Spuzzum wrote:
You can obtain an approximation as follows:

sin x = x-(x^3/3!)+(x^5/5!)-(x^7/7!)+(x^9/9!)...
cos x = 1-(x^2/2!)+(x^4/4!)-(x^6/6!)+(x^8/8!)...

Okay, I see the pattern...

tan x = x+(x^3/3)+(2x^5/15)...
Did you intend them to be factorials, or are they simply rational expressions?

not factorials.
The calculation of the constants is based on power series and is a bit involved to explain here, but yes tan does appear slightly different, if you feel you need them, the next two terms for tan are:
+(17x^7/315)+(62x^9/2835)
In BYOND notation, I think that would be:

proc
factorial(N as num) //taken from the docs - what crashed earlier, I suppose (the return (n)-n bug)
if(N<=0) return 1
return .(N-1)*N

sin(num as num)
var/figure = num
figure -= (num**(3/factorial(3))
figure += (num**(5/factorial(5))
figure -= (num**(7/factorial(7))
return figure

cos(num as num)
var/figure = 1
figure -= num**(2/factorial(2))
figure += num**(4/factorial(4))
figure -= num**(6/factorial(6))
return figure

tan(num as num) //confused about the third iteration... the pattern wasn't completely apparent...
var/figure = 2*num //(num + x^3/3) = (x + x^1) = (x + x) = 2x
figure += 2*(num**5/15)
figure += 3*(num**7/35) //???
return figure
No and No
In the interest of efficiency, you will be much better off calculating your factorials and using the number, rather than re-calcing what is effectively a constant term each time you make the call.
ie
sin(num as num)
var/figure = num
figure -= (num**3)/6
figure += (num**5)/120
figure -= (num**7)/5040
return figure
Also, note the order of computation, your brackets are in the wrong place. You might like to do some optimisation - I would think that the original number of terms were sufficient for the type of calculations that will be done in byond, with this type of calculation you are starting to get very small changes very quickly - don't use more terms than you need.
inv(num as num)
return 1/num

inv_tan(num)
return inv(tan(num))

inv_sin(num)
return inv(sin(num))

inv_cos(num)
return inv(cos(num))
NO!
I could also make up functions that expand as many times as is specified in an argument (eg var/theta_r = sin(theta,iterations), but that isn't a priority now.

I think its unlikely you'll need this facility, generally you are better off making this type of call as computationally simple as possible (you might end up calling it a lot).

It is quite easy when I think about it (a for iteration would do the trick in a pinch, at least for sin and cos), but I'll save it for later.

x must be in radians (2pi rads = 360 deg)
! is factorial eg. 2! = 1*2, 3! = 1*2*3 ...
The above approximations are known as binomial series and expand indefinetly, use as many terms as you require for accuracy - I would think the above is sufficient.

I hope this is of help.

Thank you. I suppose I'm right in assuming that the inverse sin is simply 1/sin()? (All I really learned in school about calculating trigonometric functions is how to calculate sine and that by using the other values (sin = cos/tan, that sort of thing... I might be wrong in that example, I kind of, er, forgot ;-).)
tan = sin/cos
However, if you wish to inverse the trig function (like you get on a calculator sin^-1) this is very different from using the forward function and taking the reciprocal, as you have done above. To avoid this confusion many people call the inverse function the arc function.
if
sin X = Y
then
asin Y = X

Here are the arc series - (inverses)

asin x = x + (1/2)*(x^3/3) + ((1*3)/(2*4))*(x^5/5)
+ ((1*3*5)/(2*4*6))*(x^7/7) + ...
atan x = x - (x^3/3) + (x^5/5) - (x^7/7) + ...
acos x = (pi/2) - asin x

Be careful with your brackets/orders e.g. (x^3/3) means take x, cube it and divide the answer by 3. Remeber, the angle returned will be in radians. Be careful with calls to atan, you can generate some very large numbers.
Anyhow, I believe this would be a very good internal feature (hint, hint). And if it isn't, I'm assuredly going to make a library for it. =)

Sorry I took so long to get back to you, I've been away.
In response to Al
Be careful with your brackets/orders e.g. (x^3/3) means take x, cube it and divide the answer by 3. Remeber, the angle returned will be in radians. Be careful with calls to atan, you can generate some very large numbers.

sorry I meant tan, you'll find that the true function has a discontinuity at pi/2, I haven't tried, but I expect you'll start to get strange results around that number. This also means that you should keep all of your angles going into tan less than pi/2 rads (90deg), which is a pain. You might be better off using tan = sin/cos, more calcs, but more robust (the sin and cos approximations are good for any real number).
In response to Al
Also, note the order of computation, your brackets are in the wrong place.

Oh, dammit. I was always such a purist about order of operations, and I seemed to have violated it on every count =P. My math teacher would be rolling in his lounge chair, on his vacation.