]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/LibC/Math/s_tanh.c
Standard Libraries for EDK II.
[mirror_edk2.git] / StdLib / LibC / Math / s_tanh.c
CommitLineData
2aa62f2b 1/* @(#)s_tanh.c 5.1 93/09/24 */\r
2/*\r
3 * ====================================================\r
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.\r
5 *\r
6 * Developed at SunPro, a Sun Microsystems, Inc. business.\r
7 * Permission to use, copy, modify, and distribute this\r
8 * software is freely granted, provided that this notice\r
9 * is preserved.\r
10 * ====================================================\r
11 */\r
12#include <LibConfig.h>\r
13#include <sys/EfiCdefs.h>\r
14#if defined(LIBM_SCCS) && !defined(lint)\r
15__RCSID("$NetBSD: s_tanh.c,v 1.10 2002/05/26 22:01:59 wiz Exp $");\r
16#endif\r
17\r
18/* Tanh(x)\r
19 * Return the Hyperbolic Tangent of x\r
20 *\r
21 * Method :\r
22 * x -x\r
23 * e - e\r
24 * 0. tanh(x) is defined to be -----------\r
25 * x -x\r
26 * e + e\r
27 * 1. reduce x to non-negative by tanh(-x) = -tanh(x).\r
28 * 2. 0 <= x <= 2**-55 : tanh(x) := x*(one+x)\r
29 * -t\r
30 * 2**-55 < x <= 1 : tanh(x) := -----; t = expm1(-2x)\r
31 * t + 2\r
32 * 2\r
33 * 1 <= x <= 22.0 : tanh(x) := 1- ----- ; t=expm1(2x)\r
34 * t + 2\r
35 * 22.0 < x <= INF : tanh(x) := 1.\r
36 *\r
37 * Special cases:\r
38 * tanh(NaN) is NaN;\r
39 * only tanh(0)=0 is exact for finite argument.\r
40 */\r
41\r
42#include "math.h"\r
43#include "math_private.h"\r
44\r
45static const double one=1.0, two=2.0, tiny = 1.0e-300;\r
46\r
47double\r
48tanh(double x)\r
49{\r
50 double t,z;\r
51 int32_t jx,ix;\r
52\r
53 /* High word of |x|. */\r
54 GET_HIGH_WORD(jx,x);\r
55 ix = jx&0x7fffffff;\r
56\r
57 /* x is INF or NaN */\r
58 if(ix>=0x7ff00000) {\r
59 if (jx>=0) return one/x+one; /* tanh(+-inf)=+-1 */\r
60 else return one/x-one; /* tanh(NaN) = NaN */\r
61 }\r
62\r
63 /* |x| < 22 */\r
64 if (ix < 0x40360000) { /* |x|<22 */\r
65 if (ix<0x3c800000) /* |x|<2**-55 */\r
66 return x*(one+x); /* tanh(small) = small */\r
67 if (ix>=0x3ff00000) { /* |x|>=1 */\r
68 t = expm1(two*fabs(x));\r
69 z = one - two/(t+two);\r
70 } else {\r
71 t = expm1(-two*fabs(x));\r
72 z= -t/(t+two);\r
73 }\r
74 /* |x| > 22, return +-1 */\r
75 } else {\r
76 z = one - tiny; /* raised inexact flag */\r
77 }\r
78 return (jx>=0)? z: -z;\r
79}\r