xref: /original-bsd/lib/libm/common_source/asinh.c (revision 81f57ac7)
1 /*
2  * Copyright (c) 1985 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  * All recipients should regard themselves as participants in an ongoing
18  * research project and hence should feel obligated to report their
19  * experiences (good or bad) with these elementary function codes, using
20  * the sendbug(8) program, to the authors.
21  */
22 
23 #ifndef lint
24 static char sccsid[] = "@(#)asinh.c	5.4 (Berkeley) 09/22/88";
25 #endif /* not lint */
26 
27 /* ASINH(X)
28  * RETURN THE INVERSE HYPERBOLIC SINE OF X
29  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
30  * CODED IN C BY K.C. NG, 2/16/85;
31  * REVISED BY K.C. NG on 3/7/85, 3/24/85, 4/16/85.
32  *
33  * Required system supported functions :
34  *	copysign(x,y)
35  *	sqrt(x)
36  *
37  * Required kernel function:
38  *	log1p(x) 		...return log(1+x)
39  *
40  * Method :
41  *	Based on
42  *		asinh(x) = sign(x) * log [ |x| + sqrt(x*x+1) ]
43  *	we have
44  *	asinh(x) := x  if  1+x*x=1,
45  *		 := sign(x)*(log1p(x)+ln2))	 if sqrt(1+x*x)=x, else
46  *		 := sign(x)*log1p(|x| + |x|/(1/|x| + sqrt(1+(1/|x|)^2)) )
47  *
48  * Accuracy:
49  *	asinh(x) returns the exact inverse hyperbolic sine of x nearly rounded.
50  *	In a test run with 52,000 random arguments on a VAX, the maximum
51  *	observed error was 1.58 ulps (units in the last place).
52  *
53  * Constants:
54  * The hexadecimal values are the intended ones for the following constants.
55  * The decimal values may be used, provided that the compiler will convert
56  * from decimal to binary accurately enough to produce the hexadecimal values
57  * shown.
58  */
59 #include "mathimpl.h"
60 
61 vc(ln2hi, 6.9314718055829871446E-1  ,7217,4031,0000,f7d0,   0, .B17217F7D00000)
62 vc(ln2lo, 1.6465949582897081279E-12 ,bcd5,2ce7,d9cc,e4f1, -39, .E7BCD5E4F1D9CC)
63 
64 ic(ln2hi, 6.9314718036912381649E-1,   -1, 1.62E42FEE00000)
65 ic(ln2lo, 1.9082149292705877000E-10, -33, 1.A39EF35793C76)
66 
67 #ifdef vccast
68 #define    ln2hi    vccast(ln2hi)
69 #define    ln2lo    vccast(ln2lo)
70 #endif
71 
72 double asinh(x)
73 double x;
74 {
75 	double t,s;
76 	const static double	small=1.0E-10,	/* fl(1+small*small) == 1 */
77 				big  =1.0E20,	/* fl(1+big) == big */
78 				one  =1.0   ;
79 
80 #if !defined(vax)&&!defined(tahoe)
81 	if(x!=x) return(x);	/* x is NaN */
82 #endif	/* !defined(vax)&&!defined(tahoe) */
83 	if((t=copysign(x,one))>small)
84 	    if(t<big) {
85 	     	s=one/t; return(copysign(log1p(t+t/(s+sqrt(one+s*s))),x)); }
86 	    else	/* if |x| > big */
87 		{s=log1p(t)+ln2lo; return(copysign(s+ln2hi,x));}
88 	else	/* if |x| < small */
89 	    return(x);
90 }
91