New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose round-to-nearest division algorithm in Objects/longobject.c #53063
Comments
The implementation of 'round' for Python integers uses a round-to-nearest form of divmod: a, b -> q, r, where q is the nearest integer to a / b and r = a - b*q. This form of divmod would be useful elsewhere. In particular, it's currently needed for implementing multiplication and division of timedeltas by a float: see bpo-1289118 . This patch exposes the operation to Python C code as _PyLong_Divmod_Near, and refactors long_round to use this operation. |
Just a few nitpicks on the patch (in increasing pickiness):
if (quo_is_neg) The later form may also be more optimization friendly, particularly if compiler wants to inline static long_add or long_sub.
|
Thanks for reviewing! Updated patch, that addresses points 1-3. For 4, there's no need for the old code, since "self - divmod_near(self, 10**-n)[1]" is enough. And I didn't like the old algorithm anyway; the new one is more straightforward. For 5, I've added a Python version of divmod_near to the comments. |
Looking at Py_DECREF(one);
result = PyTuple_New(2);
if (result == NULL)
goto error;
..
error:
Py_XDECREF(one); If PyTuple_New fails, wouldn't it result in one being DECREF's twice? |
Ah, good point. I knew there was a reason I didn't like Py_XDECREF. |
Committed (with the DECREF(one) fix) in r81541. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: