xref: /csrg-svn/lib/libm/common_source/cosh.c (revision 31853)
124591Szliu /*
224591Szliu  * Copyright (c) 1985 Regents of the University of California.
324591Szliu  *
424591Szliu  * Use and reproduction of this software are granted  in  accordance  with
524591Szliu  * the terms and conditions specified in  the  Berkeley  Software  License
624591Szliu  * Agreement (in particular, this entails acknowledgement of the programs'
724591Szliu  * source, and inclusion of this notice) with the additional understanding
824591Szliu  * that  all  recipients  should regard themselves as participants  in  an
924591Szliu  * ongoing  research  project and hence should  feel  obligated  to report
1024591Szliu  * their  experiences (good or bad) with these elementary function  codes,
1124591Szliu  * using "sendbug 4bsd-bugs@BERKELEY", to the authors.
1224591Szliu  */
1324591Szliu 
1424591Szliu #ifndef lint
1524706Selefunt static char sccsid[] =
16*31853Szliu "@(#)cosh.c	1.2 (Berkeley) 8/21/85; 1.6 (ucb.elefunt) 07/13/87";
17*31853Szliu #endif	/* not lint */
1824591Szliu 
1924591Szliu /* COSH(X)
2024591Szliu  * RETURN THE HYPERBOLIC COSINE OF X
2124591Szliu  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
2224591Szliu  * CODED IN C BY K.C. NG, 1/8/85;
2324591Szliu  * REVISED BY K.C. NG on 2/8/85, 2/23/85, 3/7/85, 3/29/85, 4/16/85.
2424591Szliu  *
2524591Szliu  * Required system supported functions :
2624591Szliu  *	copysign(x,y)
2724591Szliu  *	scalb(x,N)
2824591Szliu  *
2924591Szliu  * Required kernel function:
3024591Szliu  *	exp(x)
3124591Szliu  *	exp__E(x,c)	...return exp(x+c)-1-x for |x|<0.3465
3224591Szliu  *
3324591Szliu  * Method :
3424591Szliu  *	1. Replace x by |x|.
3524591Szliu  *	2.
3624591Szliu  *		                                        [ exp(x) - 1 ]^2
3724591Szliu  *	    0        <= x <= 0.3465  :  cosh(x) := 1 + -------------------
3824591Szliu  *			       			           2*exp(x)
3924591Szliu  *
4024591Szliu  *		                                   exp(x) +  1/exp(x)
4124591Szliu  *	    0.3465   <= x <= 22      :  cosh(x) := -------------------
4224591Szliu  *			       			           2
4324591Szliu  *	    22       <= x <= lnovfl  :  cosh(x) := exp(x)/2
4424591Szliu  *	    lnovfl   <= x <= lnovfl+log(2)
4524591Szliu  *				     :  cosh(x) := exp(x)/2 (avoid overflow)
4624591Szliu  *	    log(2)+lnovfl <  x <  INF:  overflow to INF
4724591Szliu  *
4824591Szliu  *	Note: .3465 is a number near one half of ln2.
4924591Szliu  *
5024591Szliu  * Special cases:
5124591Szliu  *	cosh(x) is x if x is +INF, -INF, or NaN.
5224591Szliu  *	only cosh(0)=1 is exact for finite x.
5324591Szliu  *
5424591Szliu  * Accuracy:
5524591Szliu  *	cosh(x) returns the exact hyperbolic cosine of x nearly rounded.
5624591Szliu  *	In a test run with 768,000 random arguments on a VAX, the maximum
5724591Szliu  *	observed error was 1.23 ulps (units in the last place).
5824591Szliu  *
5924591Szliu  * Constants:
6024591Szliu  * The hexadecimal values are the intended ones for the following constants.
6124591Szliu  * The decimal values may be used, provided that the compiler will convert
6224591Szliu  * from decimal to binary accurately enough to produce the hexadecimal values
6324591Szliu  * shown.
6424591Szliu  */
6524591Szliu 
66*31853Szliu #if defined(vax)||defined(tahoe)
67*31853Szliu #ifdef vax
6831812Szliu #define _0x(A,B)	0x/**/A/**/B
69*31853Szliu #else	/* vax */
7031812Szliu #define _0x(A,B)	0x/**/B/**/A
71*31853Szliu #endif	/* vax */
7226893Selefunt /* static double  */
7324591Szliu /* mln2hi =  8.8029691931113054792E1     , Hex  2^  7   *  .B00F33C7E22BDB */
7424591Szliu /* mln2lo = -4.9650192275318476525E-16   , Hex  2^-50   * -.8F1B60279E582A */
7524591Szliu /* lnovfl =  8.8029691931113053016E1     ; Hex  2^  7   *  .B00F33C7E22BDA */
7631812Szliu static long    mln2hix[] = { _0x(0f33,43b0), _0x(2bdb,c7e2)};
7731812Szliu static long    mln2lox[] = { _0x(1b60,a70f), _0x(582a,279e)};
7831812Szliu static long    lnovflx[] = { _0x(0f33,43b0), _0x(2bda,c7e2)};
7924591Szliu #define   mln2hi    (*(double*)mln2hix)
8024591Szliu #define   mln2lo    (*(double*)mln2lox)
8124591Szliu #define   lnovfl    (*(double*)lnovflx)
82*31853Szliu #else	/* defined(vax)||defined(tahoe) */
8326893Selefunt static double
8424591Szliu mln2hi =  7.0978271289338397310E2     , /*Hex  2^ 10   *  1.62E42FEFA39EF */
8524591Szliu mln2lo =  2.3747039373786107478E-14   , /*Hex  2^-45   *  1.ABC9E3B39803F */
8624591Szliu lnovfl =  7.0978271289338397310E2     ; /*Hex  2^  9   *  1.62E42FEFA39EF */
87*31853Szliu #endif	/* defined(vax)||defined(tahoe) */
8824591Szliu 
89*31853Szliu #if defined(vax)||defined(tahoe)
9024591Szliu static max = 126                      ;
91*31853Szliu #else	/* defined(vax)||defined(tahoe) */
9224591Szliu static max = 1023                     ;
93*31853Szliu #endif	/* defined(vax)||defined(tahoe) */
9424591Szliu 
9524591Szliu double cosh(x)
9624591Szliu double x;
9724591Szliu {
9824591Szliu 	static double half=1.0/2.0,one=1.0, small=1.0E-18; /* fl(1+small)==1 */
9924591Szliu 	double scalb(),copysign(),exp(),exp__E(),t;
10024591Szliu 
101*31853Szliu #if !defined(vax)&&!defined(tahoe)
10224591Szliu 	if(x!=x) return(x);	/* x is NaN */
103*31853Szliu #endif	/* !defined(vax)&&!defined(tahoe) */
10424591Szliu 	if((x=copysign(x,one)) <= 22)
10524591Szliu 	    if(x<0.3465)
10624591Szliu 		if(x<small) return(one+x);
10724591Szliu 		else {t=x+exp__E(x,0.0);x=t+t; return(one+t*t/(2.0+x)); }
10824591Szliu 
10924591Szliu 	    else /* for x lies in [0.3465,22] */
11024591Szliu 	        { t=exp(x); return((t+one/t)*half); }
11124591Szliu 
11224591Szliu 	if( lnovfl <= x && x <= (lnovfl+0.7))
11324591Szliu         /* for x lies in [lnovfl, lnovfl+ln2], decrease x by ln(2^(max+1))
11424591Szliu          * and return 2^max*exp(x) to avoid unnecessary overflow
11524591Szliu          */
11624591Szliu 	    return(scalb(exp((x-mln2hi)-mln2lo), max));
11724591Szliu 
11824591Szliu 	else
11924591Szliu 	    return(exp(x)*half);	/* for large x,  cosh(x)=exp(x)/2 */
12024591Szliu }
121