Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

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