sábado, 4 de setembro de 2010

Funções matemáticas em C

Implementações das funções seno, cosseno, logaritmo natural e exponencial em C. Possui uns testes (sem arcabouço de teste) que compara com as funções da biblioteca math.h.

  #include <stdio.h> #include <math.h> #define epsilon 1e-8 double seno(double x); double cosseno(double x); /* return -1 se entrada invalida, entrada valida = ]0, 2]*/ double ln(double x); double expo(double x); double modulo(double x); /* return 1 se igual, 0 se diferente. */ double igualComEpsilon(double x, double y); void testaTudo(); int testaValoresConhecidos(); int testaValoresGerais(); int testaPropriedades(); int testaValoresSeno(); int testaValoresCosseno(); int testaValoresLn(); int testaValoresExp(); int testaProp1(); int testaProp2(); int testaProp3(); int main() { testaTudo(); printf("\n\nFim.\n"); getchar(); return 0; } double seno(double x) { int k = 0; double termo = x, y = 0; while(modulo(termo) > epsilon) { y = y + termo; k = k + 2; termo = termo * (-1 * (x * x)) / (k * (k + 1)); } return y; } double cosseno(double x) { int k = 0; double termo = 1, y = 0; while(modulo(termo) > epsilon) { y = y + termo; k = k + 2; termo = termo * (-1 * (x * x)) / (k * (k - 1)); } return y; } double ln(double x) { int k = 1; double termo = x, y = 0; x++; if(x <= 0 || x > 2) return -1; while(modulo(termo) > epsilon) { y = y + termo; termo = termo * (-k * x) / (k+1); k = k + 1; //printf("%f\n", termo); // getchar(); } return y; } double expo(double x) { int k = 0; double termo = 1, y = 0; while(modulo(termo) > epsilon) { y = y + termo; k = k + 1; termo = termo * (x) / (k); } return y; } double modulo(double x) { if(x < 0) { x = sqrt(x * x); } return x; } double igualComEpsilon(double x, double y) { if(modulo(x - y) < epsilon) return 1; else return 0; } void testaTudo() { int qtdeDeErros; printf("Testando Tudo\n"); printf("________________________________________\n"); printf("\nTestando valores conhecidos:\n\n"); qtdeDeErros = testaValoresConhecidos(); printf("\nTotal: %d erro(s)\n", qtdeDeErros); printf("________________________________________\n"); printf("\nTestando valores gerais:\n\n"); qtdeDeErros = testaValoresGerais(); printf("\nTotal: %d erro(s)\n", qtdeDeErros); printf("________________________________________\n"); printf("\nTestando propriedades:\n\n"); qtdeDeErros = testaPropriedades(); printf("\nTotal: %d erro(s)\n", qtdeDeErros); } int testaValoresConhecidos() { int qtdeDeErros = 0; double t; t = 0; printf("0 : "); if(igualComEpsilon(seno(t), sin(t)) == 1) printf("SEN OK "); else { printf("SEN ERROR "); qtdeDeErros = qtdeDeErros + 1; } if(igualComEpsilon(cosseno(t), cos(t)) == 1) printf("COS OK\n"); else { printf("COS ERROR\n"); qtdeDeErros = qtdeDeErros + 1; } t = M_PI; printf("pi : "); if(igualComEpsilon(seno(t), sin(t)) == 1) printf("SEN OK "); else { printf("SEN ERROR "); qtdeDeErros = qtdeDeErros + 1; } if(igualComEpsilon(cosseno(t), cos(t)) == 1) printf("COS OK\n"); else { printf("COS ERROR\n"); qtdeDeErros = qtdeDeErros + 1; } t = M_PI / 2; printf("pi/2 : "); if(igualComEpsilon(seno(t), sin(t)) == 1) printf("SEN OK "); else { printf("SEN ERROR "); qtdeDeErros = qtdeDeErros + 1; } if(igualComEpsilon(cosseno(t), cos(t)) == 1) printf("COS OK\n"); else { printf("COS ERROR\n"); qtdeDeErros = qtdeDeErros + 1; } t = M_PI / 3; printf("pi/3 : "); if(igualComEpsilon(seno(t), sin(t)) == 1) printf("SEN OK "); else { printf("SEN ERROR "); qtdeDeErros = qtdeDeErros + 1; } if(igualComEpsilon(cosseno(t), cos(t)) == 1) printf("COS OK\n"); else { printf("COS ERROR\n"); qtdeDeErros = qtdeDeErros + 1; } t = M_PI / 4; printf("pi/4 : "); if(igualComEpsilon(seno(t), sin(t)) == 1) printf("SEN OK "); else { printf("SEN ERROR "); qtdeDeErros = qtdeDeErros + 1; } if(igualComEpsilon(cosseno(t), cos(t)) == 1) printf("COS OK\n"); else { printf("COS ERROR\n"); qtdeDeErros = qtdeDeErros + 1; } t = 2 * M_PI / 3; printf("2pi/3: "); if(igualComEpsilon(seno(t), sin(t)) == 1) printf("SEN OK "); else { printf("SEN ERROR "); qtdeDeErros = qtdeDeErros + 1; } if(igualComEpsilon(cosseno(t), cos(t)) == 1) printf("COS OK\n"); else { printf("COS ERROR\n"); qtdeDeErros = qtdeDeErros + 1; } t = 3 * M_PI / 2; printf("3pi/2: "); if(igualComEpsilon(seno(t), sin(t)) == 1) printf("SEN OK "); else { printf("SEN ERROR "); qtdeDeErros = qtdeDeErros + 1; } if(igualComEpsilon(cosseno(t), cos(t)) == 1) printf("COS OK\n"); else { printf("COS ERROR\n"); qtdeDeErros = qtdeDeErros + 1; } t = 1; if(igualComEpsilon(ln(t), 0) == 1) printf("LN OK "); else { printf("LN ERROR\n"); qtdeDeErros = qtdeDeErros + 1; } t = 1; printf("1 : "); if(igualComEpsilon(expo(t), exp(t)) == 1) printf("EXP OK\n"); else { printf("EXP ERROR\n"); qtdeDeErros = qtdeDeErros + 1; } t = 0; printf("0 : "); if(igualComEpsilon(expo(t), exp(t)) == 1) printf("EXP OK\n"); else { printf("EXP ERROR\n"); qtdeDeErros = qtdeDeErros + 1; } t = -1; printf("-1 : "); if(igualComEpsilon(expo(t), exp(t)) == 1) printf("EXP OK\n"); else { printf("EXP ERROR\n"); qtdeDeErros = qtdeDeErros + 1; } return qtdeDeErros; } int testaValoresGerais() { int qtdeDeErros, qtdeDeErrosTotal = 0; qtdeDeErros = testaValoresSeno(); qtdeDeErrosTotal = qtdeDeErrosTotal + qtdeDeErros; printf(" seno: %4d erro(s)\n", qtdeDeErros); qtdeDeErros = testaValoresCosseno(); qtdeDeErrosTotal = qtdeDeErrosTotal + qtdeDeErros; printf("cosseno: %4d erro(s)\n", qtdeDeErros); qtdeDeErros = testaValoresLn(); qtdeDeErrosTotal = qtdeDeErrosTotal + qtdeDeErros; printf(" ln: %4d erro(s)\n", qtdeDeErros); qtdeDeErros = testaValoresExp(); qtdeDeErrosTotal = qtdeDeErrosTotal + qtdeDeErros; printf(" exp: %4d erro(s)\n", qtdeDeErros); return qtdeDeErrosTotal; } int testaPropriedades() { int qtdeDeErros, qtdeDeErrosTotal = 0; qtdeDeErros = testaProp1(); qtdeDeErrosTotal = qtdeDeErrosTotal + qtdeDeErros; printf("sin^2(x) + cos^2(x) = 1: %4d erro(s)\n", qtdeDeErros); qtdeDeErros = testaProp2(); qtdeDeErrosTotal = qtdeDeErrosTotal + qtdeDeErros; printf(" e^ln(x) = x: %4d erro(s)\n", qtdeDeErros); qtdeDeErros = testaProp3(); qtdeDeErrosTotal = qtdeDeErrosTotal + qtdeDeErros; printf(" ln(e^x) = x: %4d erro(s)\n", qtdeDeErros); return qtdeDeErrosTotal; } int testaValoresSeno() { int t, qtdeDeErros = 0; double j; for(j = -5; j <= 5; j = j + 0.01) { t = igualComEpsilon(seno(j), sin(j)); if(t == 0) qtdeDeErros = qtdeDeErros + 1; } return qtdeDeErros; } int testaValoresCosseno() { int t, qtdeDeErros = 0; double j; for(j = -5; j <= 5; j = j + 0.01) { t = igualComEpsilon(cosseno(j), cos(j)); if(t == 0) qtdeDeErros = qtdeDeErros + 1; } return qtdeDeErros; } int testaValoresLn() { int t, qtdeDeErros = 0; double j; for(j = 1; j <= 2; j = j + 0.01) { t = igualComEpsilon(ln(j), log(j)); if(t == 0) qtdeDeErros = qtdeDeErros + 1; } return qtdeDeErros; } int testaValoresExp() { int t, qtdeDeErros = 0; double j; for(j = -5; j <= 5; j = j + 0.01) { t = igualComEpsilon(expo(j), exp(j)); if(t == 0) qtdeDeErros = qtdeDeErros + 1; } return qtdeDeErros; } int testaProp1() { int t, qtdeDeErros = 0; double j, aux; for(j = -5; j <= 5; j = j + 0.01) { aux = (cosseno(j)*cosseno(j)) + (seno(j)*seno(j)); t = igualComEpsilon(aux, 1); if(t == 0) qtdeDeErros = qtdeDeErros + 1; } return qtdeDeErros; } int testaProp2() { int t, qtdeDeErros = 0; double j, aux; for(j = 1; j <= 2; j = j + 0.01) { aux = expo(ln(j)); t = igualComEpsilon(aux, j); if(t == 0) qtdeDeErros = qtdeDeErros + 1; } return qtdeDeErros; } int testaProp3() { int t, qtdeDeErros = 0; double j, aux; for(j = 0; j <= 2; j = j + 0.01) { aux = ln(expo(j)); t = igualComEpsilon(aux, j); if(t == 0) qtdeDeErros = qtdeDeErros + 1; } return qtdeDeErros; }

Nenhum comentário:

Postar um comentário