[issue46187] Optionally support rounding for math.isqrt()

Tim Peters report at bugs.python.org
Fri Dec 31 11:29:25 EST 2021


Tim Peters <tim at python.org> added the comment:

Suppose we added isqrt_rem(n), returning the integer pair (i, rem) such that

    n == i**2 + rem
    0 <= rem <= 2*i

Then:

- Want the floor of sqrt(n)? i.
- The ceiling? i + (rem != 0).
- Rounded? i + (rem > i).
- Is n a perfect square? not rem.

That's how mpz addresses these, although it has a different function to compute the floor without returning the remainder too.

I wouldn't object to that - just +0, though. Depending on implementation details, which I haven't investigated, it may or may not be materially faster than doing:

def isqrt_rem(n):
    return (i := isqrt(n)), n - i*i

myself.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46187>
_______________________________________


More information about the Python-bugs-list mailing list