Back to index

glibc  2.9
s_tanh.c
Go to the documentation of this file.
00001 /* @(#)s_tanh.c 5.1 93/09/24 */
00002 /*
00003  * ====================================================
00004  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
00005  *
00006  * Developed at SunPro, a Sun Microsystems, Inc. business.
00007  * Permission to use, copy, modify, and distribute this
00008  * software is freely granted, provided that this notice
00009  * is preserved.
00010  * ====================================================
00011  */
00012 
00013 #if defined(LIBM_SCCS) && !defined(lint)
00014 static char rcsid[] = "$NetBSD: s_tanh.c,v 1.7 1995/05/10 20:48:22 jtc Exp $";
00015 #endif
00016 
00017 /* Tanh(x)
00018  * Return the Hyperbolic Tangent of x
00019  *
00020  * Method :
00021  *                                 x    -x
00022  *                                e  - e
00023  *     0. tanh(x) is defined to be -----------
00024  *                                 x    -x
00025  *                                e  + e
00026  *     1. reduce x to non-negative by tanh(-x) = -tanh(x).
00027  *     2.  0      <= x <= 2**-55 : tanh(x) := x*(one+x)
00028  *                                         -t
00029  *         2**-55 <  x <=  1     : tanh(x) := -----; t = expm1(-2x)
00030  *                                        t + 2
00031  *                                             2
00032  *         1      <= x <=  22.0  : tanh(x) := 1-  ----- ; t=expm1(2x)
00033  *                                           t + 2
00034  *         22.0   <  x <= INF    : tanh(x) := 1.
00035  *
00036  * Special cases:
00037  *     tanh(NaN) is NaN;
00038  *     only tanh(0)=0 is exact for finite argument.
00039  */
00040 
00041 #include "math.h"
00042 #include "math_private.h"
00043 
00044 #ifdef __STDC__
00045 static const double one=1.0, two=2.0, tiny = 1.0e-300;
00046 #else
00047 static double one=1.0, two=2.0, tiny = 1.0e-300;
00048 #endif
00049 
00050 #ifdef __STDC__
00051        double __tanh(double x)
00052 #else
00053        double __tanh(x)
00054        double x;
00055 #endif
00056 {
00057        double t,z;
00058        int32_t jx,ix,lx;
00059 
00060     /* High word of |x|. */
00061        EXTRACT_WORDS(jx,lx,x);
00062        ix = jx&0x7fffffff;
00063 
00064     /* x is INF or NaN */
00065        if(ix>=0x7ff00000) {
00066            if (jx>=0) return one/x+one;    /* tanh(+-inf)=+-1 */
00067            else       return one/x-one;    /* tanh(NaN) = NaN */
00068        }
00069 
00070     /* |x| < 22 */
00071        if (ix < 0x40360000) {             /* |x|<22 */
00072            if ((ix | lx) == 0)
00073               return x;            /* x == +-0 */
00074            if (ix<0x3c800000)             /* |x|<2**-55 */
00075               return x*(one+x);           /* tanh(small) = small */
00076            if (ix>=0x3ff00000) {   /* |x|>=1  */
00077               t = __expm1(two*fabs(x));
00078               z = one - two/(t+two);
00079            } else {
00080                t = __expm1(-two*fabs(x));
00081                z= -t/(t+two);
00082            }
00083     /* |x| > 22, return +-1 */
00084        } else {
00085            z = one - tiny;         /* raised inexact flag */
00086        }
00087        return (jx>=0)? z: -z;
00088 }
00089 weak_alias (__tanh, tanh)
00090 #ifdef NO_LONG_DOUBLE
00091 strong_alias (__tanh, __tanhl)
00092 weak_alias (__tanh, tanhl)
00093 #endif