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 /* Changes for 128-bit long double contributed by
00018    Stephen L. Moshier <moshier@na-net.ornl.gov> */
00019 
00020 /* tanhl(x)
00021  * Return the Hyperbolic Tangent of x
00022  *
00023  * Method :
00024  *                                      x    -x
00025  *                                     e  - e
00026  *      0. tanhl(x) is defined to be -----------
00027  *                                      x    -x
00028  *                                     e  + e
00029  *      1. reduce x to non-negative by tanhl(-x) = -tanhl(x).
00030  *      2.  0      <= x <= 2**-57 : tanhl(x) := x*(one+x)
00031  *                                               -t
00032  *          2**-57 <  x <=  1     : tanhl(x) := -----; t = expm1l(-2x)
00033  *                                              t + 2
00034  *                                                    2
00035  *          1      <= x <=  40.0  : tanhl(x) := 1-  ----- ; t=expm1l(2x)
00036  *                                                  t + 2
00037  *          40.0   <  x <= INF    : tanhl(x) := 1.
00038  *
00039  * Special cases:
00040  *      tanhl(NaN) is NaN;
00041  *      only tanhl(0)=0 is exact for finite argument.
00042  */
00043 
00044 #include "math.h"
00045 #include "math_private.h"
00046 
00047 #ifdef __STDC__
00048 static const long double one = 1.0, two = 2.0, tiny = 1.0e-4900L;
00049 #else
00050 static long double one = 1.0, two = 2.0, tiny = 1.0e-4900L;
00051 #endif
00052 
00053 #ifdef __STDC__
00054 long double
00055 __tanhl (long double x)
00056 #else
00057 long double
00058 __tanhl (x)
00059      long double x;
00060 #endif
00061 {
00062   long double t, z;
00063   u_int32_t jx, ix;
00064   ieee854_long_double_shape_type u;
00065 
00066   /* Words of |x|. */
00067   u.value = x;
00068   jx = u.parts32.w0;
00069   ix = jx & 0x7fffffff;
00070   /* x is INF or NaN */
00071   if (ix >= 0x7fff0000)
00072     {
00073       /* for NaN it's not important which branch: tanhl(NaN) = NaN */
00074       if (jx & 0x80000000)
00075        return one / x - one;       /* tanhl(-inf)= -1; */
00076       else
00077        return one / x + one;       /* tanhl(+inf)=+1 */
00078     }
00079 
00080   /* |x| < 40 */
00081   if (ix < 0x40044000)
00082     {
00083       if (u.value == 0)
00084        return x;            /* x == +- 0 */
00085       if (ix < 0x3fc60000)  /* |x| < 2^-57 */
00086        return x * (one + tiny); /* tanh(small) = small */
00087       u.parts32.w0 = ix;    /* Absolute value of x.  */
00088       if (ix >= 0x3fff0000)
00089        {                    /* |x| >= 1  */
00090          t = __expm1l (two * u.value);
00091          z = one - two / (t + two);
00092        }
00093       else
00094        {
00095          t = __expm1l (-two * u.value);
00096          z = -t / (t + two);
00097        }
00098       /* |x| > 40, return +-1 */
00099     }
00100   else
00101     {
00102       z = one - tiny;              /* raised inexact flag */
00103     }
00104   return (jx & 0x80000000) ? -z : z;
00105 }
00106 weak_alias (__tanhl, tanhl)