Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.01 KB | None | 0 0
  1. //DO NOT USE FOR SCHOOL PROJECTS!!!!
  2. // 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"!!!!
  3. //Otherwise use of this code is strictly prohibed.
  4.  
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <ctype.h>
  9. #include <limits.h>
  10. #include <string.h>
  11. #include <errno.h>
  12. #include <math.h>
  13.  
  14. double tanh_(double x, double precision);
  15.  
  16. int main(void)
  17. {
  18.     double x,precision,result,precise;
  19.  
  20.     printf("Zadejte x: ");
  21.     scanf("%lf",&x);
  22.     printf("\n\nZadejte presnost: ");
  23.     scanf("%lf",&precision);
  24.     result=tanh_(x,precision);
  25.     precise=tanh(x);
  26.  
  27.  
  28.     printf("Vypocitano: %1.15e \n \nPresne: %1.15e",result,precise);
  29.     printf("\n\n Rozdil: %1.15e",result-precise);
  30.     printf("\n\nPresnost: %1.15e \n\n",precision);
  31.     return EXIT_SUCCESS;
  32. }
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44. double tanh_(double x, double precision)
  45. {
  46. if(x>20)
  47.     return 1;
  48.  
  49. else if(x<(-20))
  50.     return (-1);
  51.  
  52. else if(x<=0.000005&&x>=(-0.000005))
  53.     return x;
  54.  
  55. double sinh_inc,sinh_=x,cosh_inc,cosh_=1,ant_tanh,act_tanh,x2=x*x;
  56. double difference;
  57. double c_inc_numerator=1,c_inc_denominator=1,k=0;
  58. double s_inc_numerator=x,s_inc_denominator=1;
  59.     while(1)
  60.     {
  61.         ++k;
  62.  
  63.         //cosh
  64.         c_inc_numerator*=x2; //x^(2*(k+1))=x^2k * x^2
  65.         c_inc_denominator*=((k+k-1)*(k+k)); //(2k+1)! = 2k! * 2k+1 * 2k+2
  66.  
  67.         cosh_inc=(c_inc_numerator)/(c_inc_denominator);
  68.         cosh_+=cosh_inc;
  69.  
  70.  
  71.         //sinh
  72.         s_inc_numerator=c_inc_numerator*x;
  73.         s_inc_denominator=c_inc_denominator*(k+k+1);
  74.  
  75.         sinh_inc=s_inc_numerator/s_inc_denominator;
  76.         sinh_+=sinh_inc;
  77.  
  78.  
  79.         //division >> tanh
  80.         ant_tanh=act_tanh; //store previous result
  81.         act_tanh=((sinh_)/(cosh_)); //make actual result
  82.         difference=ant_tanh-act_tanh; //calculate the difference
  83.         difference=difference<0?(-difference):difference; //make absolute value
  84.  
  85.         if (difference<=precision)
  86.         return act_tanh;
  87.     }
  88.  
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement