xref: /netbsd-src/lib/libm/src/e_atanh.c (revision ae1bfcddc410612bc8c58b807e1830becb69a24c)
1 /* @(#)e_atanh.c 5.1 93/09/24 */
2 /*
3  * ====================================================
4  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5  *
6  * Developed at SunPro, a Sun Microsystems, Inc. business.
7  * Permission to use, copy, modify, and distribute this
8  * software is freely granted, provided that this notice
9  * is preserved.
10  * ====================================================
11  */
12 
13 #ifndef lint
14 static char rcsid[] = "$Id: e_atanh.c,v 1.4 1994/03/03 17:04:09 jtc Exp $";
15 #endif
16 
17 /* __ieee754_atanh(x)
18  * Method :
19  *    1.Reduced x to positive by atanh(-x) = -atanh(x)
20  *    2.For x>=0.5
21  *                  1              2x                          x
22  *	atanh(x) = --- * log(1 + -------) = 0.5 * log1p(2 * --------)
23  *                  2             1 - x                      1 - x
24  *
25  * 	For x<0.5
26  *	atanh(x) = 0.5*log1p(2x+2x*x/(1-x))
27  *
28  * Special cases:
29  *	atanh(x) is NaN if |x| > 1 with signal;
30  *	atanh(NaN) is that NaN with no signal;
31  *	atanh(+-1) is +-INF with signal.
32  *
33  */
34 
35 #include <math.h>
36 #include <machine/endian.h>
37 
38 #if BYTE_ORDER == LITTLE_ENDIAN
39 #define n0	1
40 #else
41 #define n0	0
42 #endif
43 
44 #ifdef __STDC__
45 static const double one = 1.0, huge = 1e300;
46 #else
47 static double one = 1.0, huge = 1e300;
48 #endif
49 
50 static double zero = 0.0;
51 
52 #ifdef __STDC__
53 	double __ieee754_atanh(double x)
54 #else
55 	double __ieee754_atanh(x)
56 	double x;
57 #endif
58 {
59 	double t;
60 	int hx,ix;
61 	unsigned lx;
62 
63 	hx = *(n0+(int*)&x);		/* high word */
64 	lx = *(1-n0+(int*)&x);		/* low word */
65 	ix = hx&0x7fffffff;
66 	if ((ix|((lx|(-lx))>>31))>0x3ff00000) /* |x|>1 */
67 	    return (x-x)/(x-x);
68 	if(ix==0x3ff00000)
69 	    return x/zero;
70 	if(ix<0x3e300000&&(huge+x)>zero) return x;	/* x<2**-28 */
71 	*(n0+(int*)&x) = ix;		/* x <- |x| */
72 	if(ix<0x3fe00000) {		/* x < 0.5 */
73 	    t = x+x;
74 	    t = 0.5*log1p(t+t*x/(one-x));
75 	} else
76 	    t = 0.5*log1p((x+x)/(one-x));
77 	if(hx>=0) return t; else return -t;
78 }
79