Back to index

glibc  2.9
s_tanhl.c
Go to the documentation of this file.
00001 /* s_tanhl.c -- long double version of s_tanh.c.
00002  * Conversion to long double by Ulrich Drepper,
00003  * Cygnus Support, drepper@cygnus.com.
00004  */
00005 
00006 /*
00007  * ====================================================
00008  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
00009  *
00010  * Developed at SunPro, a Sun Microsystems, Inc. business.
00011  * Permission to use, copy, modify, and distribute this
00012  * software is freely granted, provided that this notice
00013  * is preserved.
00014  * ====================================================
00015  */
00016 
00017 #if defined(LIBM_SCCS) && !defined(lint)
00018 static char rcsid[] = "$NetBSD: $";
00019 #endif
00020 
00021 /* tanhl(x)
00022  * Return the Hyperbolic Tangent of x
00023  *
00024  * Method :
00025  *                                  x    -x
00026  *                                 e  - e
00027  *     0. tanhl(x) is defined to be -----------
00028  *                                  x    -x
00029  *                                 e  + e
00030  *     1. reduce x to non-negative by tanhl(-x) = -tanhl(x).
00031  *     2.  0      <= x <= 2**-55 : tanhl(x) := x*(one+x)
00032  *                                          -t
00033  *         2**-55 <  x <=  1     : tanhl(x) := -----; t = expm1l(-2x)
00034  *                                         t + 2
00035  *                                              2
00036  *         1      <= x <=  23.0  : tanhl(x) := 1-  ----- ; t=expm1l(2x)
00037  *                                            t + 2
00038  *         23.0   <  x <= INF    : tanhl(x) := 1.
00039  *
00040  * Special cases:
00041  *     tanhl(NaN) is NaN;
00042  *     only tanhl(0)=0 is exact for finite argument.
00043  */
00044 
00045 #include "math.h"
00046 #include "math_private.h"
00047 
00048 #ifdef __STDC__
00049 static const long double one=1.0, two=2.0, tiny = 1.0e-4900L;
00050 #else
00051 static long double one=1.0, two=2.0, tiny = 1.0e-4900L;
00052 #endif
00053 
00054 #ifdef __STDC__
00055        long double __tanhl(long double x)
00056 #else
00057        long double __tanhl(x)
00058        long double x;
00059 #endif
00060 {
00061        long double t,z;
00062        int32_t se;
00063        u_int32_t j0,j1,ix;
00064 
00065     /* High word of |x|. */
00066        GET_LDOUBLE_WORDS(se,j0,j1,x);
00067        ix = se&0x7fff;
00068 
00069     /* x is INF or NaN */
00070        if(ix==0x7fff) {
00071            /* for NaN it's not important which branch: tanhl(NaN) = NaN */
00072            if (se&0x8000) return one/x-one;      /* tanhl(-inf)= -1; */
00073            else           return one/x+one;      /* tanhl(+inf)=+1 */
00074        }
00075 
00076     /* |x| < 23 */
00077        if (ix < 0x4003 || (ix == 0x4003 && j0 < 0xb8000000u)) {/* |x|<23 */
00078            if ((ix|j0|j1) == 0)
00079               return x;            /* x == +- 0 */
00080            if (ix<0x3fc8)          /* |x|<2**-55 */
00081               return x*(one+tiny); /* tanh(small) = small */
00082            if (ix>=0x3fff) {       /* |x|>=1  */
00083               t = __expm1l(two*fabsl(x));
00084               z = one - two/(t+two);
00085            } else {
00086                t = __expm1l(-two*fabsl(x));
00087                z= -t/(t+two);
00088            }
00089     /* |x| > 23, return +-1 */
00090        } else {
00091            z = one - tiny;         /* raised inexact flag */
00092        }
00093        return (se&0x8000)? -z: z;
00094 }
00095 weak_alias (__tanhl, tanhl)