#include "arch.h"

double sin(a)
double a;
{
	register int i, j, q;
	double h;

	h = a / PIB2;
	i = h * ST;
	q = h;
	if (i < 0) {
		i = -i;
		h = -h;
		q = -q + 2;
	}
	h = h * ST - i;
	i %= ST;
	switch (q % 4) {
	case 0 :
		return(st[i] * (1. - h) + st[i + 1] * h);
	case 1 :
		return(st[ST - 1 - i] * h + st[ST - i] * (1. - h));
	case 2 :
		return(-st[i] * (1. - h) - st[i + 1] * h);
	case 3:
		return(-st[ST - 1 - i] * h - st[ST - i] * (1. - h));
	}
}


double cos(a)
double a;
{
	register int i, q;
	double h;

	h = a / PIB2;
	i = h * ST + ST;
	q = (int)h + 1;
	if (i < 0) {
		i = -i;
		h = -h;
		q = -q + 2;
	}
	h = h * ST - i;
	i %= ST;
	switch (q % 4) {
	case 0 :
		return(st[i] * (1. - h) + st[i + 1] * h);
	case 1 :
		return(st[ST - 1 - i] * h + st[ST - i] * (1. - h));
	case 2 :
		return(-st[i] * (1. - h) - st[i + 1] * h);
	case 3:
		return(-st[ST - 1 - i] * h - st[ST - i] * (1. - h));
	}
}


#define ABS(n)  ((n < 0.) ? -n : n)

double atan2(y, x)
double y, x;
{
	register int i;

	if (y + x == y) {
		if (y >= 0.) {
			return(PIB2);
		}
		else {
			return(-PIB2);
		}
	}
	if (ABS(y) <= ABS(x)) {
		if (x > 0.) {
			if (y > 0.) {
			i = (y / x) * AT;
				return(at[i]);
			}
			else {
				i = (-y / x) * AT;
				return(-at[i]);
			}
		}
		else {
			if (y > 0.) {
				i = (y / -x) * AT;
				return(PI - at[i]);
			}
			else {
				i = (-y / -x) * AT;
				return(-PI + at[i]);
			}
		}
	}
	else {
		if (x > 0.) {
			if (y > 0.) {
				i = (x / y) * AT;
				return(PIB2 - at[i]);
			}
			else {
				i = (x / -y) * AT;
				return(-PIB2 + at[i]);
			}
		}
		else {
			if (y > 0.) {
				i = (-x / y) * AT;
				return(PIB2 + at[i]);
			}
			else {
				i = (-x / -y) * AT;
				return(-PIB2 - at[i]);
			}
		}
	}
}
