Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b67ad7e

Browse files
committed
Patch #826074: cmath.log optional base argument, fixes #823209
(Contributed by Andrew Gaul.)
1 parent ad21945 commit b67ad7e

4 files changed

Lines changed: 53 additions & 11 deletions

File tree

Doc/lib/libcmath.tex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,12 @@ \section{\module{cmath} ---
7373
Return the exponential value \code{e**\var{x}}.
7474
\end{funcdesc}
7575

76-
\begin{funcdesc}{log}{x}
77-
Return the natural logarithm of \var{x}.
76+
\begin{funcdesc}{log}{x\optional{, base}}
77+
Returns the logarithm of \var{x} to the given \var{base}.
78+
If the \var{base} is not specified, returns the natural logarithm of \var{x}.
7879
There is one branch cut, from 0 along the negative real axis to
7980
-\infinity, continuous from above.
81+
\versionchanged[\var{base} argument added]{2.4}
8082
\end{funcdesc}
8183

8284
\begin{funcdesc}{log10}{x}

Lib/test/test_cmath.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,25 @@
22
""" Simple test script for cmathmodule.c
33
Roger E. Masse
44
"""
5-
import cmath
6-
from test.test_support import verbose
5+
import cmath, math
6+
from test.test_support import verbose, verify, TestFailed
7+
8+
verify(abs(cmath.log(10) - math.log(10)) < 1e-9)
9+
verify(abs(cmath.log(10,2) - math.log(10,2)) < 1e-9)
10+
try:
11+
cmath.log('a')
12+
except TypeError:
13+
pass
14+
else:
15+
raise TestFailed
16+
17+
try:
18+
cmath.log(10, 'a')
19+
except TypeError:
20+
pass
21+
else:
22+
raise TestFailed
23+
724

825
testdict = {'acos' : 1.0,
926
'acosh' : 1.0,

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,9 @@ Extension modules
336336
Library
337337
-------
338338

339+
- Bug #823209: cmath.log() now takes an optional base argument so that its
340+
API matches math.log().
341+
339342
- Bug #957381: distutils bdist_rpm no longer fails on recent RPM versions
340343
that generate a *-debuginfo.rpm.
341344

Modules/cmathmodule.c

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ static Py_complex c_halfi = {0., 0.5};
2020
static Py_complex c_log(Py_complex);
2121
static Py_complex c_prodi(Py_complex);
2222
static Py_complex c_sqrt(Py_complex);
23+
static PyObject * math_error(void);
2324

2425

2526
static Py_complex
@@ -164,11 +165,6 @@ c_log(Py_complex x)
164165
return r;
165166
}
166167

167-
PyDoc_STRVAR(c_log_doc,
168-
"log(x)\n"
169-
"\n"
170-
"Return the natural logarithm of x.");
171-
172168

173169
static Py_complex
174170
c_log10(Py_complex x)
@@ -312,6 +308,31 @@ PyDoc_STRVAR(c_tanh_doc,
312308
"\n"
313309
"Return the hyperbolic tangent of x.");
314310

311+
static PyObject *
312+
cmath_log(PyObject *self, PyObject *args)
313+
{
314+
Py_complex x;
315+
Py_complex y;
316+
317+
if (!PyArg_ParseTuple(args, "D|D", &x, &y))
318+
return NULL;
319+
320+
errno = 0;
321+
PyFPE_START_PROTECT("complex function", return 0)
322+
x = c_log(x);
323+
if (PyTuple_GET_SIZE(args) == 2)
324+
x = c_quot(x, c_log(y));
325+
PyFPE_END_PROTECT(x)
326+
if (errno != 0)
327+
return math_error();
328+
Py_ADJUST_ERANGE2(x.real, x.imag);
329+
return PyComplex_FromCComplex(x);
330+
}
331+
332+
PyDoc_STRVAR(cmath_log_doc,
333+
"log(x[, base]) -> the logarithm of x to the given base.\n\
334+
If the base not specified, returns the natural logarithm (base e) of x.");
335+
315336

316337
/* And now the glue to make them available from Python: */
317338

@@ -358,7 +379,6 @@ FUNC1(cmath_atanh, c_atanh)
358379
FUNC1(cmath_cos, c_cos)
359380
FUNC1(cmath_cosh, c_cosh)
360381
FUNC1(cmath_exp, c_exp)
361-
FUNC1(cmath_log, c_log)
362382
FUNC1(cmath_log10, c_log10)
363383
FUNC1(cmath_sin, c_sin)
364384
FUNC1(cmath_sinh, c_sinh)
@@ -381,7 +401,7 @@ static PyMethodDef cmath_methods[] = {
381401
{"cos", cmath_cos, METH_VARARGS, c_cos_doc},
382402
{"cosh", cmath_cosh, METH_VARARGS, c_cosh_doc},
383403
{"exp", cmath_exp, METH_VARARGS, c_exp_doc},
384-
{"log", cmath_log, METH_VARARGS, c_log_doc},
404+
{"log", cmath_log, METH_VARARGS, cmath_log_doc},
385405
{"log10", cmath_log10, METH_VARARGS, c_log10_doc},
386406
{"sin", cmath_sin, METH_VARARGS, c_sin_doc},
387407
{"sinh", cmath_sinh, METH_VARARGS, c_sinh_doc},

0 commit comments

Comments
 (0)