add Berkeley specific header
[unix-history] / usr / src / lib / libm / common_source / asinh.c
CommitLineData
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
6 * provided that this notice is preserved and that due credit is given
7 * to the University of California at Berkeley. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
11 *
12 * All recipients should regard themselves as participants in an ongoing
13 * research project and hence should feel obligated to report their
14 * experiences (good or bad) with these elementary function codes, using
15 * the sendbug(8) program, to the authors.
21585cc4
ZAL
16 */
17
18#ifndef lint
4c182be8
KB
19static char sccsid[] = "@(#)asinh.c 5.2 (Berkeley) %G%";
20#endif /* not lint */
21585cc4
ZAL
21
22/* ASINH(X)
23 * RETURN THE INVERSE HYPERBOLIC SINE OF X
24 * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
25 * CODED IN C BY K.C. NG, 2/16/85;
26 * REVISED BY K.C. NG on 3/7/85, 3/24/85, 4/16/85.
27 *
28 * Required system supported functions :
29 * copysign(x,y)
30 * sqrt(x)
31 *
32 * Required kernel function:
33 * log1p(x) ...return log(1+x)
34 *
35 * Method :
36 * Based on
37 * asinh(x) = sign(x) * log [ |x| + sqrt(x*x+1) ]
38 * we have
39 * asinh(x) := x if 1+x*x=1,
40 * := sign(x)*(log1p(x)+ln2)) if sqrt(1+x*x)=x, else
41 * := sign(x)*log1p(|x| + |x|/(1/|x| + sqrt(1+(1/|x|)^2)) )
42 *
43 * Accuracy:
44 * asinh(x) returns the exact inverse hyperbolic sine of x nearly rounded.
45 * In a test run with 52,000 random arguments on a VAX, the maximum
46 * observed error was 1.58 ulps (units in the last place).
47 *
48 * Constants:
49 * The hexadecimal values are the intended ones for the following constants.
50 * The decimal values may be used, provided that the compiler will convert
51 * from decimal to binary accurately enough to produce the hexadecimal values
52 * shown.
53 */
54
859dc438
ZAL
55#if defined(vax)||defined(tahoe) /* VAX/TAHOE D format */
56#ifdef vax
0e01cbea 57#define _0x(A,B) 0x/**/A/**/B
859dc438 58#else /* vax */
0e01cbea 59#define _0x(A,B) 0x/**/B/**/A
859dc438 60#endif /* vax */
21585cc4
ZAL
61/* static double */
62/* ln2hi = 6.9314718055829871446E-1 , Hex 2^ 0 * .B17217F7D00000 */
63/* ln2lo = 1.6465949582897081279E-12 ; Hex 2^-39 * .E7BCD5E4F1D9CC */
0e01cbea
ZAL
64static long ln2hix[] = { _0x(7217,4031), _0x(0000,f7d0)};
65static long ln2lox[] = { _0x(bcd5,2ce7), _0x(d9cc,e4f1)};
21585cc4
ZAL
66#define ln2hi (*(double*)ln2hix)
67#define ln2lo (*(double*)ln2lox)
859dc438 68#else /* defined(vax)||defined(tahoe) */
21585cc4
ZAL
69static double
70ln2hi = 6.9314718036912381649E-1 , /*Hex 2^ -1 * 1.62E42FEE00000 */
71ln2lo = 1.9082149292705877000E-10 ; /*Hex 2^-33 * 1.A39EF35793C76 */
859dc438 72#endif /* defined(vax)||defined(tahoe) */
21585cc4
ZAL
73
74double asinh(x)
75double x;
76{
77 double copysign(),log1p(),sqrt(),t,s;
78 static double small=1.0E-10, /* fl(1+small*small) == 1 */
79 big =1.0E20, /* fl(1+big) == big */
80 one =1.0 ;
81
859dc438 82#if !defined(vax)&&!defined(tahoe)
21585cc4 83 if(x!=x) return(x); /* x is NaN */
859dc438 84#endif /* !defined(vax)&&!defined(tahoe) */
21585cc4
ZAL
85 if((t=copysign(x,one))>small)
86 if(t<big) {
87 s=one/t; return(copysign(log1p(t+t/(s+sqrt(one+s*s))),x)); }
88 else /* if |x| > big */
89 {s=log1p(t)+ln2lo; return(copysign(s+ln2hi,x));}
90 else /* if |x| < small */
91 return(x);
92}