summaryrefslogtreecommitdiffstats
path: root/Modules/_testcapi
diff options
context:
space:
mode:
authorSergey B Kirpichev <skirpichev@gmail.com>2024-11-26 15:57:39 (GMT)
committerGitHub <noreply@github.com>2024-11-26 15:57:39 (GMT)
commit987311d42e3ec838de8ff27f9f0575aa791a6bde (patch)
treed05e0da42e5cdf50d774368dfede7f93ef2ee2bd /Modules/_testcapi
parentdcf629213bc046318c862ec0af5db3dfd1fc473a (diff)
downloadcpython-987311d42e3ec838de8ff27f9f0575aa791a6bde.zip
cpython-987311d42e3ec838de8ff27f9f0575aa791a6bde.tar.gz
cpython-987311d42e3ec838de8ff27f9f0575aa791a6bde.tar.bz2
gh-69639: Add mixed-mode rules for complex arithmetic (C-like) (GH-124829)
"Generally, mixed-mode arithmetic combining real and complex variables should be performed directly, not by first coercing the real to complex, lest the sign of zero be rendered uninformative; the same goes for combinations of pure imaginary quantities with complex variables." (c) Kahan, W: Branch cuts for complex elementary functions. This patch implements mixed-mode arithmetic rules, combining real and complex variables as specified by C standards since C99 (in particular, there is no special version for the true division with real lhs operand). Most C compilers implementing C99+ Annex G have only these special rules (without support for imaginary type, which is going to be deprecated in C2y).
Diffstat (limited to 'Modules/_testcapi')
-rw-r--r--Modules/_testcapi/complex.c50
1 files changed, 47 insertions, 3 deletions
diff --git a/Modules/_testcapi/complex.c b/Modules/_testcapi/complex.c
index eceb131..b726cd3 100644
--- a/Modules/_testcapi/complex.c
+++ b/Modules/_testcapi/complex.c
@@ -46,21 +46,59 @@ _py_c_neg(PyObject *Py_UNUSED(module), PyObject *num)
static PyObject * \
_py_c_##suffix(PyObject *Py_UNUSED(module), PyObject *args) \
{ \
- Py_complex num, exp, res; \
+ Py_complex a, b, res; \
\
- if (!PyArg_ParseTuple(args, "DD", &num, &exp)) { \
+ if (!PyArg_ParseTuple(args, "DD", &a, &b)) { \
return NULL; \
} \
\
errno = 0; \
- res = _Py_c_##suffix(num, exp); \
+ res = _Py_c_##suffix(a, b); \
+ return Py_BuildValue("Di", &res, errno); \
+ };
+
+#define _PY_CR_FUNC2(suffix) \
+ static PyObject * \
+ _py_cr_##suffix(PyObject *Py_UNUSED(module), PyObject *args) \
+ { \
+ Py_complex a, res; \
+ double b; \
+ \
+ if (!PyArg_ParseTuple(args, "Dd", &a, &b)) { \
+ return NULL; \
+ } \
+ \
+ errno = 0; \
+ res = _Py_cr_##suffix(a, b); \
+ return Py_BuildValue("Di", &res, errno); \
+ };
+
+#define _PY_RC_FUNC2(suffix) \
+ static PyObject * \
+ _py_rc_##suffix(PyObject *Py_UNUSED(module), PyObject *args) \
+ { \
+ Py_complex b, res; \
+ double a; \
+ \
+ if (!PyArg_ParseTuple(args, "dD", &a, &b)) { \
+ return NULL; \
+ } \
+ \
+ errno = 0; \
+ res = _Py_rc_##suffix(a, b); \
return Py_BuildValue("Di", &res, errno); \
};
_PY_C_FUNC2(sum)
+_PY_CR_FUNC2(sum)
_PY_C_FUNC2(diff)
+_PY_CR_FUNC2(diff)
+_PY_RC_FUNC2(diff)
_PY_C_FUNC2(prod)
+_PY_CR_FUNC2(prod)
_PY_C_FUNC2(quot)
+_PY_CR_FUNC2(quot)
+_PY_RC_FUNC2(quot)
_PY_C_FUNC2(pow)
static PyObject*
@@ -86,10 +124,16 @@ static PyMethodDef test_methods[] = {
{"complex_fromccomplex", complex_fromccomplex, METH_O},
{"complex_asccomplex", complex_asccomplex, METH_O},
{"_py_c_sum", _py_c_sum, METH_VARARGS},
+ {"_py_cr_sum", _py_cr_sum, METH_VARARGS},
{"_py_c_diff", _py_c_diff, METH_VARARGS},
+ {"_py_cr_diff", _py_cr_diff, METH_VARARGS},
+ {"_py_rc_diff", _py_rc_diff, METH_VARARGS},
{"_py_c_neg", _py_c_neg, METH_O},
{"_py_c_prod", _py_c_prod, METH_VARARGS},
+ {"_py_cr_prod", _py_cr_prod, METH_VARARGS},
{"_py_c_quot", _py_c_quot, METH_VARARGS},
+ {"_py_cr_quot", _py_cr_quot, METH_VARARGS},
+ {"_py_rc_quot", _py_rc_quot, METH_VARARGS},
{"_py_c_pow", _py_c_pow, METH_VARARGS},
{"_py_c_abs", _py_c_abs, METH_O},
{NULL},