diff options
author | Tal Einat <taleinat@gmail.com> | 2015-05-31 22:05:00 +0300 |
---|---|---|
committer | Tal Einat <taleinat@gmail.com> | 2015-05-31 22:05:00 +0300 |
commit | ebca10bccbf59478578bb008da8276e878ebf415 (patch) | |
tree | 01633d9c69ffacfc824a10a9fd0865fcccca77ec /Modules/clinic | |
parent | d5e128f82cab0122914f06cf33d6a44f03e2a33b (diff) | |
download | cpython-ebca10bccbf59478578bb008da8276e878ebf415.tar.gz |
Issue #19543: Implementation of isclose as per PEP 485
For details, see:
PEP 0485 -- A Function for testing approximate equality
Functions added: math.isclose() and cmath.isclose().
Original code by Chris Barker. Patch by Tal Einat.
Diffstat (limited to 'Modules/clinic')
-rw-r--r-- | Modules/clinic/cmathmodule.c.h | 53 |
1 files changed, 52 insertions, 1 deletions
diff --git a/Modules/clinic/cmathmodule.c.h b/Modules/clinic/cmathmodule.c.h index e8fa6cb061..7d61649783 100644 --- a/Modules/clinic/cmathmodule.c.h +++ b/Modules/clinic/cmathmodule.c.h @@ -806,4 +806,55 @@ cmath_isinf(PyModuleDef *module, PyObject *arg) exit: return return_value; } -/*[clinic end generated code: output=274f59792cf4f418 input=a9049054013a1b77]*/ + +PyDoc_STRVAR(cmath_isclose__doc__, +"isclose($module, /, a, b, *, rel_tol=1e-09, abs_tol=0.0)\n" +"--\n" +"\n" +"Determine whether two complex numbers are close in value.\n" +"\n" +" rel_tol\n" +" maximum difference for being considered \"close\", relative to the\n" +" magnitude of the input values\n" +" abs_tol\n" +" maximum difference for being considered \"close\", regardless of the\n" +" magnitude of the input values\n" +"\n" +"Return True if a is close in value to b, and False otherwise.\n" +"\n" +"For the values to be considered close, the difference between them must be\n" +"smaller than at least one of the tolerances.\n" +"\n" +"-inf, inf and NaN behave similarly to the IEEE 754 Standard. That is, NaN is\n" +"not close to anything, even itself. inf and -inf are only close to themselves."); + +#define CMATH_ISCLOSE_METHODDEF \ + {"isclose", (PyCFunction)cmath_isclose, METH_VARARGS|METH_KEYWORDS, cmath_isclose__doc__}, + +static int +cmath_isclose_impl(PyModuleDef *module, Py_complex a, Py_complex b, + double rel_tol, double abs_tol); + +static PyObject * +cmath_isclose(PyModuleDef *module, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + static char *_keywords[] = {"a", "b", "rel_tol", "abs_tol", NULL}; + Py_complex a; + Py_complex b; + double rel_tol = 1e-09; + double abs_tol = 0.0; + int _return_value; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "DD|$dd:isclose", _keywords, + &a, &b, &rel_tol, &abs_tol)) + goto exit; + _return_value = cmath_isclose_impl(module, a, b, rel_tol, abs_tol); + if ((_return_value == -1) && PyErr_Occurred()) + goto exit; + return_value = PyBool_FromLong((long)_return_value); + +exit: + return return_value; +} +/*[clinic end generated code: output=229e9c48c9d27362 input=a9049054013a1b77]*/ |