xref: /csrg-svn/lib/libm/common_source/cosh.c (revision 34124)
1*34124Sbostic /*
224591Szliu  * Copyright (c) 1985 Regents of the University of California.
3*34124Sbostic  * All rights reserved.
4*34124Sbostic  *
5*34124Sbostic  * Redistribution and use in source and binary forms are permitted
6*34124Sbostic  * provided that this notice is preserved and that due credit is given
7*34124Sbostic  * to the University of California at Berkeley. The name of the University
8*34124Sbostic  * may not be used to endorse or promote products derived from this
9*34124Sbostic  * software without specific prior written permission. This software
10*34124Sbostic  * is provided ``as is'' without express or implied warranty.
11*34124Sbostic  *
12*34124Sbostic  * All recipients should regard themselves as participants in an ongoing
13*34124Sbostic  * research project and hence should feel obligated to report their
14*34124Sbostic  * experiences (good or bad) with these elementary function codes, using
15*34124Sbostic  * the sendbug(8) program, to the authors.
1624591Szliu  */
1724591Szliu 
1824591Szliu #ifndef lint
19*34124Sbostic static char sccsid[] = "@(#)cosh.c	5.2 (Berkeley) 04/29/88";
20*34124Sbostic #endif /* not lint */
2124591Szliu 
2224591Szliu /* COSH(X)
2324591Szliu  * RETURN THE HYPERBOLIC COSINE OF X
2424591Szliu  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
2524591Szliu  * CODED IN C BY K.C. NG, 1/8/85;
2624591Szliu  * REVISED BY K.C. NG on 2/8/85, 2/23/85, 3/7/85, 3/29/85, 4/16/85.
2724591Szliu  *
2824591Szliu  * Required system supported functions :
2924591Szliu  *	copysign(x,y)
3024591Szliu  *	scalb(x,N)
3124591Szliu  *
3224591Szliu  * Required kernel function:
3324591Szliu  *	exp(x)
3424591Szliu  *	exp__E(x,c)	...return exp(x+c)-1-x for |x|<0.3465
3524591Szliu  *
3624591Szliu  * Method :
3724591Szliu  *	1. Replace x by |x|.
3824591Szliu  *	2.
3924591Szliu  *		                                        [ exp(x) - 1 ]^2
4024591Szliu  *	    0        <= x <= 0.3465  :  cosh(x) := 1 + -------------------
4124591Szliu  *			       			           2*exp(x)
4224591Szliu  *
4324591Szliu  *		                                   exp(x) +  1/exp(x)
4424591Szliu  *	    0.3465   <= x <= 22      :  cosh(x) := -------------------
4524591Szliu  *			       			           2
4624591Szliu  *	    22       <= x <= lnovfl  :  cosh(x) := exp(x)/2
4724591Szliu  *	    lnovfl   <= x <= lnovfl+log(2)
4824591Szliu  *				     :  cosh(x) := exp(x)/2 (avoid overflow)
4924591Szliu  *	    log(2)+lnovfl <  x <  INF:  overflow to INF
5024591Szliu  *
5124591Szliu  *	Note: .3465 is a number near one half of ln2.
5224591Szliu  *
5324591Szliu  * Special cases:
5424591Szliu  *	cosh(x) is x if x is +INF, -INF, or NaN.
5524591Szliu  *	only cosh(0)=1 is exact for finite x.
5624591Szliu  *
5724591Szliu  * Accuracy:
5824591Szliu  *	cosh(x) returns the exact hyperbolic cosine of x nearly rounded.
5924591Szliu  *	In a test run with 768,000 random arguments on a VAX, the maximum
6024591Szliu  *	observed error was 1.23 ulps (units in the last place).
6124591Szliu  *
6224591Szliu  * Constants:
6324591Szliu  * The hexadecimal values are the intended ones for the following constants.
6424591Szliu  * The decimal values may be used, provided that the compiler will convert
6524591Szliu  * from decimal to binary accurately enough to produce the hexadecimal values
6624591Szliu  * shown.
6724591Szliu  */
6824591Szliu 
6931853Szliu #if defined(vax)||defined(tahoe)
7031853Szliu #ifdef vax
7131812Szliu #define _0x(A,B)	0x/**/A/**/B
7231853Szliu #else	/* vax */
7331812Szliu #define _0x(A,B)	0x/**/B/**/A
7431853Szliu #endif	/* vax */
7526893Selefunt /* static double  */
7624591Szliu /* mln2hi =  8.8029691931113054792E1     , Hex  2^  7   *  .B00F33C7E22BDB */
7724591Szliu /* mln2lo = -4.9650192275318476525E-16   , Hex  2^-50   * -.8F1B60279E582A */
7824591Szliu /* lnovfl =  8.8029691931113053016E1     ; Hex  2^  7   *  .B00F33C7E22BDA */
7931812Szliu static long    mln2hix[] = { _0x(0f33,43b0), _0x(2bdb,c7e2)};
8031812Szliu static long    mln2lox[] = { _0x(1b60,a70f), _0x(582a,279e)};
8131812Szliu static long    lnovflx[] = { _0x(0f33,43b0), _0x(2bda,c7e2)};
8224591Szliu #define   mln2hi    (*(double*)mln2hix)
8324591Szliu #define   mln2lo    (*(double*)mln2lox)
8424591Szliu #define   lnovfl    (*(double*)lnovflx)
8531853Szliu #else	/* defined(vax)||defined(tahoe) */
8626893Selefunt static double
8724591Szliu mln2hi =  7.0978271289338397310E2     , /*Hex  2^ 10   *  1.62E42FEFA39EF */
8824591Szliu mln2lo =  2.3747039373786107478E-14   , /*Hex  2^-45   *  1.ABC9E3B39803F */
8924591Szliu lnovfl =  7.0978271289338397310E2     ; /*Hex  2^  9   *  1.62E42FEFA39EF */
9031853Szliu #endif	/* defined(vax)||defined(tahoe) */
9124591Szliu 
9231853Szliu #if defined(vax)||defined(tahoe)
9324591Szliu static max = 126                      ;
9431853Szliu #else	/* defined(vax)||defined(tahoe) */
9524591Szliu static max = 1023                     ;
9631853Szliu #endif	/* defined(vax)||defined(tahoe) */
9724591Szliu 
9824591Szliu double cosh(x)
9924591Szliu double x;
10024591Szliu {
10124591Szliu 	static double half=1.0/2.0,one=1.0, small=1.0E-18; /* fl(1+small)==1 */
10224591Szliu 	double scalb(),copysign(),exp(),exp__E(),t;
10324591Szliu 
10431853Szliu #if !defined(vax)&&!defined(tahoe)
10524591Szliu 	if(x!=x) return(x);	/* x is NaN */
10631853Szliu #endif	/* !defined(vax)&&!defined(tahoe) */
10724591Szliu 	if((x=copysign(x,one)) <= 22)
10824591Szliu 	    if(x<0.3465)
10924591Szliu 		if(x<small) return(one+x);
11024591Szliu 		else {t=x+exp__E(x,0.0);x=t+t; return(one+t*t/(2.0+x)); }
11124591Szliu 
11224591Szliu 	    else /* for x lies in [0.3465,22] */
11324591Szliu 	        { t=exp(x); return((t+one/t)*half); }
11424591Szliu 
11524591Szliu 	if( lnovfl <= x && x <= (lnovfl+0.7))
11624591Szliu         /* for x lies in [lnovfl, lnovfl+ln2], decrease x by ln(2^(max+1))
11724591Szliu          * and return 2^max*exp(x) to avoid unnecessary overflow
11824591Szliu          */
11924591Szliu 	    return(scalb(exp((x-mln2hi)-mln2lo), max));
12024591Szliu 
12124591Szliu 	else
12224591Szliu 	    return(exp(x)*half);	/* for large x,  cosh(x)=exp(x)/2 */
12324591Szliu }
124