home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 Mobile / Chip_Mobile_2001.iso / palm / hobby / palmoon / palmoon.EXE / e_cosh.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-16  |  2.7 KB  |  96 lines

  1. // 15 August 1997, Rick Huebner:  Small changes made to adapt for MathLib
  2.  
  3. /* @(#)e_cosh.c 5.1 93/09/24 */
  4. /*
  5.  * ====================================================
  6.  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  7.  *
  8.  * Developed at SunPro, a Sun Microsystems, Inc. business.
  9.  * Permission to use, copy, modify, and distribute this
  10.  * software is freely granted, provided that this notice 
  11.  * is preserved.
  12.  * ====================================================
  13.  */
  14.  
  15. #if defined(LIBM_SCCS) && !defined(lint)
  16. static char rcsid[] = "$NetBSD: e_cosh.c,v 1.7 1995/05/10 20:44:58 jtc Exp $";
  17. #endif
  18.  
  19. /* __ieee754_cosh(x)
  20.  * Method : 
  21.  * mathematically cosh(x) if defined to be (exp(x)+exp(-x))/2
  22.  *    1. Replace x by |x| (cosh(x) = cosh(-x)). 
  23.  *    2. 
  24.  *                                                [ exp(x) - 1 ]^2 
  25.  *        0        <= x <= ln2/2  :  cosh(x) := 1 + -------------------
  26.  *                                          2*exp(x)
  27.  *
  28.  *                                          exp(x) +  1/exp(x)
  29.  *        ln2/2    <= x <= 22     :  cosh(x) := -------------------
  30.  *                                         2
  31.  *        22       <= x <= lnovft :  cosh(x) := exp(x)/2 
  32.  *        lnovft   <= x <= ln2ovft:  cosh(x) := exp(x/2)/2 * exp(x/2)
  33.  *        ln2ovft  <  x        :  cosh(x) := huge*huge (overflow)
  34.  *
  35.  * Special cases:
  36.  *    cosh(x) is |x| if x is +INF, -INF, or NaN.
  37.  *    only cosh(0)=1 is exact for finite x.
  38.  */
  39.  
  40. #include "math.h"
  41. #include "math_private.h"
  42.  
  43. #ifdef __STDC__
  44. static const double one = 1.0, half=0.5, huge = 1.0e300;
  45. #else
  46. static double one = 1.0, half=0.5, huge = 1.0e300;
  47. #endif
  48.  
  49. #ifdef __STDC__
  50.     double __ieee754_cosh(double x)
  51. #else
  52.     double __ieee754_cosh(x)
  53.     double x;
  54. #endif
  55. {    
  56.     double t,w;
  57.     int32_t ix;
  58.     u_int32_t lx;
  59.  
  60.     /* High word of |x|. */
  61.     GET_HIGH_WORD(ix,x);
  62.     ix &= 0x7fffffff;
  63.  
  64.     /* x is INF or NaN */
  65.     if(ix>=0x7ff00000) return x*x;    
  66.  
  67.     /* |x| in [0,0.5*ln2], return 1+expm1(|x|)^2/(2*exp(|x|)) */
  68.     if(ix<0x3fd62e43) {
  69.         t = __expm1(__fabs(x));
  70.         w = one+t;
  71.         if (ix<0x3c800000) return w;    /* cosh(tiny) = 1 */
  72.         return one+(t*t)/(w+w);
  73.     }
  74.  
  75.     /* |x| in [0.5*ln2,22], return (exp(|x|)+1/exp(|x|)/2; */
  76.     if (ix < 0x40360000) {
  77.         t = __ieee754_exp(__fabs(x));
  78.         return half*t+half/t;
  79.     }
  80.  
  81.     /* |x| in [22, log(maxdouble)] return half*exp(|x|) */
  82.     if (ix < 0x40862E42)  return half*__ieee754_exp(__fabs(x));
  83.  
  84.     /* |x| in [log(maxdouble), overflowthresold] */
  85.     GET_LOW_WORD(lx,x);
  86.     if (ix<0x408633CE || 
  87.           (ix==0x408633ce)&&(lx<=(u_int32_t)0x8fb9f87d)) {
  88.         w = __ieee754_exp(half*__fabs(x));
  89.         t = half*w;
  90.         return t*w;
  91.     }
  92.  
  93.     /* |x| > overflowthresold, cosh(x) overflow */
  94.     return huge*huge;
  95. }
  96.