Two things:
Use brackets when putting multiple terms in one calculation because AGS evaluates right to left! This is a historical thing that CJ refuses to change in the face of reason
I see Gilbot did this, but forgot to explain it.
Also, the expansion will be more accurate between -pi/4 and pi/4 - everything else can be refactored to use this range.
For example,
function taylor_sin(int x)
{
// taylor series approximation of sin x
// t = x - x^3/3! + x^5/5! - x^7/7!
// as Gilbot
return t;
}
function taylor_cos(int x)
{
// taylor series approximation of cos x
// t = 1 - x^2/2! + x^4/4! - x^6/6!
// left as an exercise to the reader
return t;
}
function sin(int x)
{
// two steps to try to reduce the drift
while (x > 31416) x = x - 31416;
while (x > 3142) x = x - 2*3142;
if (x < -3*3142/4) return -taylor_sin(x+3142);
if (x < -3142/4) return -taylor_cos(x+3142/2);
if (x < 3142/4) return taylor_sin(x);
if (x < 3*3142/4) return taylor_cos(x-3142/2);
return taylor_sin(x-3142);
}
Or you could use my maths plugin. This is the tech forum so I think I can safely mention that.
Use brackets when putting multiple terms in one calculation because AGS evaluates right to left! This is a historical thing that CJ refuses to change in the face of reason

I see Gilbot did this, but forgot to explain it.
Also, the expansion will be more accurate between -pi/4 and pi/4 - everything else can be refactored to use this range.
For example,
function taylor_sin(int x)
{
// taylor series approximation of sin x
// t = x - x^3/3! + x^5/5! - x^7/7!
// as Gilbot
return t;
}
function taylor_cos(int x)
{
// taylor series approximation of cos x
// t = 1 - x^2/2! + x^4/4! - x^6/6!
// left as an exercise to the reader
return t;
}
function sin(int x)
{
// two steps to try to reduce the drift
while (x > 31416) x = x - 31416;
while (x > 3142) x = x - 2*3142;
if (x < -3*3142/4) return -taylor_sin(x+3142);
if (x < -3142/4) return -taylor_cos(x+3142/2);
if (x < 3142/4) return taylor_sin(x);
if (x < 3*3142/4) return taylor_cos(x-3142/2);
return taylor_sin(x-3142);
}
Or you could use my maths plugin. This is the tech forum so I think I can safely mention that.