Back to index

glibc  2.9
e_coshl.c
Go to the documentation of this file.
00001 /*
00002  * ====================================================
00003  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
00004  *
00005  * Developed at SunPro, a Sun Microsystems, Inc. business.
00006  * Permission to use, copy, modify, and distribute this
00007  * software is freely granted, provided that this notice
00008  * is preserved.
00009  * ====================================================
00010  */
00011 
00012 #if defined(LIBM_SCCS) && !defined(lint)
00013 static char rcsid[] = "$NetBSD: e_cosh.c,v 1.7 1995/05/10 20:44:58 jtc Exp $";
00014 #endif
00015 
00016 /* __ieee754_coshl(x)
00017  * Method :
00018  * mathematically coshl(x) if defined to be (exp(x)+exp(-x))/2
00019  *     1. Replace x by |x| (coshl(x) = coshl(-x)).
00020  *     2.
00021  *                                                    [ exp(x) - 1 ]^2
00022  *         0        <= x <= ln2/2  :  coshl(x) := 1 + -------------------
00023  *                                                          2*exp(x)
00024  *
00025  *                                               exp(x) +  1/exp(x)
00026  *         ln2/2    <= x <= 22     :  coshl(x) := -------------------
00027  *                                                          2
00028  *         22       <= x <= lnovft :  coshl(x) := expl(x)/2
00029  *         lnovft   <= x <= ln2ovft:  coshl(x) := expl(x/2)/2 * expl(x/2)
00030  *         ln2ovft  <  x        :  coshl(x) := huge*huge (overflow)
00031  *
00032  * Special cases:
00033  *     coshl(x) is |x| if x is +INF, -INF, or NaN.
00034  *     only coshl(0)=1 is exact for finite x.
00035  */
00036 
00037 #include "math.h"
00038 #include "math_private.h"
00039 
00040 #ifdef __STDC__
00041 static const long double one = 1.0, half=0.5, huge = 1.0e4900L;
00042 #else
00043 static long double one = 1.0, half=0.5, huge = 1.0e4900L;
00044 #endif
00045 
00046 #ifdef __STDC__
00047        long double __ieee754_coshl(long double x)
00048 #else
00049        long double __ieee754_coshl(x)
00050        long double x;
00051 #endif
00052 {
00053        long double t,w;
00054        int32_t ex;
00055        u_int32_t mx,lx;
00056 
00057     /* High word of |x|. */
00058        GET_LDOUBLE_WORDS(ex,mx,lx,x);
00059        ex &= 0x7fff;
00060 
00061     /* x is INF or NaN */
00062        if(ex==0x7fff) return x*x;
00063 
00064     /* |x| in [0,0.5*ln2], return 1+expm1l(|x|)^2/(2*expl(|x|)) */
00065        if(ex < 0x3ffd || (ex == 0x3ffd && mx < 0xb17217f7u)) {
00066            t = __expm1l(fabsl(x));
00067            w = one+t;
00068            if (ex<0x3fbc) return w;       /* cosh(tiny) = 1 */
00069            return one+(t*t)/(w+w);
00070        }
00071 
00072     /* |x| in [0.5*ln2,22], return (exp(|x|)+1/exp(|x|)/2; */
00073        if (ex < 0x4003 || (ex == 0x4003 && mx < 0xb0000000u)) {
00074               t = __ieee754_expl(fabsl(x));
00075               return half*t+half/t;
00076        }
00077 
00078     /* |x| in [22, ln(maxdouble)] return half*exp(|x|) */
00079        if (ex < 0x400c || (ex == 0x400c && mx < 0xb1700000u))
00080               return half*__ieee754_expl(fabsl(x));
00081 
00082     /* |x| in [log(maxdouble), log(2*maxdouble)) */
00083        if (ex == 0x400c && (mx < 0xb174ddc0u
00084                           || (mx == 0xb174ddc0u && lx < 0x31aec0ebu)))
00085        {
00086            w = __ieee754_expl(half*fabsl(x));
00087            t = half*w;
00088            return t*w;
00089        }
00090 
00091     /* |x| >= log(2*maxdouble), cosh(x) overflow */
00092        return huge*huge;
00093 }