Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <math.h>
- #include "f.h"
- int main(void)
- {
- double x,res,eps;
- clock_t time;
- printf("x and EPS ");
- if((scanf("%lf",&x)!=1) || scanf("%lf",&eps)!=1)
- {
- fprintf(stderr,"Can not read x or eps\n");
- return ERROR_READ;
- }
- if(eps<0)
- {
- fprintf(stderr,"Incorrect EPS!\n");
- return ERROR_READ;
- }
- time = clock();
- res = fun(x,eps);
- printf("RESULT: %lf\nCalculation error: %lf\nTIME: %lf\n",res,fabs(res - sin(x)),((double)(clock() - time))/CLOCKS_PER_SEC);
- return 0;
- }
- #include<math.h>
- #include "f.h"
- double fun(double x,double eps)
- {
- int i,flag = 0;
- double res,k;
- if(x<0)
- {
- x *= -1;
- flag = 1;
- }
- x -= floor(x/(2*M_PI))*2*M_PI;
- if(x>M_PI)
- {
- x -= M_PI;
- flag++;
- }
- if(x>1)
- {
- x -= M_PI/2;
- if(x<0)
- x*= -1;
- k = 1;
- res = 1;
- for(i = 1;k>eps;i++)
- {
- k *= x*x/((2*i-1)*2*i);
- if(i%2)
- res -= k;
- else
- res += k;
- }
- }else
- {
- k = x;
- res = x;
- for(i = 1;k>eps;i++)
- {
- k *= x*x/(2*i*(2*i + 1));
- if(i%2)
- res -= k;
- else
- res += k;
- }
- }
- if(flag%2)
- res *= -1;
- return res;
- }
- #ifndef F_H
- #define F_H
- double fun(double x0, double eps);
- #define ERROR_READ 1
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement