<div dir="ltr"><div><div>Folks,<br><br></div>After a huge delay, I finally found the time to implement the PEP 485 isclose() function, in C. I tihnk it's time for some review. I appologise for the fact that I have little experience with C, and haven't used the raw C API for years, but it's a pretty simple function, and there's lots of code to copy, so I think it's in OK shape. I hav not yet integrated it with the cPyton source code -- it belongs in mathmodule.c, but I found it easier to put it in a separate module while figuring it out.<br><br></div>You can find the code in the same gitHub repo as the draft PEP and python prototyping code:<br><br><a href="https://github.com/PythonCHB/close_pep">https://github.com/PythonCHB/close_pep</a><br><br>the code is in:<br>  is_close_module.c<br><div><br></div><div>There is a test module in:<br>  test_isclose_c.py<br><br></div><div>and it can be built with:<br><br></div><div>python3 setup.py build_ext --inplace<br><br></div><div>Let me know if I should restructure it or put it anywhere else before it gets reviewed but in the meantime, i welcome any feedback.<br><br></div><div>Thanks,<br></div><div>  -Chris<br><br></div><div>A few questions I have off the bat:<br><br>C-API (and plain C) questions:<br>=============================<br><br>* Is there a better way to create a False or True than::<br><br>    PyBool_FromLong(0) and PyBool_FromLong(1)<br><br>* Style question: should I put brackets in an if clause that has only one line?::<br><br>    if (some_check) {<br>        just_this_one_expression<br>    }<br><br>* I can't find docs for PyDoc_STRVAR: but it looks like it should use it -- how?<br><br>* I'm getting a warning in my PyMethodDef clause::<br><br>    static PyMethodDef IsCloseMethods[] = {<br>        {"isclose", isclose_c, METH_VARARGS | METH_KEYWORDS,<br>         "determine if two floating point numbers are close"},<br>        {NULL, NULL, 0, NULL}        /* Sentinel */<br>    };<br><br>is_close_module.c:61:17: warning: incompatible pointer types initializing<br>      'PyCFunction' (aka 'PyObject *(*)(PyObject *, PyObject *)') with an<br>      expression of type 'PyObject *(PyObject *, PyObject *, PyObject *)'<br>      [-Wincompatible-pointer-types]<br>    {"isclose", isclose_c, METH_VARARGS | METH_KEYWORDS,<br><br>but it seems to be working -- and I've copied, as well as I can, the examples.<br><br>Functionality Questions:<br>========================<br><br>* What do do with other numeric types?<br>  - Integers cast fine...<br>  - Decimal  and Fraction cast fine, too -- but precision is presumably lost.<br>  - Complex ? -- add it to cmath?<br><br><br>* It's raising an Exception for negative tolerances: which don't make sense,<br>  but don't really cause harm either (fabs() is used anyway). I can't say I recall why I did that<br>  for the python prototype, but I reproduced in the C version. Should I?<br><br>* What about zero tolerance? should equal values still be considered close? They are now, and tests reflect that.<br><br><br><br><br></div><div><br></div><div><br></div><div><br><br><br><br><br clear="all"><div><div><div><br>-- <br><div class="gmail_signature"><br>Christopher Barker, Ph.D.<br>Oceanographer<br><br>Emergency Response Division<br>NOAA/NOS/OR&R            (206) 526-6959   voice<br>7600 Sand Point Way NE   (206) 526-6329   fax<br>Seattle, WA  98115       (206) 526-6317   main reception<br><br><a href="mailto:Chris.Barker@noaa.gov" target="_blank">Chris.Barker@noaa.gov</a></div>
</div></div></div></div></div>