diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -1888,6 +1888,27 @@ Convert angle x from degrees to radians."); static PyObject * +math_isclose(PyObject *self, PyObject *args, PyObject *kw) +{ + double x, y, rel_tol = 1e-9, abs_tol = 0.0; + double absx, absy, tol; + static char *keywords[] = {"x", "y", "rel_tol", "abs_tol"}; + if (!PyArg_ParseTupleAndKeywords(args, kw, "dd|dd:isclose", keywords, + &x, &y, &rel_tol, &abs_tol)) + return NULL; + absx = fabs(x); + absy = fabs(y); + tol = rel_tol * ((absx > absy)?absx:absy); + if (abs_tol > tol) + tol = abs_tol; + return PyBool_FromLong(fabs(x - y) <= tol); +} + +PyDoc_STRVAR(math_isclose_doc, +"isclose(x, y, rel_tol=1e-9, abs_tol=0.0) -> bool\n\n\ +Return True if if x is close in value to y. False otherwise."); + +static PyObject * math_isfinite(PyObject *self, PyObject *arg) { double x = PyFloat_AsDouble(arg); @@ -1951,6 +1972,8 @@ {"fsum", math_fsum, METH_O, math_fsum_doc}, {"gamma", math_gamma, METH_O, math_gamma_doc}, {"hypot", math_hypot, METH_VARARGS, math_hypot_doc}, + {"isclose", (PyCFunction)math_isclose, + METH_VARARGS|METH_KEYWORDS, math_isclose_doc}, {"isfinite", math_isfinite, METH_O, math_isfinite_doc}, {"isinf", math_isinf, METH_O, math_isinf_doc}, {"isnan", math_isnan, METH_O, math_isnan_doc},