]> granicus.if.org Git - python/commitdiff
Patch #826074: cmath.log optional base argument, fixes #823209
authorRaymond Hettinger <python@rcn.com>
Mon, 14 Jun 2004 07:40:10 +0000 (07:40 +0000)
committerRaymond Hettinger <python@rcn.com>
Mon, 14 Jun 2004 07:40:10 +0000 (07:40 +0000)
(Contributed by Andrew Gaul.)

Doc/lib/libcmath.tex
Lib/test/test_cmath.py
Misc/NEWS
Modules/cmathmodule.c

index 77496bf4e0559abdacb0b2db1f5aad13b087e351..465c8fd89f91c45b7d745044cb9537d46adb65fc 100644 (file)
@@ -73,10 +73,12 @@ Return the hyperbolic cosine of \var{x}.
 Return the exponential value \code{e**\var{x}}.
 \end{funcdesc}
 
-\begin{funcdesc}{log}{x}
-Return the natural logarithm of \var{x}.
+\begin{funcdesc}{log}{x\optional{, base}}
+Returns the logarithm of \var{x} to the given \var{base}.
+If the \var{base} is not specified, returns the natural logarithm of \var{x}.
 There is one branch cut, from 0 along the negative real axis to
 -\infinity, continuous from above.
+\versionchanged[\var{base} argument added]{2.4}
 \end{funcdesc}
 
 \begin{funcdesc}{log10}{x}
index c4096993b6c7d5c26487fd06130c8b1618de3f4f..6e392928c3c989b355a79509900f2548c0755a09 100755 (executable)
@@ -2,8 +2,25 @@
 """ Simple test script for cmathmodule.c
     Roger E. Masse
 """
-import cmath
-from test.test_support import verbose
+import cmath, math
+from test.test_support import verbose, verify, TestFailed
+
+verify(abs(cmath.log(10) - math.log(10)) < 1e-9)
+verify(abs(cmath.log(10,2) - math.log(10,2)) < 1e-9)
+try:
+    cmath.log('a')
+except TypeError:
+    pass
+else:
+    raise TestFailed
+
+try:
+    cmath.log(10, 'a')
+except TypeError:
+    pass
+else:
+    raise TestFailed
+
 
 testdict = {'acos' : 1.0,
             'acosh' : 1.0,
index 20a1042220f4bf0a39a5f4ff88144d9bc847d59b..873022ac2f3e60c903cb88bfcc40e4457f5175b1 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -336,6 +336,9 @@ Extension modules
 Library
 -------
 
+- Bug #823209:  cmath.log() now takes an optional base argument so that its
+  API matches math.log().                                                                  
+
 - Bug #957381: distutils bdist_rpm no longer fails on recent RPM versions
   that generate a *-debuginfo.rpm.
 
index 8faa6bf185f76eba8b4b5575c6bae11d694608fa..78b9dd5f8fad55ec2122e3239bd56fdbcd3547f9 100644 (file)
@@ -20,6 +20,7 @@ static Py_complex c_halfi = {0., 0.5};
 static Py_complex c_log(Py_complex);
 static Py_complex c_prodi(Py_complex);
 static Py_complex c_sqrt(Py_complex);
+static PyObject * math_error(void);
 
 
 static Py_complex
@@ -164,11 +165,6 @@ c_log(Py_complex x)
        return r;
 }
 
-PyDoc_STRVAR(c_log_doc,
-"log(x)\n"
-"\n"
-"Return the natural logarithm of x.");
-
 
 static Py_complex
 c_log10(Py_complex x)
@@ -312,6 +308,31 @@ PyDoc_STRVAR(c_tanh_doc,
 "\n"
 "Return the hyperbolic tangent of x.");
 
+static PyObject *
+cmath_log(PyObject *self, PyObject *args)
+{
+       Py_complex x;
+       Py_complex y;
+
+       if (!PyArg_ParseTuple(args, "D|D", &x, &y))
+               return NULL;
+
+       errno = 0;
+       PyFPE_START_PROTECT("complex function", return 0)
+       x = c_log(x);
+       if (PyTuple_GET_SIZE(args) == 2)
+               x = c_quot(x, c_log(y));
+       PyFPE_END_PROTECT(x)
+       if (errno != 0)
+               return math_error();
+       Py_ADJUST_ERANGE2(x.real, x.imag);
+       return PyComplex_FromCComplex(x);
+}
+
+PyDoc_STRVAR(cmath_log_doc,
+"log(x[, base]) -> the logarithm of x to the given base.\n\
+If the base not specified, returns the natural logarithm (base e) of x.");
+
 
 /* And now the glue to make them available from Python: */
 
@@ -358,7 +379,6 @@ FUNC1(cmath_atanh, c_atanh)
 FUNC1(cmath_cos, c_cos)
 FUNC1(cmath_cosh, c_cosh)
 FUNC1(cmath_exp, c_exp)
-FUNC1(cmath_log, c_log)
 FUNC1(cmath_log10, c_log10)
 FUNC1(cmath_sin, c_sin)
 FUNC1(cmath_sinh, c_sinh)
@@ -381,7 +401,7 @@ static PyMethodDef cmath_methods[] = {
        {"cos",    cmath_cos,   METH_VARARGS, c_cos_doc},
        {"cosh",   cmath_cosh,  METH_VARARGS, c_cosh_doc},
        {"exp",    cmath_exp,   METH_VARARGS, c_exp_doc},
-       {"log",    cmath_log,   METH_VARARGS, c_log_doc},
+       {"log",    cmath_log,   METH_VARARGS, cmath_log_doc},
        {"log10",  cmath_log10, METH_VARARGS, c_log10_doc},
        {"sin",    cmath_sin,   METH_VARARGS, c_sin_doc},
        {"sinh",   cmath_sinh,  METH_VARARGS, c_sinh_doc},