amath  1.8.5
Simple command line calculator
cosh.c File Reference

Hyperbolic cosine function. More...

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

Go to the source code of this file.

Functions

double cosh (double x)
 Hyperbolic cosine function. More...
 

Variables

static const double one = 1.0
 
static const double half = 0.5
 
static const double huge = 1.0e300
 

Detailed Description

Hyperbolic cosine function.

Definition in file cosh.c.

Function Documentation

◆ cosh()

double cosh ( double  x)

Hyperbolic cosine function.

Mathematically cosh(x) if defined to be (exp(x)+exp(-x))/2

Method
1. Replace x by |x| (cosh(x) = cosh(-x))
2.
                                              [ exp(x) - 1 ]^2
   0        <= x <= ln2/2  :  cosh(x) := 1 + -------------------
                                                2*exp(x)

                                         exp(x) +  1/exp(x)
   ln2/2    <= x <= 22     :  cosh(x) := -------------------
                                                 2
   22       <= x <= lnovft :  cosh(x) := exp(x)/2
   lnovft   <= x <= ln2ovft:  cosh(x) := exp(x/2)/2 * exp(x/2)
   ln2ovft  <  x           :  cosh(x) := huge*huge (overflow)
Special cases:
cosh(x) is |x| if x is +INF, -INF, or NaN
only cosh(0)=1 is exact for finite x

Definition at line 83 of file cosh.c.

References exp(), expm1(), fabs(), half, huge, and one.

Referenced by cchc(), cchsh(), ccot(), ccoth(), ccsc(), ccsch(), coth(), csec(), csech(), ctan(), ctanh(), RealNumber::HypCosine(), and sech().

84 {
85  double t, w;
86  int32_t ix;
87  uint32_t lx;
88 
89  // High word of |x|
90  GET_HIGH_WORD(ix, x);
91  ix &= 0x7FFFFFFF;
92 
93  // x is INF or NaN
94  if (ix >= 0x7FF00000)
95  {
96  return x * x;
97  }
98 
99  // |x| in [0,0.5*ln2]
100  if (ix < 0x3FD62E43)
101  {
102  t = expm1(fabs(x));
103  w = one + t;
104  if (ix < 0x3C800000)
105  {
106  // cosh(tiny) = 1
107  return w;
108  }
109 
110  return one + (t * t) / (w + w);
111  }
112 
113  // |x| in [0.5*ln2,22]
114  if (ix < 0x40360000)
115  {
116  t = exp(fabs(x));
117  return half * t + half / t;
118  }
119 
120  // |x| in [22, log(maxdouble)]
121  if (ix < 0x40862E42)
122  {
123  return half * exp(fabs(x));
124  }
125 
126  // |x| in [log(maxdouble), overflowthresold]
127  lx = *((((*(unsigned *)&one) >> 29)) + (unsigned *)&x);
128  if (ix < 0x408633CE || ((ix == 0x408633CE) && (lx <= (unsigned)0x8FB9F87D)))
129  {
130  w = exp(half * fabs(x));
131  t = half * w;
132  return t * w;
133  }
134 
135  // |x| > overflowthresold, cosh(x) overflow
136  return huge * huge;
137 }
#define GET_HIGH_WORD(i, d)
Get the more significant 32 bit int from a double.
Definition: prim.h:167
double exp(double x)
Returns the exponential of x.
Definition: exp.c:138
static const double one
Definition: cosh.c:53
double expm1(double x)
Definition: expm1.c:153
double fabs(double x)
Returns the absolute value of x.
Definition: fabs.c:51
static const double huge
Definition: cosh.c:55
static const double half
Definition: cosh.c:54
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ half

const double half = 0.5
static

Definition at line 54 of file cosh.c.

Referenced by cosh().

◆ huge

const double huge = 1.0e300
static

Definition at line 55 of file cosh.c.

Referenced by cosh().

◆ one

const double one = 1.0
static

Definition at line 53 of file cosh.c.

Referenced by cosh().