source: trunk/libs/newlib/src/newlib/libm/mathfp/s_tanh.c @ 471

Last change on this file since 471 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 2.3 KB
Line 
1
2/* @(#)z_tanh.c 1.0 98/08/13 */
3/*****************************************************************
4 * The following routines are coded directly from the algorithms
5 * and coefficients given in "Software Manual for the Elementary
6 * Functions" by William J. Cody, Jr. and William Waite, Prentice
7 * Hall, 1980.
8 *****************************************************************/
9
10/*
11
12FUNCTION
13        <<tanh>>, <<tanhf>>---hyperbolic tangent
14
15INDEX
16tanh
17INDEX
18tanhf
19
20SYNOPSIS
21        #include <math.h>
22        double tanh(double <[x]>);
23        float tanhf(float <[x]>);
24
25DESCRIPTION
26
27<<tanh>> computes the hyperbolic tangent of
28the argument <[x]>.  Angles are specified in radians.
29
30<<tanh(<[x]>)>> is defined as
31. sinh(<[x]>)/cosh(<[x]>)
32
33<<tanhf>> is identical, save that it takes and returns <<float>> values.
34
35RETURNS
36The hyperbolic tangent of <[x]> is returned.
37
38PORTABILITY
39<<tanh>> is ANSI C.  <<tanhf>> is an extension.
40
41*/
42
43/******************************************************************
44 * Hyperbolic Tangent
45 *
46 * Input:
47 *   x - floating point value
48 *
49 * Output:
50 *   hyperbolic tangent of x
51 *
52 * Description:
53 *   This routine calculates hyperbolic tangent.
54 *
55 *****************************************************************/
56
57#include <float.h>
58#include "fdlibm.h"
59#include "zmath.h"
60
61#ifndef _DOUBLE_IS_32BITS
62
63static const double LN3_OVER2 = 0.54930614433405484570;
64static const double p[] = { -0.16134119023996228053e+4,
65                            -0.99225929672236083313e+2,
66                            -0.96437492777225469787 };
67static const double q[] = { 0.48402357071988688686e+4,
68                            0.22337720718962312926e+4,
69                            0.11274474380534949335e+3 }; 
70
71double
72tanh (double x)
73{
74  double f, res, g, P, Q, R;
75
76  f = fabs (x);
77
78  /* Check if the input is too big. */ 
79  if (f > BIGX)
80    res = 1.0; 
81
82  else if (f > LN3_OVER2)
83    res = 1.0 - 2.0 / (exp (2 * f) + 1.0);
84
85  /* Check if the input is too small. */
86  else if (f < z_rooteps)
87    res = f;
88
89  /* Calculate the Taylor series. */
90  else
91    {
92      g = f * f;
93
94      P = (p[2] * g + p[1]) * g + p[0];
95      Q = ((g + q[2]) * g + q[1]) * g + q[0];
96      R = g * (P / Q);
97
98      res = f + f * R;
99    }
100
101  if (x < 0.0)
102    res = -res;
103
104  return (res);
105}
106
107#endif /* _DOUBLE_IS_32BITS */
Note: See TracBrowser for help on using the repository browser.