1 /* Copyright JS Foundation and other contributors, http://js.foundation
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *
15 * This file is based on work under the following copyright and permission
16 * notice:
17 *
18 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
19 *
20 * Developed at SunSoft, a Sun Microsystems, Inc. business.
21 * Permission to use, copy, modify, and distribute this
22 * software is freely granted, provided that this notice
23 * is preserved.
24 *
25 * @(#)e_atanh.c 1.3 95/01/18
26 */
27
28 #include "jerry-libm-internal.h"
29
30 /* atanh(x)
31 * Method :
32 * 1.Reduced x to positive by atanh(-x) = -atanh(x)
33 * 2.For x >= 0.5
34 * 1 2x x
35 * atanh(x) = --- * log(1 + -------) = 0.5 * log1p(2 * --------)
36 * 2 1 - x 1 - x
37 *
38 * For x < 0.5
39 * atanh(x) = 0.5 * log1p(2x + 2x * x / (1 - x))
40 *
41 * Special cases:
42 * atanh(x) is NaN if |x| > 1 with signal;
43 * atanh(NaN) is that NaN with no signal;
44 * atanh(+-1) is +-INF with signal.
45 *
46 */
47
48 #define zero 0.0
49 #define one 1.0
50 #define huge 1.0e+300
51
52 double
atanh(double x)53 atanh (double x)
54 {
55 double t;
56 int hx, ix;
57 double_accessor temp;
58 temp.dbl = x;
59 hx = temp.as_int.hi;
60 ix = hx & 0x7fffffff;
61
62 /* |x| > 1 */
63 if ((ix | ((unsigned int) (temp.as_int.lo | (-temp.as_int.lo)) >> 31)) > 0x3ff00000)
64 {
65 return NAN;
66 }
67 if (ix == 0x3ff00000)
68 {
69 return x / zero;
70 }
71 if (ix < 0x3e300000 && (huge + x) > zero)
72 {
73 return x; /* x<2**-28 */
74 }
75
76 /* x <- |x| */
77 temp.as_int.hi = ix;
78 if (ix < 0x3fe00000)
79 {
80 /* x < 0.5 */
81 t = temp.dbl + temp.dbl;
82 t = 0.5 * log1p (t + t * temp.dbl / (one - temp.dbl));
83 }
84 else
85 {
86 t = 0.5 * log1p ((temp.dbl + temp.dbl) / (one - temp.dbl));
87 }
88 if (hx >= 0)
89 {
90 return t;
91 }
92 else
93 {
94 return -t;
95 }
96 } /* atanh */
97
98 #undef zero
99 #undef one
100 #undef huge
101