Back to Code Snippets / Powrót do Strzępów Kodu


#include <stdio.h>
#include <math.h>

// czym wieksza dokladnosc tym lepiej ;>
#define DOKLADNOSC 15

double my_pow(double what, int count)
{
 double res = 1.0;
 int i;
 for(i = 0; i < count; i++)
   res *= what;

 return res;
}

double my_factorial(int what)
{
 double res = 1.0f;
 int i;
 for(i = 2; i <= what; i++)
   res *= (double)i;

 return res;
}

double my_cos(double kat)
{
 double sum = 0.0f;
 int i;

 for(i = 0; i < DOKLADNOSC; i++)
 {
   sum += (my_pow(-1.0, i) * my_pow(kat, 2 * i)) / my_factorial(2 * i);
 }

 return sum;
}

int main(void)
{
 double x;

 for(x = 0.0; x < 7.0; x += 0.1)
 {
   double my_res = my_cos(x);
   double org_res = cos(x);
   printf("x = %.3f  my = %.4f  org = %.4f  diff = %.4f\n", x, my_res, org_res, fabs(my_res - org_res));
 }

 return 0;
}

Comments:

2010-10-19 07:04:28 = croner
{
powyżej 7 to już nie działa, bu
}
2010-10-19 07:07:50 = croner
{
while (kat >= PI*2)
kat -= PI*2;
}
2010-10-19 07:17:39 = croner
{
jeszcze fix dla ujemnyhc wartosci kąta

if (kat < 0.0)
kat = -kat;

while (kat >= PI*2)
kat -= PI*2;
}

Add a comment:

Nick:
URL (optional):
Math captcha: 2 ∗ 2 + 6 =