Commit | Line | Data |
---|---|---|
4c182be8 | 1 | /* |
21585cc4 | 2 | * Copyright (c) 1985 Regents of the University of California. |
4c182be8 KB |
3 | * All rights reserved. |
4 | * | |
5 | * Redistribution and use in source and binary forms are permitted | |
a399f6c8 KB |
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. | |
4c182be8 KB |
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. | |
21585cc4 ZAL |
21 | */ |
22 | ||
23 | #ifndef lint | |
a399f6c8 | 24 | static char sccsid[] = "@(#)asinh.c 5.3 (Berkeley) %G%"; |
4c182be8 | 25 | #endif /* not lint */ |
21585cc4 ZAL |
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 | ||
859dc438 ZAL |
60 | #if defined(vax)||defined(tahoe) /* VAX/TAHOE D format */ |
61 | #ifdef vax | |
0e01cbea | 62 | #define _0x(A,B) 0x/**/A/**/B |
859dc438 | 63 | #else /* vax */ |
0e01cbea | 64 | #define _0x(A,B) 0x/**/B/**/A |
859dc438 | 65 | #endif /* vax */ |
21585cc4 ZAL |
66 | /* static double */ |
67 | /* ln2hi = 6.9314718055829871446E-1 , Hex 2^ 0 * .B17217F7D00000 */ | |
68 | /* ln2lo = 1.6465949582897081279E-12 ; Hex 2^-39 * .E7BCD5E4F1D9CC */ | |
0e01cbea ZAL |
69 | static long ln2hix[] = { _0x(7217,4031), _0x(0000,f7d0)}; |
70 | static long ln2lox[] = { _0x(bcd5,2ce7), _0x(d9cc,e4f1)}; | |
21585cc4 ZAL |
71 | #define ln2hi (*(double*)ln2hix) |
72 | #define ln2lo (*(double*)ln2lox) | |
859dc438 | 73 | #else /* defined(vax)||defined(tahoe) */ |
21585cc4 ZAL |
74 | static double |
75 | ln2hi = 6.9314718036912381649E-1 , /*Hex 2^ -1 * 1.62E42FEE00000 */ | |
76 | ln2lo = 1.9082149292705877000E-10 ; /*Hex 2^-33 * 1.A39EF35793C76 */ | |
859dc438 | 77 | #endif /* defined(vax)||defined(tahoe) */ |
21585cc4 ZAL |
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 | ||
859dc438 | 87 | #if !defined(vax)&&!defined(tahoe) |
21585cc4 | 88 | if(x!=x) return(x); /* x is NaN */ |
859dc438 | 89 | #endif /* !defined(vax)&&!defined(tahoe) */ |
21585cc4 ZAL |
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 | } |