Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //DO NOT USE FOR SCHOOL PROJECTS!!!!
- // Notify author on v.i.c.l.i.c.k@hotmail.com if you plan to use any part of this code between keywords "main" and "act_tanh"!!!!
- //Otherwise use of this code is strictly prohibed.
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <limits.h>
- #include <string.h>
- #include <errno.h>
- #include <math.h>
- double tanh_(double x, double precision);
- int main(void)
- {
- double x,precision,result,precise;
- printf("Zadejte x: ");
- scanf("%lf",&x);
- printf("\n\nZadejte presnost: ");
- scanf("%lf",&precision);
- result=tanh_(x,precision);
- precise=tanh(x);
- printf("Vypocitano: %1.15e \n \nPresne: %1.15e",result,precise);
- printf("\n\n Rozdil: %1.15e",result-precise);
- printf("\n\nPresnost: %1.15e \n\n",precision);
- return EXIT_SUCCESS;
- }
- double tanh_(double x, double precision)
- {
- if(x>20)
- return 1;
- else if(x<(-20))
- return (-1);
- else if(x<=0.000005&&x>=(-0.000005))
- return x;
- double sinh_inc,sinh_=x,cosh_inc,cosh_=1,ant_tanh,act_tanh,x2=x*x;
- double difference;
- double c_inc_numerator=1,c_inc_denominator=1,k=0;
- double s_inc_numerator=x,s_inc_denominator=1;
- while(1)
- {
- ++k;
- //cosh
- c_inc_numerator*=x2; //x^(2*(k+1))=x^2k * x^2
- c_inc_denominator*=((k+k-1)*(k+k)); //(2k+1)! = 2k! * 2k+1 * 2k+2
- cosh_inc=(c_inc_numerator)/(c_inc_denominator);
- cosh_+=cosh_inc;
- //sinh
- s_inc_numerator=c_inc_numerator*x;
- s_inc_denominator=c_inc_denominator*(k+k+1);
- sinh_inc=s_inc_numerator/s_inc_denominator;
- sinh_+=sinh_inc;
- //division >> tanh
- ant_tanh=act_tanh; //store previous result
- act_tanh=((sinh_)/(cosh_)); //make actual result
- difference=ant_tanh-act_tanh; //calculate the difference
- difference=difference<0?(-difference):difference; //make absolute value
- if (difference<=precision)
- return act_tanh;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement