Exemple #1
0
void buildSinTable(short* pTbl) {
    int i;
    pSinTable = pTbl;
    
    for (i = 0;i < SINTABLE_LENGTH;++i) {
        const float s = taylor_sin(F_PI * (float)i / (float)SINTABLE_LENGTH);
        pSinTable[i] = (short) float_to_fixed(s);
    }
    
}
Exemple #2
0
double taylor_sin(double x, unsigned int N)
{
	if(x >= 0)
	{
		while(x >= TWO_PI)
			x -= TWO_PI;

		if(x >= PI)
			if(x - PI >= HALF_PI)
				return -1 * taylor_sin_bounded(TWO_PI - x, N);
			else
				return -1 * taylor_sin_bounded(x - PI, N);
		else
			if(x >= HALF_PI)
				return taylor_sin_bounded(PI - x, N);
			else
				return taylor_sin_bounded(x, N);
	}

	return -1 * taylor_sin(-x, N);
}
Exemple #3
0
int main(int argc, char **argv)
{
	double x, y;
	unsigned int n, p, D;
	mpfr_t R, X;
	char sf[50];
	FILE *in;
	
	if(argc > 1)
	{
		switch(argv[1][0])
		{
			case 'a':
				if(argc == 5 && 
				   sscanf(argv[2], "%lf", &x) == 1 &&
				   sscanf(argv[3], "%u" , &n) == 1 &&
				   sscanf(argv[4], "%u" , &D) == 1)
					printf("Cos(%.*lf) = %.*lf\n",
							d(D), x, D, taylor_cos(x, n));
				else
					printf("Usage: %s a <x=value for Cos(x)> <n> "
						   "<D=Number of digits to display>\n",

							argv[0]);
				break;
			 
			case 'b':
				if(argc == 5 && 
				   sscanf(argv[2], "%lf", &x) == 1 &&
				   sscanf(argv[3], "%u" , &n) == 1 &&
				   sscanf(argv[4], "%u" , &D) == 1)
					printf("Sin(%.*lf) = %.*lf\n",
							d(D), x, D, taylor_sin(x, n));
				else
					printf("Usage: %s b <x=value for Sin(x)> <n> "
						   "<D=Number of digits to display>\n",

							argv[0]);
				break;

			case 'c':
				if(argc == 5 && 
				   sscanf(argv[2], "%lf", &x) == 1 &&
				   sscanf(argv[3], "%u" , &n) == 1 &&
				   sscanf(argv[4], "%u" , &D) == 1)
					printf("Tan(%.*lf) = %.*lf\n",
							d(D), x, D, taylor_tan(x, n));
				else
					printf("Usage: %s a <x=value for Tan(x)> <n> "
						   "<D=Number of digits to display>\n",

							argv[0]);
				break;
			
			case 'd':
				if(argc == 6 && 
				   sscanf(argv[3], "%u", &D) == 1 &&
				   sscanf(argv[4], "%u", &n) == 1 &&
				   sscanf(argv[5], "%u", &p) == 1)
				{
					mpfr_set_default_prec(p);
					INIT_CONSTANTS
					if (mpfr_init_set_str(X, argv[2], 10, MPFR_RNDN) == 0)
					{
						mpfr_init(R);

						sprintf(sf, "cos(%%.%uRNf) =~\n\t%%.%uRNf\n",
							d(D), D);
						
						mpfr_taylor_cos(R, X, n);
						mpfr_printf(sf, X, R);
					}
					else
						printf("Usage: %s d <x=value for Cos(x)> "
							   "<D=Number of digits to display> "
							   "<n> <p=bits of precision to use>\n",
							   argv[0]);
				}
				else
					printf("Usage: %s d <x=value for Cos(x)> "
						   "<D=Number of digits to display> "
						   "<n> <p=bits of precision to use>\n",
						   argv[0]);
				break;
			
			case 'e':
				if(argc == 6 && 
				   sscanf(argv[3], "%u", &D) == 1 &&
				   sscanf(argv[4], "%u", &n) == 1 &&
				   sscanf(argv[5], "%u", &p) == 1)
				{
					mpfr_set_default_prec(p);
					INIT_CONSTANTS
					if (mpfr_init_set_str(X, argv[2], 10, MPFR_RNDN) == 0)
					{
						mpfr_init(R);

						sprintf(sf, "sin(%%.%uRNf) =~\n\t%%.%uRNf\n",
							d(D), D);
						
						mpfr_taylor_sin(R, X, n);
						mpfr_printf(sf, X, R);
					}
					else
						printf("Usage: %s d <x=value for Sin(x)> "
							   "<D=Number of digits to display> "
							   "<n> <p=bits of precision to use>\n",
							   argv[0]);
				}
Exemple #4
0
double taylor_tan(double x, unsigned int N)
{
	return taylor_sin(x,N)/taylor_cos(x,N);
}