#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;
}
Back to Code Snippets / Powrót do Strzępów Kodu
Comments:
kat -= PI*2;
if (kat < 0.0)
kat = -kat;
while (kat >= PI*2)
kat -= PI*2;
Add a comment: