11/* Definitions of some C99 math library functions, for those platforms
22 that don't implement these functions already. */
33
4+ #include "Python.h"
45#include <float.h>
5- #include <math.h>
6+
7+ /* The following copyright notice applies to the original
8+ implementations of acosh, asinh and atanh. */
9+
10+ /*
11+ * ====================================================
12+ * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
13+ *
14+ * Developed at SunPro, a Sun Microsystems, Inc. business.
15+ * Permission to use, copy, modify, and distribute this
16+ * software is freely granted, provided that this notice
17+ * is preserved.
18+ * ====================================================
19+ */
20+
21+ static const double ln2 = 6.93147180559945286227E-01 ;
22+ static const double two_pow_m28 = 3.7252902984619141E-09 ; /* 2**-28 */
23+ static const double two_pow_p28 = 268435456.0 ; /* 2**28 */
24+ static const double zero = 0.0 ;
25+
26+ /* acosh(x)
27+ * Method :
28+ * Based on
29+ * acosh(x) = log [ x + sqrt(x*x-1) ]
30+ * we have
31+ * acosh(x) := log(x)+ln2, if x is large; else
32+ * acosh(x) := log(2x-1/(sqrt(x*x-1)+x)) if x>2; else
33+ * acosh(x) := log1p(t+sqrt(2.0*t+t*t)); where t=x-1.
34+ *
35+ * Special cases:
36+ * acosh(x) is NaN with signal if x<1.
37+ * acosh(NaN) is NaN without signal.
38+ */
39+
40+ double
41+ _Py_acosh (double x )
42+ {
43+ if (Py_IS_NAN (x )) {
44+ return x + x ;
45+ }
46+ if (x < 1. ) { /* x < 1; return a signaling NaN */
47+ errno = EDOM ;
48+ #ifdef Py_NAN
49+ return Py_NAN ;
50+ #else
51+ return (x - x )/(x - x );
52+ #endif
53+ }
54+ else if (x >= two_pow_p28 ) { /* x > 2**28 */
55+ if (Py_IS_INFINITY (x )) {
56+ return x + x ;
57+ } else {
58+ return log (x )+ ln2 ; /* acosh(huge)=log(2x) */
59+ }
60+ }
61+ else if (x == 1. ) {
62+ return 0.0 ; /* acosh(1) = 0 */
63+ }
64+ else if (x > 2. ) { /* 2 < x < 2**28 */
65+ double t = x * x ;
66+ return log (2.0 * x - 1.0 / (x + sqrt (t - 1.0 )));
67+ }
68+ else { /* 1 < x <= 2 */
69+ double t = x - 1.0 ;
70+ return log1p (t + sqrt (2.0 * t + t * t ));
71+ }
72+ }
73+
74+
75+ /* asinh(x)
76+ * Method :
77+ * Based on
78+ * asinh(x) = sign(x) * log [ |x| + sqrt(x*x+1) ]
79+ * we have
80+ * asinh(x) := x if 1+x*x=1,
81+ * := sign(x)*(log(x)+ln2)) for large |x|, else
82+ * := sign(x)*log(2|x|+1/(|x|+sqrt(x*x+1))) if|x|>2, else
83+ * := sign(x)*log1p(|x| + x^2/(1 + sqrt(1+x^2)))
84+ */
85+
86+ double
87+ _Py_asinh (double x )
88+ {
89+ double w ;
90+ double absx = fabs (x );
91+
92+ if (Py_IS_NAN (x ) || Py_IS_INFINITY (x )) {
93+ return x + x ;
94+ }
95+ if (absx < two_pow_m28 ) { /* |x| < 2**-28 */
96+ return x ; /* return x inexact except 0 */
97+ }
98+ if (absx > two_pow_p28 ) { /* |x| > 2**28 */
99+ w = log (absx )+ ln2 ;
100+ }
101+ else if (absx > 2.0 ) { /* 2 < |x| < 2**28 */
102+ w = log (2.0 * absx + 1.0 / (sqrt (x * x + 1.0 ) + absx ));
103+ }
104+ else { /* 2**-28 <= |x| < 2= */
105+ double t = x * x ;
106+ w = log1p (absx + t / (1.0 + sqrt (1.0 + t )));
107+ }
108+ return copysign (w , x );
109+
110+ }
111+
112+ /* atanh(x)
113+ * Method :
114+ * 1.Reduced x to positive by atanh(-x) = -atanh(x)
115+ * 2.For x>=0.5
116+ * 1 2x x
117+ * atanh(x) = --- * log(1 + -------) = 0.5 * log1p(2 * --------)
118+ * 2 1 - x 1 - x
119+ *
120+ * For x<0.5
121+ * atanh(x) = 0.5*log1p(2x+2x*x/(1-x))
122+ *
123+ * Special cases:
124+ * atanh(x) is NaN if |x| >= 1 with signal;
125+ * atanh(NaN) is that NaN with no signal;
126+ *
127+ */
128+
129+ double
130+ _Py_atanh (double x )
131+ {
132+ double absx ;
133+ double t ;
134+
135+ if (Py_IS_NAN (x )) {
136+ return x + x ;
137+ }
138+ absx = fabs (x );
139+ if (absx >= 1. ) { /* |x| >= 1 */
140+ errno = EDOM ;
141+ #ifdef Py_NAN
142+ return Py_NAN ;
143+ #else
144+ return x /zero ;
145+ #endif
146+ }
147+ if (absx < two_pow_m28 ) { /* |x| < 2**-28 */
148+ return x ;
149+ }
150+ if (absx < 0.5 ) { /* |x| < 0.5 */
151+ t = absx + absx ;
152+ t = 0.5 * log1p (t + t * absx / (1.0 - absx ));
153+ }
154+ else { /* 0.5 <= |x| <= 1.0 */
155+ t = 0.5 * log1p ((absx + absx ) / (1.0 - absx ));
156+ }
157+ return copysign (t , x );
158+ }
6159
7160/* Mathematically, expm1(x) = exp(x) - 1. The expm1 function is designed
8161 to avoid the significant loss of precision that arises from direct
@@ -29,3 +182,47 @@ _Py_expm1(double x)
29182 else
30183 return exp (x ) - 1.0 ;
31184}
185+
186+ /* log1p(x) = log(1+x). The log1p function is designed to avoid the
187+ significant loss of precision that arises from direct evaluation when x is
188+ small. */
189+
190+ double
191+ _Py_log1p (double x )
192+ {
193+ /* For x small, we use the following approach. Let y be the nearest float
194+ to 1+x, then
195+
196+ 1+x = y * (1 - (y-1-x)/y)
197+
198+ so log(1+x) = log(y) + log(1-(y-1-x)/y). Since (y-1-x)/y is tiny, the
199+ second term is well approximated by (y-1-x)/y. If abs(x) >=
200+ DBL_EPSILON/2 or the rounding-mode is some form of round-to-nearest
201+ then y-1-x will be exactly representable, and is computed exactly by
202+ (y-1)-x.
203+
204+ If abs(x) < DBL_EPSILON/2 and the rounding mode is not known to be
205+ round-to-nearest then this method is slightly dangerous: 1+x could be
206+ rounded up to 1+DBL_EPSILON instead of down to 1, and in that case
207+ y-1-x will not be exactly representable any more and the result can be
208+ off by many ulps. But this is easily fixed: for a floating-point
209+ number |x| < DBL_EPSILON/2., the closest floating-point number to
210+ log(1+x) is exactly x.
211+ */
212+
213+ double y ;
214+ if (fabs (x ) < DBL_EPSILON /2. ) {
215+ return x ;
216+ } else if (-0.5 <= x && x <= 1. ) {
217+ /* WARNING: it's possible than an overeager compiler
218+ will incorrectly optimize the following two lines
219+ to the equivalent of "return log(1.+x)". If this
220+ happens, then results from log1p will be inaccurate
221+ for small x. */
222+ y = 1. + x ;
223+ return log (y )- ((y - 1. )- x )/y ;
224+ } else {
225+ /* NaNs and infinities should end up here */
226+ return log (1. + x );
227+ }
228+ }
0 commit comments