134124Sbostic /*
2*61285Sbostic * Copyright (c) 1985, 1993
3*61285Sbostic * The Regents of the University of California. All rights reserved.
434124Sbostic *
542657Sbostic * %sccs.include.redist.c%
624591Szliu */
724591Szliu
824591Szliu #ifndef lint
9*61285Sbostic static char sccsid[] = "@(#)cosh.c 8.1 (Berkeley) 06/04/93";
1034124Sbostic #endif /* not lint */
1124591Szliu
1224591Szliu /* COSH(X)
1324591Szliu * RETURN THE HYPERBOLIC COSINE OF X
1424591Szliu * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
1524591Szliu * CODED IN C BY K.C. NG, 1/8/85;
1624591Szliu * REVISED BY K.C. NG on 2/8/85, 2/23/85, 3/7/85, 3/29/85, 4/16/85.
1724591Szliu *
1824591Szliu * Required system supported functions :
1924591Szliu * copysign(x,y)
2024591Szliu * scalb(x,N)
2124591Szliu *
2224591Szliu * Required kernel function:
2324591Szliu * exp(x)
2424591Szliu * exp__E(x,c) ...return exp(x+c)-1-x for |x|<0.3465
2524591Szliu *
2624591Szliu * Method :
2724591Szliu * 1. Replace x by |x|.
2824591Szliu * 2.
2924591Szliu * [ exp(x) - 1 ]^2
3024591Szliu * 0 <= x <= 0.3465 : cosh(x) := 1 + -------------------
3124591Szliu * 2*exp(x)
3224591Szliu *
3324591Szliu * exp(x) + 1/exp(x)
3424591Szliu * 0.3465 <= x <= 22 : cosh(x) := -------------------
3524591Szliu * 2
3624591Szliu * 22 <= x <= lnovfl : cosh(x) := exp(x)/2
3724591Szliu * lnovfl <= x <= lnovfl+log(2)
3824591Szliu * : cosh(x) := exp(x)/2 (avoid overflow)
3924591Szliu * log(2)+lnovfl < x < INF: overflow to INF
4024591Szliu *
4124591Szliu * Note: .3465 is a number near one half of ln2.
4224591Szliu *
4324591Szliu * Special cases:
4424591Szliu * cosh(x) is x if x is +INF, -INF, or NaN.
4524591Szliu * only cosh(0)=1 is exact for finite x.
4624591Szliu *
4724591Szliu * Accuracy:
4824591Szliu * cosh(x) returns the exact hyperbolic cosine of x nearly rounded.
4924591Szliu * In a test run with 768,000 random arguments on a VAX, the maximum
5024591Szliu * observed error was 1.23 ulps (units in the last place).
5124591Szliu *
5224591Szliu * Constants:
5324591Szliu * The hexadecimal values are the intended ones for the following constants.
5424591Szliu * The decimal values may be used, provided that the compiler will convert
5524591Szliu * from decimal to binary accurately enough to produce the hexadecimal values
5624591Szliu * shown.
5724591Szliu */
5824591Szliu
5935679Sbostic #include "mathimpl.h"
6024591Szliu
6135679Sbostic vc(mln2hi, 8.8029691931113054792E1 ,0f33,43b0,2bdb,c7e2, 7, .B00F33C7E22BDB)
6235679Sbostic vc(mln2lo,-4.9650192275318476525E-16 ,1b60,a70f,582a,279e, -50,-.8F1B60279E582A)
6335679Sbostic vc(lnovfl, 8.8029691931113053016E1 ,0f33,43b0,2bda,c7e2, 7, .B00F33C7E22BDA)
6435679Sbostic
6535679Sbostic ic(mln2hi, 7.0978271289338397310E2, 10, 1.62E42FEFA39EF)
6635679Sbostic ic(mln2lo, 2.3747039373786107478E-14, -45, 1.ABC9E3B39803F)
6735679Sbostic ic(lnovfl, 7.0978271289338397310E2, 9, 1.62E42FEFA39EF)
6835679Sbostic
6935679Sbostic #ifdef vccast
7035679Sbostic #define mln2hi vccast(mln2hi)
7135679Sbostic #define mln2lo vccast(mln2lo)
7235679Sbostic #define lnovfl vccast(lnovfl)
7335679Sbostic #endif
7435679Sbostic
7531853Szliu #if defined(vax)||defined(tahoe)
7624591Szliu static max = 126 ;
7731853Szliu #else /* defined(vax)||defined(tahoe) */
7824591Szliu static max = 1023 ;
7931853Szliu #endif /* defined(vax)||defined(tahoe) */
8024591Szliu
cosh(x)8124591Szliu double cosh(x)
8224591Szliu double x;
8324591Szliu {
8435679Sbostic static const double half=1.0/2.0,
8535679Sbostic one=1.0, small=1.0E-18; /* fl(1+small)==1 */
8635679Sbostic double t;
8724591Szliu
8831853Szliu #if !defined(vax)&&!defined(tahoe)
8924591Szliu if(x!=x) return(x); /* x is NaN */
9031853Szliu #endif /* !defined(vax)&&!defined(tahoe) */
9124591Szliu if((x=copysign(x,one)) <= 22)
9224591Szliu if(x<0.3465)
9324591Szliu if(x<small) return(one+x);
9457452Sbostic else {t=x+__exp__E(x,0.0);x=t+t; return(one+t*t/(2.0+x)); }
9524591Szliu
9624591Szliu else /* for x lies in [0.3465,22] */
9724591Szliu { t=exp(x); return((t+one/t)*half); }
9824591Szliu
9924591Szliu if( lnovfl <= x && x <= (lnovfl+0.7))
10024591Szliu /* for x lies in [lnovfl, lnovfl+ln2], decrease x by ln(2^(max+1))
10124591Szliu * and return 2^max*exp(x) to avoid unnecessary overflow
10224591Szliu */
10324591Szliu return(scalb(exp((x-mln2hi)-mln2lo), max));
10424591Szliu
10524591Szliu else
10624591Szliu return(exp(x)*half); /* for large x, cosh(x)=exp(x)/2 */
10724591Szliu }
108