xref: /original-bsd/lib/libm/common_source/acosh.c (revision c3e32dec)
1 /*
2  * Copyright (c) 1985, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)acosh.c	8.1 (Berkeley) 06/04/93";
10 #endif /* not lint */
11 
12 /* ACOSH(X)
13  * RETURN THE INVERSE HYPERBOLIC COSINE OF X
14  * DOUBLE PRECISION (VAX D FORMAT 56 BITS, IEEE DOUBLE 53 BITS)
15  * CODED IN C BY K.C. NG, 2/16/85;
16  * REVISED BY K.C. NG on 3/6/85, 3/24/85, 4/16/85, 8/17/85.
17  *
18  * Required system supported functions :
19  *	sqrt(x)
20  *
21  * Required kernel function:
22  *	log1p(x) 		...return log(1+x)
23  *
24  * Method :
25  *	Based on
26  *		acosh(x) = log [ x + sqrt(x*x-1) ]
27  *	we have
28  *		acosh(x) := log1p(x)+ln2,	if (x > 1.0E20); else
29  *		acosh(x) := log1p( sqrt(x-1) * (sqrt(x-1) + sqrt(x+1)) ) .
30  *	These formulae avoid the over/underflow complication.
31  *
32  * Special cases:
33  *	acosh(x) is NaN with signal if x<1.
34  *	acosh(NaN) is NaN without signal.
35  *
36  * Accuracy:
37  *	acosh(x) returns the exact inverse hyperbolic cosine of x nearly
38  *	rounded. In a test run with 512,000 random arguments on a VAX, the
39  *	maximum observed error was 3.30 ulps (units of the last place) at
40  *	x=1.0070493753568216 .
41  *
42  * Constants:
43  * The hexadecimal values are the intended ones for the following constants.
44  * The decimal values may be used, provided that the compiler will convert
45  * from decimal to binary accurately enough to produce the hexadecimal values
46  * shown.
47  */
48 
49 #include "mathimpl.h"
50 
51 vc(ln2hi, 6.9314718055829871446E-1  ,7217,4031,0000,f7d0,   0, .B17217F7D00000)
52 vc(ln2lo, 1.6465949582897081279E-12 ,bcd5,2ce7,d9cc,e4f1, -39, .E7BCD5E4F1D9CC)
53 
54 ic(ln2hi, 6.9314718036912381649E-1,  -1, 1.62E42FEE00000)
55 ic(ln2lo, 1.9082149292705877000E-10,-33, 1.A39EF35793C76)
56 
57 #ifdef vccast
58 #define    ln2hi    vccast(ln2hi)
59 #define    ln2lo    vccast(ln2lo)
60 #endif
61 
62 double acosh(x)
63 double x;
64 {
65 	double t,big=1.E20; /* big+1==big */
66 
67 #if !defined(vax)&&!defined(tahoe)
68 	if(x!=x) return(x);	/* x is NaN */
69 #endif	/* !defined(vax)&&!defined(tahoe) */
70 
71     /* return log1p(x) + log(2) if x is large */
72 	if(x>big) {t=log1p(x)+ln2lo; return(t+ln2hi);}
73 
74 	t=sqrt(x-1.0);
75 	return(log1p(t*(t+sqrt(x+1.0))));
76 }
77