xref: /csrg-svn/lib/libm/common_source/cosh.c (revision 32712)
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; 5.1 (ucb.elefunt) 11/30/87";
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 #if defined(vax)||defined(tahoe)
67 #ifdef vax
68 #define _0x(A,B)	0x/**/A/**/B
69 #else	/* vax */
70 #define _0x(A,B)	0x/**/B/**/A
71 #endif	/* vax */
72 /* static double  */
73 /* mln2hi =  8.8029691931113054792E1     , Hex  2^  7   *  .B00F33C7E22BDB */
74 /* mln2lo = -4.9650192275318476525E-16   , Hex  2^-50   * -.8F1B60279E582A */
75 /* lnovfl =  8.8029691931113053016E1     ; Hex  2^  7   *  .B00F33C7E22BDA */
76 static long    mln2hix[] = { _0x(0f33,43b0), _0x(2bdb,c7e2)};
77 static long    mln2lox[] = { _0x(1b60,a70f), _0x(582a,279e)};
78 static long    lnovflx[] = { _0x(0f33,43b0), _0x(2bda,c7e2)};
79 #define   mln2hi    (*(double*)mln2hix)
80 #define   mln2lo    (*(double*)mln2lox)
81 #define   lnovfl    (*(double*)lnovflx)
82 #else	/* defined(vax)||defined(tahoe) */
83 static double
84 mln2hi =  7.0978271289338397310E2     , /*Hex  2^ 10   *  1.62E42FEFA39EF */
85 mln2lo =  2.3747039373786107478E-14   , /*Hex  2^-45   *  1.ABC9E3B39803F */
86 lnovfl =  7.0978271289338397310E2     ; /*Hex  2^  9   *  1.62E42FEFA39EF */
87 #endif	/* defined(vax)||defined(tahoe) */
88 
89 #if defined(vax)||defined(tahoe)
90 static max = 126                      ;
91 #else	/* defined(vax)||defined(tahoe) */
92 static max = 1023                     ;
93 #endif	/* defined(vax)||defined(tahoe) */
94 
95 double cosh(x)
96 double x;
97 {
98 	static double half=1.0/2.0,one=1.0, small=1.0E-18; /* fl(1+small)==1 */
99 	double scalb(),copysign(),exp(),exp__E(),t;
100 
101 #if !defined(vax)&&!defined(tahoe)
102 	if(x!=x) return(x);	/* x is NaN */
103 #endif	/* !defined(vax)&&!defined(tahoe) */
104 	if((x=copysign(x,one)) <= 22)
105 	    if(x<0.3465)
106 		if(x<small) return(one+x);
107 		else {t=x+exp__E(x,0.0);x=t+t; return(one+t*t/(2.0+x)); }
108 
109 	    else /* for x lies in [0.3465,22] */
110 	        { t=exp(x); return((t+one/t)*half); }
111 
112 	if( lnovfl <= x && x <= (lnovfl+0.7))
113         /* for x lies in [lnovfl, lnovfl+ln2], decrease x by ln(2^(max+1))
114          * and return 2^max*exp(x) to avoid unnecessary overflow
115          */
116 	    return(scalb(exp((x-mln2hi)-mln2lo), max));
117 
118 	else
119 	    return(exp(x)*half);	/* for large x,  cosh(x)=exp(x)/2 */
120 }
121