Advertisement
Guest User

sinhatanh test

a guest
Oct 17th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <float.h>
  4. #include <typeinfo>
  5.  
  6. #define debug(x) printf(#x " = %.20lle\n", x)
  7.  
  8. template<class R>
  9. R sinhatanh(R x)
  10. {
  11.     if (typeid(R) == typeid(float))
  12.         return sinhf(atanhf(x));
  13.     else if (typeid(R) == typeid(double))
  14.         return sinh(atanh(x));
  15.     else if (typeid(R) == typeid(long double))
  16.         return sinhl(atanhl(x));
  17.  
  18.     __builtin_unreachable();
  19. }
  20.  
  21. template<class R>
  22. R sinhatanh2(R x)
  23. {
  24.     if (typeid(R) == typeid(float))
  25.         return x/sqrtf(1 - x*x);
  26.     else if (typeid(R) == typeid(double))
  27.         return x/sqrt(1 - x*x);
  28.     else if (typeid(R) == typeid(long double))
  29.         return x/sqrtl(1 - x*x);
  30.  
  31.     __builtin_unreachable();
  32. }
  33.  
  34. template<class R>
  35. R coshatanh(R x)
  36. {
  37.     if (typeid(R) == typeid(float))
  38.         return coshf(atanhf(x));
  39.     else if (typeid(R) == typeid(double))
  40.         return cosh(atanh(x));
  41.     else if (typeid(R) == typeid(long double))
  42.         return coshl(atanhl(x));
  43.  
  44.     __builtin_unreachable();
  45. }
  46.  
  47. template<class R>
  48. R coshatanh2(R x)
  49. {
  50.     if (typeid(R) == typeid(float))
  51.         return 1/sqrtf(1 - x*x);
  52.     else if (typeid(R) == typeid(double))
  53.         return 1/sqrt(1 - x*x);
  54.     else if (typeid(R) == typeid(long double))
  55.         return 1/sqrtl(1 - x*x);
  56.  
  57.     __builtin_unreachable();
  58. }
  59.  
  60. template<class R>
  61. R nextafter_(R x, R y)
  62. {
  63.     if (typeid(R) == typeid(float))
  64.         return nextafterf(x, y);
  65.     else if (typeid(R) == typeid(double))
  66.         return nextafter(x, y);
  67.     else if (typeid(R) == typeid(long double))
  68.         return nextafterl(x, y);
  69.  
  70.     __builtin_unreachable();
  71. }
  72.  
  73. void sinh_test()
  74. {
  75.     long double xi = 1, s1, s2, mone=-1;
  76.    
  77.     do
  78.     {
  79.         s1 = sinhatanh(xi);
  80.         s2 = sinhatanh2(xi);
  81.        
  82.         if (s1 != s2)
  83.         {    
  84.             debug(s1);
  85.             debug(s2);
  86.             debug(s1 - s2);
  87.             debug(xi);
  88.         }
  89.  
  90.         xi = nextafter_(xi, mone);
  91.  
  92.     } while(isnan(s1-s2) || isinf(s2-s1) || s1 - s2 == 0);
  93. }
  94.  
  95. void cosh_test()
  96. {
  97.     long double xi = 1, c1, c2, mone=-1;
  98.    
  99.     do
  100.     {
  101.         c1 = coshatanh(xi);
  102.         c2 = coshatanh2(xi);
  103.        
  104.         if (c1 != c2)
  105.         {    
  106.             debug(c1);
  107.             debug(c2);
  108.             debug(c1 - c2);
  109.             debug(xi);
  110.         }
  111.  
  112.         xi = nextafter_(xi, mone);
  113.  
  114.     } while (isnan(c1-c2) || isinf(c2-c1) || c1 - c2 == 0);
  115. }
  116.  
  117. int main()
  118. {
  119.    
  120.     sinh_test();
  121.     cosh_test();
  122.  
  123.     return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement