Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / lib / libm / common_source / cosh.c
1 /*
2  * Copyright (c) 1985, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#)cosh.c   8.1 (Berkeley) 6/4/93
34  */
35
36 /* COSH(X)
37  * RETURN THE HYPERBOLIC COSINE OF X
38  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
39  * CODED IN C BY K.C. NG, 1/8/85;
40  * REVISED BY K.C. NG on 2/8/85, 2/23/85, 3/7/85, 3/29/85, 4/16/85.
41  *
42  * Required system supported functions :
43  *      copysign(x,y)
44  *      scalb(x,N)
45  *
46  * Required kernel function:
47  *      exp(x)
48  *      exp__E(x,c)     ...return exp(x+c)-1-x for |x|<0.3465
49  *
50  * Method :
51  *      1. Replace x by |x|.
52  *      2.
53  *                                                      [ exp(x) - 1 ]^2
54  *          0        <= x <= 0.3465  :  cosh(x) := 1 + -------------------
55  *                                                         2*exp(x)
56  *
57  *                                                 exp(x) +  1/exp(x)
58  *          0.3465   <= x <= 22      :  cosh(x) := -------------------
59  *                                                         2
60  *          22       <= x <= lnovfl  :  cosh(x) := exp(x)/2
61  *          lnovfl   <= x <= lnovfl+log(2)
62  *                                   :  cosh(x) := exp(x)/2 (avoid overflow)
63  *          log(2)+lnovfl <  x <  INF:  overflow to INF
64  *
65  *      Note: .3465 is a number near one half of ln2.
66  *
67  * Special cases:
68  *      cosh(x) is x if x is +INF, -INF, or NaN.
69  *      only cosh(0)=1 is exact for finite x.
70  *
71  * Accuracy:
72  *      cosh(x) returns the exact hyperbolic cosine of x nearly rounded.
73  *      In a test run with 768,000 random arguments on a VAX, the maximum
74  *      observed error was 1.23 ulps (units in the last place).
75  *
76  * Constants:
77  * The hexadecimal values are the intended ones for the following constants.
78  * The decimal values may be used, provided that the compiler will convert
79  * from decimal to binary accurately enough to produce the hexadecimal values
80  * shown.
81  */
82
83 #include "mathimpl.h"
84
85 vc(mln2hi, 8.8029691931113054792E1   ,0f33,43b0,2bdb,c7e2,   7, .B00F33C7E22BDB)
86 vc(mln2lo,-4.9650192275318476525E-16 ,1b60,a70f,582a,279e, -50,-.8F1B60279E582A)
87 vc(lnovfl, 8.8029691931113053016E1   ,0f33,43b0,2bda,c7e2,   7, .B00F33C7E22BDA)
88
89 ic(mln2hi, 7.0978271289338397310E2,    10, 1.62E42FEFA39EF)
90 ic(mln2lo, 2.3747039373786107478E-14, -45, 1.ABC9E3B39803F)
91 ic(lnovfl, 7.0978271289338397310E2,     9, 1.62E42FEFA39EF)
92
93 #ifdef vccast
94 #define   mln2hi    vccast(mln2hi)
95 #define   mln2lo    vccast(mln2lo)
96 #define   lnovfl    vccast(lnovfl)
97 #endif
98
99 #if defined(vax)||defined(tahoe)
100 static max = 126                      ;
101 #else   /* defined(vax)||defined(tahoe) */
102 static max = 1023                     ;
103 #endif  /* defined(vax)||defined(tahoe) */
104
105 double cosh(x)
106 double x;
107 {
108         static const double half=1.0/2.0,
109                 one=1.0, small=1.0E-18; /* fl(1+small)==1 */
110         double t;
111
112 #if !defined(vax)&&!defined(tahoe)
113         if(x!=x) return(x);     /* x is NaN */
114 #endif  /* !defined(vax)&&!defined(tahoe) */
115         if((x=copysign(x,one)) <= 22)
116             if(x<0.3465)
117                 if(x<small) return(one+x);
118                 else {t=x+__exp__E(x,0.0);x=t+t; return(one+t*t/(2.0+x)); }
119
120             else /* for x lies in [0.3465,22] */
121                 { t=exp(x); return((t+one/t)*half); }
122
123         if( lnovfl <= x && x <= (lnovfl+0.7))
124         /* for x lies in [lnovfl, lnovfl+ln2], decrease x by ln(2^(max+1))
125          * and return 2^max*exp(x) to avoid unnecessary overflow
126          */
127             return(scalb(exp((x-mln2hi)-mln2lo), max));
128
129         else
130             return(exp(x)*half);        /* for large x,  cosh(x)=exp(x)/2 */
131 }