xref: /original-bsd/lib/libm/common_source/asinh.c (revision 06bf7e12)
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[] = "@(#)asinh.c	8.1 (Berkeley) 06/04/93";
10 #endif /* not lint */
11 
12 /* ASINH(X)
13  * RETURN THE INVERSE HYPERBOLIC SINE 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/7/85, 3/24/85, 4/16/85.
17  *
18  * Required system supported functions :
19  *	copysign(x,y)
20  *	sqrt(x)
21  *
22  * Required kernel function:
23  *	log1p(x) 		...return log(1+x)
24  *
25  * Method :
26  *	Based on
27  *		asinh(x) = sign(x) * log [ |x| + sqrt(x*x+1) ]
28  *	we have
29  *	asinh(x) := x  if  1+x*x=1,
30  *		 := sign(x)*(log1p(x)+ln2))	 if sqrt(1+x*x)=x, else
31  *		 := sign(x)*log1p(|x| + |x|/(1/|x| + sqrt(1+(1/|x|)^2)) )
32  *
33  * Accuracy:
34  *	asinh(x) returns the exact inverse hyperbolic sine of x nearly rounded.
35  *	In a test run with 52,000 random arguments on a VAX, the maximum
36  *	observed error was 1.58 ulps (units in the last place).
37  *
38  * Constants:
39  * The hexadecimal values are the intended ones for the following constants.
40  * The decimal values may be used, provided that the compiler will convert
41  * from decimal to binary accurately enough to produce the hexadecimal values
42  * shown.
43  */
44 #include "mathimpl.h"
45 
46 vc(ln2hi, 6.9314718055829871446E-1  ,7217,4031,0000,f7d0,   0, .B17217F7D00000)
47 vc(ln2lo, 1.6465949582897081279E-12 ,bcd5,2ce7,d9cc,e4f1, -39, .E7BCD5E4F1D9CC)
48 
49 ic(ln2hi, 6.9314718036912381649E-1,   -1, 1.62E42FEE00000)
50 ic(ln2lo, 1.9082149292705877000E-10, -33, 1.A39EF35793C76)
51 
52 #ifdef vccast
53 #define    ln2hi    vccast(ln2hi)
54 #define    ln2lo    vccast(ln2lo)
55 #endif
56 
57 double asinh(x)
58 double x;
59 {
60 	double t,s;
61 	const static double	small=1.0E-10,	/* fl(1+small*small) == 1 */
62 				big  =1.0E20,	/* fl(1+big) == big */
63 				one  =1.0   ;
64 
65 #if !defined(vax)&&!defined(tahoe)
66 	if(x!=x) return(x);	/* x is NaN */
67 #endif	/* !defined(vax)&&!defined(tahoe) */
68 	if((t=copysign(x,one))>small)
69 	    if(t<big) {
70 	     	s=one/t; return(copysign(log1p(t+t/(s+sqrt(one+s*s))),x)); }
71 	    else	/* if |x| > big */
72 		{s=log1p(t)+ln2lo; return(copysign(s+ln2hi,x));}
73 	else	/* if |x| < small */
74 	    return(x);
75 }
76