xref: /csrg-svn/lib/libm/common_source/asinh.c (revision 34931)
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.3 (Berkeley) 06/30/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 
60 #if defined(vax)||defined(tahoe)	/* VAX/TAHOE D format */
61 #ifdef vax
62 #define _0x(A,B)	0x/**/A/**/B
63 #else	/* vax */
64 #define _0x(A,B)	0x/**/B/**/A
65 #endif	/* vax */
66 /* static double */
67 /* ln2hi  =  6.9314718055829871446E-1    , Hex  2^  0   *  .B17217F7D00000 */
68 /* ln2lo  =  1.6465949582897081279E-12   ; Hex  2^-39   *  .E7BCD5E4F1D9CC */
69 static long     ln2hix[] = { _0x(7217,4031), _0x(0000,f7d0)};
70 static long     ln2lox[] = { _0x(bcd5,2ce7), _0x(d9cc,e4f1)};
71 #define    ln2hi    (*(double*)ln2hix)
72 #define    ln2lo    (*(double*)ln2lox)
73 #else	/* defined(vax)||defined(tahoe) */
74 static double
75 ln2hi  =  6.9314718036912381649E-1    , /*Hex  2^ -1   *  1.62E42FEE00000 */
76 ln2lo  =  1.9082149292705877000E-10   ; /*Hex  2^-33   *  1.A39EF35793C76 */
77 #endif	/* defined(vax)||defined(tahoe) */
78 
79 double asinh(x)
80 double x;
81 {
82 	double copysign(),log1p(),sqrt(),t,s;
83 	static double small=1.0E-10,	/* fl(1+small*small) == 1 */
84 		      big  =1.0E20,	/* fl(1+big) == big */
85 		      one  =1.0   ;
86 
87 #if !defined(vax)&&!defined(tahoe)
88 	if(x!=x) return(x);	/* x is NaN */
89 #endif	/* !defined(vax)&&!defined(tahoe) */
90 	if((t=copysign(x,one))>small)
91 	    if(t<big) {
92 	     	s=one/t; return(copysign(log1p(t+t/(s+sqrt(one+s*s))),x)); }
93 	    else	/* if |x| > big */
94 		{s=log1p(t)+ln2lo; return(copysign(s+ln2hi,x));}
95 	else	/* if |x| < small */
96 	    return(x);
97 }
98