amath  1.8.5
Simple command line calculator
tanh.c File Reference

Hyperbolic tangent function. More...

#include "prim.h"
Include dependency graph for tanh.c:

Go to the source code of this file.

Functions

double tanh (double x)
 Hyperbolic tangent function. More...
 

Variables

static const double one = 1.0
 
static const double two = 2.0
 
static const double tiny = 1.0e-300
 

Detailed Description

Hyperbolic tangent function.

Definition in file tanh.c.

Function Documentation

◆ tanh()

double tanh ( double  x)

Hyperbolic tangent function.

Returns
The Hyperbolic Tangent of x
Method
                                   x    -x
                                  e  - e
    0. tanh(x) is defined to be --------—
                                   x    -x
                                  e  + e
    1. reduce x to non-negative by tanh(-x) = -tanh(x)
    2.  0      <= x <= 2**-55 : tanh(x) = x*(one+x)
                                           -t
        2**-55 <  x <=  1     : tanh(x) = --—; t = expm1(-2x)
                                          t + 2
                                                2
        1      <= x <=  22.0  : tanh(x) = 1-  --— ; t=expm1(2x)
                                              t + 2
        22.0   <  x <= INF    : tanh(x) = 1
Special cases
    tanh(NaN) is NaN
    only tanh(0)=0 is exact for finite argument.

Definition at line 81 of file tanh.c.

References expm1(), fabs(), one, tiny, and two.

Referenced by RealNumber::HypTangent().

82 {
83  double t, z;
84  int32_t jx, ix;
85 
86  /* High word of |x|. */
87  GET_HIGH_WORD(jx, x);
88  ix = jx & 0x7FFFFFFF;
89 
90  /* x is INF or NaN */
91  if (ix >= 0x7FF00000)
92  {
93  if (jx >= 0)
94  return one / x + one; /* tanh(+-inf)=+-1 */
95  else
96  return NAN; /* tanh(NaN) = NaN */
97  }
98 
99  /* |x| < 22 */
100  if (ix < 0x40360000)
101  { /* |x|<22 */
102  if (ix < 0x3C800000) /* |x|<2**-55 */
103  return x * (one + x); /* tanh(small) = small */
104  if (ix >= 0x3FF00000)
105  { /* |x|>=1 */
106  t = expm1(two * fabs(x));
107  z = one - two / (t + two);
108  }
109  else
110  {
111  t = expm1(-two * fabs(x));
112  z = -t / (t + two);
113  }
114  }
115  /* |x| > 22, return +-1 */
116  else
117  {
118  z = one - tiny; /* raised inexact flag */
119  }
120 
121  return (jx >= 0) ? z : -z;
122 }
static const double tiny
Definition: tanh.c:51
#define GET_HIGH_WORD(i, d)
Get the more significant 32 bit int from a double.
Definition: prim.h:167
static const double one
Definition: tanh.c:49
#define NAN
Definition: mathr.h:53
double expm1(double x)
Definition: expm1.c:153
static const double two
Definition: tanh.c:50
double fabs(double x)
Returns the absolute value of x.
Definition: fabs.c:51
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ one

const double one = 1.0
static

Definition at line 49 of file tanh.c.

Referenced by tanh().

◆ tiny

const double tiny = 1.0e-300
static

Definition at line 51 of file tanh.c.

Referenced by tanh().

◆ two

const double two = 2.0
static

Definition at line 50 of file tanh.c.

Referenced by tanh().