Back to index

lightning-sunbird  0.9+nobinonly
e_cosh.c
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Mozilla Communicator client code, released
00017  * March 31, 1998.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Sun Microsystems, Inc.
00021  * Portions created by the Initial Developer are Copyright (C) 1998
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 /* @(#)e_cosh.c 1.3 95/01/18 */
00041 /*
00042  * ====================================================
00043  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
00044  *
00045  * Developed at SunSoft, a Sun Microsystems, Inc. business.
00046  * Permission to use, copy, modify, and distribute this
00047  * software is freely granted, provided that this notice 
00048  * is preserved.
00049  * ====================================================
00050  */
00051 
00052 /* __ieee754_cosh(x)
00053  * Method : 
00054  * mathematically cosh(x) if defined to be (exp(x)+exp(-x))/2
00055  *     1. Replace x by |x| (cosh(x) = cosh(-x)). 
00056  *     2. 
00057  *                                                    [ exp(x) - 1 ]^2 
00058  *         0        <= x <= ln2/2  :  cosh(x) := 1 + -------------------
00059  *                                                          2*exp(x)
00060  *
00061  *                                              exp(x) +  1/exp(x)
00062  *         ln2/2    <= x <= 22     :  cosh(x) := -------------------
00063  *                                                         2
00064  *         22       <= x <= lnovft :  cosh(x) := exp(x)/2 
00065  *         lnovft   <= x <= ln2ovft:  cosh(x) := exp(x/2)/2 * exp(x/2)
00066  *         ln2ovft  <  x        :  cosh(x) := huge*huge (overflow)
00067  *
00068  * Special cases:
00069  *     cosh(x) is |x| if x is +INF, -INF, or NaN.
00070  *     only cosh(0)=1 is exact for finite x.
00071  */
00072 
00073 #include "fdlibm.h"
00074 
00075 #ifdef _WIN32
00076 #define huge myhuge
00077 #endif
00078 
00079 #ifdef __STDC__
00080 static const double one = 1.0, half=0.5, really_big = 1.0e300;
00081 #else
00082 static double one = 1.0, half=0.5, really_big = 1.0e300;
00083 #endif
00084 
00085 #ifdef __STDC__
00086        double __ieee754_cosh(double x)
00087 #else
00088        double __ieee754_cosh(x)
00089        double x;
00090 #endif
00091 {      
00092         fd_twoints u;
00093        double t,w;
00094        int ix;
00095        unsigned lx;
00096         
00097     /* High word of |x|. */
00098         u.d = x;
00099        ix = __HI(u);
00100        ix &= 0x7fffffff;
00101 
00102     /* x is INF or NaN */
00103        if(ix>=0x7ff00000) return x*x;     
00104 
00105     /* |x| in [0,0.5*ln2], return 1+expm1(|x|)^2/(2*exp(|x|)) */
00106        if(ix<0x3fd62e43) {
00107            t = fd_expm1(fd_fabs(x));
00108            w = one+t;
00109            if (ix<0x3c800000) return w;   /* cosh(tiny) = 1 */
00110            return one+(t*t)/(w+w);
00111        }
00112 
00113     /* |x| in [0.5*ln2,22], return (exp(|x|)+1/exp(|x|)/2; */
00114        if (ix < 0x40360000) {
00115               t = __ieee754_exp(fd_fabs(x));
00116               return half*t+half/t;
00117        }
00118 
00119     /* |x| in [22, log(maxdouble)] return half*exp(|x|) */
00120        if (ix < 0x40862E42)  return half*__ieee754_exp(fd_fabs(x));
00121 
00122     /* |x| in [log(maxdouble), overflowthresold] */
00123        lx = *( (((*(unsigned*)&one)>>29)) + (unsigned*)&x);
00124        if (ix<0x408633CE || 
00125              (ix==0x408633ce)&&(lx<=(unsigned)0x8fb9f87d)) {
00126            w = __ieee754_exp(half*fd_fabs(x));
00127            t = half*w;
00128            return t*w;
00129        }
00130 
00131     /* |x| > overflowthresold, cosh(x) overflow */
00132        return really_big*really_big;
00133 }