xref: /csrg-svn/lib/libm/common_source/cosh.c (revision 26893)
1 /*
2  * Copyright (c) 1985 Regents of the University of California.
3  *
4  * Use and reproduction of this software are granted  in  accordance  with
5  * the terms and conditions specified in  the  Berkeley  Software  License
6  * Agreement (in particular, this entails acknowledgement of the programs'
7  * source, and inclusion of this notice) with the additional understanding
8  * that  all  recipients  should regard themselves as participants  in  an
9  * ongoing  research  project and hence should  feel  obligated  to report
10  * their  experiences (good or bad) with these elementary function  codes,
11  * using "sendbug 4bsd-bugs@BERKELEY", to the authors.
12  */
13 
14 #ifndef lint
15 static char sccsid[] =
16 "@(#)cosh.c	1.2 (Berkeley) 8/21/85; 1.3 (ucb.elefunt) 03/16/86";
17 #endif not lint
18 
19 /* COSH(X)
20  * RETURN THE HYPERBOLIC COSINE OF X
21  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
22  * CODED IN C BY K.C. NG, 1/8/85;
23  * REVISED BY K.C. NG on 2/8/85, 2/23/85, 3/7/85, 3/29/85, 4/16/85.
24  *
25  * Required system supported functions :
26  *	copysign(x,y)
27  *	scalb(x,N)
28  *
29  * Required kernel function:
30  *	exp(x)
31  *	exp__E(x,c)	...return exp(x+c)-1-x for |x|<0.3465
32  *
33  * Method :
34  *	1. Replace x by |x|.
35  *	2.
36  *		                                        [ exp(x) - 1 ]^2
37  *	    0        <= x <= 0.3465  :  cosh(x) := 1 + -------------------
38  *			       			           2*exp(x)
39  *
40  *		                                   exp(x) +  1/exp(x)
41  *	    0.3465   <= x <= 22      :  cosh(x) := -------------------
42  *			       			           2
43  *	    22       <= x <= lnovfl  :  cosh(x) := exp(x)/2
44  *	    lnovfl   <= x <= lnovfl+log(2)
45  *				     :  cosh(x) := exp(x)/2 (avoid overflow)
46  *	    log(2)+lnovfl <  x <  INF:  overflow to INF
47  *
48  *	Note: .3465 is a number near one half of ln2.
49  *
50  * Special cases:
51  *	cosh(x) is x if x is +INF, -INF, or NaN.
52  *	only cosh(0)=1 is exact for finite x.
53  *
54  * Accuracy:
55  *	cosh(x) returns the exact hyperbolic cosine of x nearly rounded.
56  *	In a test run with 768,000 random arguments on a VAX, the maximum
57  *	observed error was 1.23 ulps (units in the last place).
58  *
59  * Constants:
60  * The hexadecimal values are the intended ones for the following constants.
61  * The decimal values may be used, provided that the compiler will convert
62  * from decimal to binary accurately enough to produce the hexadecimal values
63  * shown.
64  */
65 
66 #ifdef VAX
67 /* static double  */
68 /* mln2hi =  8.8029691931113054792E1     , Hex  2^  7   *  .B00F33C7E22BDB */
69 /* mln2lo = -4.9650192275318476525E-16   , Hex  2^-50   * -.8F1B60279E582A */
70 /* lnovfl =  8.8029691931113053016E1     ; Hex  2^  7   *  .B00F33C7E22BDA */
71 static long    mln2hix[] = { 0x0f3343b0, 0x2bdbc7e2};
72 static long    mln2lox[] = { 0x1b60a70f, 0x582a279e};
73 static long    lnovflx[] = { 0x0f3343b0, 0x2bdac7e2};
74 #define   mln2hi    (*(double*)mln2hix)
75 #define   mln2lo    (*(double*)mln2lox)
76 #define   lnovfl    (*(double*)lnovflx)
77 #else	/* IEEE double */
78 static double
79 mln2hi =  7.0978271289338397310E2     , /*Hex  2^ 10   *  1.62E42FEFA39EF */
80 mln2lo =  2.3747039373786107478E-14   , /*Hex  2^-45   *  1.ABC9E3B39803F */
81 lnovfl =  7.0978271289338397310E2     ; /*Hex  2^  9   *  1.62E42FEFA39EF */
82 #endif
83 
84 #ifdef VAX
85 static max = 126                      ;
86 #else	/* IEEE double */
87 static max = 1023                     ;
88 #endif
89 
90 double cosh(x)
91 double x;
92 {
93 	static double half=1.0/2.0,one=1.0, small=1.0E-18; /* fl(1+small)==1 */
94 	double scalb(),copysign(),exp(),exp__E(),t;
95 
96 #ifndef VAX
97 	if(x!=x) return(x);	/* x is NaN */
98 #endif
99 	if((x=copysign(x,one)) <= 22)
100 	    if(x<0.3465)
101 		if(x<small) return(one+x);
102 		else {t=x+exp__E(x,0.0);x=t+t; return(one+t*t/(2.0+x)); }
103 
104 	    else /* for x lies in [0.3465,22] */
105 	        { t=exp(x); return((t+one/t)*half); }
106 
107 	if( lnovfl <= x && x <= (lnovfl+0.7))
108         /* for x lies in [lnovfl, lnovfl+ln2], decrease x by ln(2^(max+1))
109          * and return 2^max*exp(x) to avoid unnecessary overflow
110          */
111 	    return(scalb(exp((x-mln2hi)-mln2lo), max));
112 
113 	else
114 	    return(exp(x)*half);	/* for large x,  cosh(x)=exp(x)/2 */
115 }
116