Issue #7701: Fix crash in binascii.b2a_uu() in debug mode when given a
[python.git] / Modules / _math.c
blob995d1c0ecc5a46509c67e80143a3fba83d15d80e
1 /* Definitions of some C99 math library functions, for those platforms
2 that don't implement these functions already. */
4 #include "Python.h"
5 #include <float.h>
6 #include "_math.h"
8 /* The following copyright notice applies to the original
9 implementations of acosh, asinh and atanh. */
12 * ====================================================
13 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
15 * Developed at SunPro, a Sun Microsystems, Inc. business.
16 * Permission to use, copy, modify, and distribute this
17 * software is freely granted, provided that this notice
18 * is preserved.
19 * ====================================================
22 static const double ln2 = 6.93147180559945286227E-01;
23 static const double two_pow_m28 = 3.7252902984619141E-09; /* 2**-28 */
24 static const double two_pow_p28 = 268435456.0; /* 2**28 */
25 static const double zero = 0.0;
27 /* acosh(x)
28 * Method :
29 * Based on
30 * acosh(x) = log [ x + sqrt(x*x-1) ]
31 * we have
32 * acosh(x) := log(x)+ln2, if x is large; else
33 * acosh(x) := log(2x-1/(sqrt(x*x-1)+x)) if x>2; else
34 * acosh(x) := log1p(t+sqrt(2.0*t+t*t)); where t=x-1.
36 * Special cases:
37 * acosh(x) is NaN with signal if x<1.
38 * acosh(NaN) is NaN without signal.
41 double
42 _Py_acosh(double x)
44 if (Py_IS_NAN(x)) {
45 return x+x;
47 if (x < 1.) { /* x < 1; return a signaling NaN */
48 errno = EDOM;
49 #ifdef Py_NAN
50 return Py_NAN;
51 #else
52 return (x-x)/(x-x);
53 #endif
55 else if (x >= two_pow_p28) { /* x > 2**28 */
56 if (Py_IS_INFINITY(x)) {
57 return x+x;
58 } else {
59 return log(x)+ln2; /* acosh(huge)=log(2x) */
62 else if (x == 1.) {
63 return 0.0; /* acosh(1) = 0 */
65 else if (x > 2.) { /* 2 < x < 2**28 */
66 double t = x*x;
67 return log(2.0*x - 1.0 / (x + sqrt(t - 1.0)));
69 else { /* 1 < x <= 2 */
70 double t = x - 1.0;
71 return m_log1p(t + sqrt(2.0*t + t*t));
76 /* asinh(x)
77 * Method :
78 * Based on
79 * asinh(x) = sign(x) * log [ |x| + sqrt(x*x+1) ]
80 * we have
81 * asinh(x) := x if 1+x*x=1,
82 * := sign(x)*(log(x)+ln2)) for large |x|, else
83 * := sign(x)*log(2|x|+1/(|x|+sqrt(x*x+1))) if|x|>2, else
84 * := sign(x)*log1p(|x| + x^2/(1 + sqrt(1+x^2)))
87 double
88 _Py_asinh(double x)
90 double w;
91 double absx = fabs(x);
93 if (Py_IS_NAN(x) || Py_IS_INFINITY(x)) {
94 return x+x;
96 if (absx < two_pow_m28) { /* |x| < 2**-28 */
97 return x; /* return x inexact except 0 */
99 if (absx > two_pow_p28) { /* |x| > 2**28 */
100 w = log(absx)+ln2;
102 else if (absx > 2.0) { /* 2 < |x| < 2**28 */
103 w = log(2.0*absx + 1.0 / (sqrt(x*x + 1.0) + absx));
105 else { /* 2**-28 <= |x| < 2= */
106 double t = x*x;
107 w = m_log1p(absx + t / (1.0 + sqrt(1.0 + t)));
109 return copysign(w, x);
113 /* atanh(x)
114 * Method :
115 * 1.Reduced x to positive by atanh(-x) = -atanh(x)
116 * 2.For x>=0.5
117 * 1 2x x
118 * atanh(x) = --- * log(1 + -------) = 0.5 * log1p(2 * --------)
119 * 2 1 - x 1 - x
121 * For x<0.5
122 * atanh(x) = 0.5*log1p(2x+2x*x/(1-x))
124 * Special cases:
125 * atanh(x) is NaN if |x| >= 1 with signal;
126 * atanh(NaN) is that NaN with no signal;
130 double
131 _Py_atanh(double x)
133 double absx;
134 double t;
136 if (Py_IS_NAN(x)) {
137 return x+x;
139 absx = fabs(x);
140 if (absx >= 1.) { /* |x| >= 1 */
141 errno = EDOM;
142 #ifdef Py_NAN
143 return Py_NAN;
144 #else
145 return x/zero;
146 #endif
148 if (absx < two_pow_m28) { /* |x| < 2**-28 */
149 return x;
151 if (absx < 0.5) { /* |x| < 0.5 */
152 t = absx+absx;
153 t = 0.5 * m_log1p(t + t*absx / (1.0 - absx));
155 else { /* 0.5 <= |x| <= 1.0 */
156 t = 0.5 * m_log1p((absx + absx) / (1.0 - absx));
158 return copysign(t, x);
161 /* Mathematically, expm1(x) = exp(x) - 1. The expm1 function is designed
162 to avoid the significant loss of precision that arises from direct
163 evaluation of the expression exp(x) - 1, for x near 0. */
165 double
166 _Py_expm1(double x)
168 /* For abs(x) >= log(2), it's safe to evaluate exp(x) - 1 directly; this
169 also works fine for infinities and nans.
171 For smaller x, we can use a method due to Kahan that achieves close to
172 full accuracy.
175 if (fabs(x) < 0.7) {
176 double u;
177 u = exp(x);
178 if (u == 1.0)
179 return x;
180 else
181 return (u - 1.0) * x / log(u);
183 else
184 return exp(x) - 1.0;
187 /* log1p(x) = log(1+x). The log1p function is designed to avoid the
188 significant loss of precision that arises from direct evaluation when x is
189 small. */
191 double
192 _Py_log1p(double x)
194 /* For x small, we use the following approach. Let y be the nearest float
195 to 1+x, then
197 1+x = y * (1 - (y-1-x)/y)
199 so log(1+x) = log(y) + log(1-(y-1-x)/y). Since (y-1-x)/y is tiny, the
200 second term is well approximated by (y-1-x)/y. If abs(x) >=
201 DBL_EPSILON/2 or the rounding-mode is some form of round-to-nearest
202 then y-1-x will be exactly representable, and is computed exactly by
203 (y-1)-x.
205 If abs(x) < DBL_EPSILON/2 and the rounding mode is not known to be
206 round-to-nearest then this method is slightly dangerous: 1+x could be
207 rounded up to 1+DBL_EPSILON instead of down to 1, and in that case
208 y-1-x will not be exactly representable any more and the result can be
209 off by many ulps. But this is easily fixed: for a floating-point
210 number |x| < DBL_EPSILON/2., the closest floating-point number to
211 log(1+x) is exactly x.
214 double y;
215 if (fabs(x) < DBL_EPSILON/2.) {
216 return x;
217 } else if (-0.5 <= x && x <= 1.) {
218 /* WARNING: it's possible than an overeager compiler
219 will incorrectly optimize the following two lines
220 to the equivalent of "return log(1.+x)". If this
221 happens, then results from log1p will be inaccurate
222 for small x. */
223 y = 1.+x;
224 return log(y)-((y-1.)-x)/y;
225 } else {
226 /* NaNs and infinities should end up here */
227 return log(1.+x);