Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. #include <math.h>
  2. #include <errno.h>
  3. #include "fastmath.h"
  4.  
  5. /* atanh (x) = 0.5 * log ((1.0 + x)/(1.0 - x)) */
  6.  
  7. double atanh(double x)
  8. {
  9.   double z;
  10.   if isnan (x)
  11.     return x;
  12.   z = fabs (x);
  13.   if (z == 1.0)
  14.     {
  15.       errno  = ERANGE;
  16.       return (x > 0 ? INFINITY : -INFINITY);
  17.     }
  18.   if (z > 1.0)
  19.     {
  20.       errno = EDOM;
  21.       return nan("");
  22.     }
  23.   /* Rearrange formula to avoid precision loss for small x.
  24.  
  25.   atanh(x) = 0.5 * log ((1.0 + x)/(1.0 - x))
  26.            = 0.5 * log1p ((1.0 + x)/(1.0 - x) - 1.0)
  27.            = 0.5 * log1p ((1.0 + x - 1.0 + x) /(1.0 - x))
  28.            = 0.5 * log1p ((2.0 * x ) / (1.0 - x))  */
  29.   z = 0.5 * __fast_log1p ((z + z) / (1.0 - z));
  30.   return x >= 0 ? z : -z;
  31. }
  32.