Skip to content

Commit f371859

Browse files
committed
Merged revisions 76978 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r76978 | mark.dickinson | 2009-12-21 15:22:00 +0000 (Mon, 21 Dec 2009) | 3 lines Issue #7518: Move substitute definitions of C99 math functions from pymath.c to Modules/_math.c. ........
1 parent 0f72d6c commit f371859

File tree

8 files changed

+250
-231
lines changed

8 files changed

+250
-231
lines changed

Include/pymath.h

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ Symbols and macros to supply platform-independent interfaces to mathematical
88
functions and constants
99
**************************************************************************/
1010

11-
/* Python provides implementations for copysign, acosh, asinh, atanh,
12-
* log1p and hypot in Python/pymath.c just in case your math library doesn't
13-
* provide the functions.
11+
/* Python provides implementations for copysign, round and hypot in
12+
* Python/pymath.c just in case your math library doesn't provide the
13+
* functions.
1414
*
1515
*Note: PC/pyconfig.h defines copysign as _copysign
1616
*/
@@ -22,22 +22,6 @@ extern double copysign(double, double);
2222
extern double round(double);
2323
#endif
2424

25-
#ifndef HAVE_ACOSH
26-
extern double acosh(double);
27-
#endif
28-
29-
#ifndef HAVE_ASINH
30-
extern double asinh(double);
31-
#endif
32-
33-
#ifndef HAVE_ATANH
34-
extern double atanh(double);
35-
#endif
36-
37-
#ifndef HAVE_LOG1P
38-
extern double log1p(double);
39-
#endif
40-
4125
#ifndef HAVE_HYPOT
4226
extern double hypot(double, double);
4327
#endif

Modules/Setup.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ _symtable symtablemodule.c
157157
# Modules that should always be present (non UNIX dependent):
158158

159159
#array arraymodule.c # array objects
160-
#cmath cmathmodule.c # -lm # complex math library functions
160+
#cmath cmathmodule.c _math.c # -lm # complex math library functions
161161
#math mathmodule.c _math.c # -lm # math library functions, e.g. sin()
162162
#_struct _struct.c # binary structure packing/unpacking
163163
#time timemodule.c # -lm # time operations and variables

Modules/_math.c

Lines changed: 198 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,161 @@
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+
}

Modules/_math.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,32 @@
1+
double _Py_acosh(double x);
2+
double _Py_asinh(double x);
3+
double _Py_atanh(double x);
14
double _Py_expm1(double x);
5+
double _Py_log1p(double x);
6+
7+
#ifdef HAVE_ACOSH
8+
#define m_acosh acosh
9+
#else
10+
/* if the system doesn't have acosh, use the substitute
11+
function defined in Modules/_math.c. */
12+
#define m_acosh _Py_acosh
13+
#endif
14+
15+
#ifdef HAVE_ASINH
16+
#define m_asinh asinh
17+
#else
18+
/* if the system doesn't have asinh, use the substitute
19+
function defined in Modules/_math.c. */
20+
#define m_asinh _Py_asinh
21+
#endif
22+
23+
#ifdef HAVE_ATANH
24+
#define m_atanh atanh
25+
#else
26+
/* if the system doesn't have atanh, use the substitute
27+
function defined in Modules/_math.c. */
28+
#define m_atanh _Py_atanh
29+
#endif
230

331
#ifdef HAVE_EXPM1
432
#define m_expm1 expm1
@@ -7,3 +35,11 @@ double _Py_expm1(double x);
735
function defined in Modules/_math.c. */
836
#define m_expm1 _Py_expm1
937
#endif
38+
39+
#ifdef HAVE_LOG1P
40+
#define m_log1p log1p
41+
#else
42+
/* if the system doesn't have log1p, use the substitute
43+
function defined in Modules/_math.c. */
44+
#define m_log1p _Py_log1p
45+
#endif

Modules/cmathmodule.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/* much code borrowed from mathmodule.c */
44

55
#include "Python.h"
6+
#include "_math.h"
67
/* we need DBL_MAX, DBL_MIN, DBL_EPSILON, DBL_MANT_DIG and FLT_RADIX from
78
float.h. We assume that FLT_RADIX is either 2 or 16. */
89
#include <float.h>
@@ -149,7 +150,7 @@ c_acos(Py_complex z)
149150
s2.imag = z.imag;
150151
s2 = c_sqrt(s2);
151152
r.real = 2.*atan2(s1.real, s2.real);
152-
r.imag = asinh(s2.real*s1.imag - s2.imag*s1.real);
153+
r.imag = m_asinh(s2.real*s1.imag - s2.imag*s1.real);
153154
}
154155
errno = 0;
155156
return r;
@@ -181,7 +182,7 @@ c_acosh(Py_complex z)
181182
s2.real = z.real + 1.;
182183
s2.imag = z.imag;
183184
s2 = c_sqrt(s2);
184-
r.real = asinh(s1.real*s2.real + s1.imag*s2.imag);
185+
r.real = m_asinh(s1.real*s2.real + s1.imag*s2.imag);
185186
r.imag = 2.*atan2(s1.imag, s2.real);
186187
}
187188
errno = 0;
@@ -238,7 +239,7 @@ c_asinh(Py_complex z)
238239
s2.real = 1.-z.imag;
239240
s2.imag = z.real;
240241
s2 = c_sqrt(s2);
241-
r.real = asinh(s1.real*s2.imag-s2.real*s1.imag);
242+
r.real = m_asinh(s1.real*s2.imag-s2.real*s1.imag);
242243
r.imag = atan2(z.imag, s1.real*s2.real-s1.imag*s2.imag);
243244
}
244245
errno = 0;
@@ -342,7 +343,7 @@ c_atanh(Py_complex z)
342343
errno = 0;
343344
}
344345
} else {
345-
r.real = log1p(4.*z.real/((1-z.real)*(1-z.real) + ay*ay))/4.;
346+
r.real = m_log1p(4.*z.real/((1-z.real)*(1-z.real) + ay*ay))/4.;
346347
r.imag = -atan2(-2.*z.imag, (1-z.real)*(1+z.real) - ay*ay)/2.;
347348
errno = 0;
348349
}
@@ -552,7 +553,7 @@ c_log(Py_complex z)
552553
if (0.71 <= h && h <= 1.73) {
553554
am = ax > ay ? ax : ay; /* max(ax, ay) */
554555
an = ax > ay ? ay : ax; /* min(ax, ay) */
555-
r.real = log1p((am-1)*(am+1)+an*an)/2.;
556+
r.real = m_log1p((am-1)*(am+1)+an*an)/2.;
556557
} else {
557558
r.real = log(h);
558559
}

Modules/mathmodule.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -816,18 +816,18 @@ math_2(PyObject *args, double (*func) (double, double), char *funcname)
816816

817817
FUNC1(acos, acos, 0,
818818
"acos(x)\n\nReturn the arc cosine (measured in radians) of x.")
819-
FUNC1(acosh, acosh, 0,
819+
FUNC1(acosh, m_acosh, 0,
820820
"acosh(x)\n\nReturn the hyperbolic arc cosine (measured in radians) of x.")
821821
FUNC1(asin, asin, 0,
822822
"asin(x)\n\nReturn the arc sine (measured in radians) of x.")
823-
FUNC1(asinh, asinh, 0,
823+
FUNC1(asinh, m_asinh, 0,
824824
"asinh(x)\n\nReturn the hyperbolic arc sine (measured in radians) of x.")
825825
FUNC1(atan, atan, 0,
826826
"atan(x)\n\nReturn the arc tangent (measured in radians) of x.")
827827
FUNC2(atan2, m_atan2,
828828
"atan2(y, x)\n\nReturn the arc tangent (measured in radians) of y/x.\n"
829829
"Unlike atan(y/x), the signs of both x and y are considered.")
830-
FUNC1(atanh, atanh, 0,
830+
FUNC1(atanh, m_atanh, 0,
831831
"atanh(x)\n\nReturn the hyperbolic arc tangent (measured in radians) of x.")
832832

833833
static PyObject * math_ceil(PyObject *self, PyObject *number) {
@@ -895,7 +895,7 @@ FUNC1A(gamma, m_tgamma,
895895
"gamma(x)\n\nGamma function at x.")
896896
FUNC1A(lgamma, m_lgamma,
897897
"lgamma(x)\n\nNatural logarithm of absolute value of Gamma function at x.")
898-
FUNC1(log1p, log1p, 1,
898+
FUNC1(log1p, m_log1p, 1,
899899
"log1p(x)\n\nReturn the natural logarithm of 1+x (base e).\n"
900900
"The result is computed in a way which is accurate for x near zero.")
901901
FUNC1(sin, sin, 0,

0 commit comments

Comments
 (0)