New submission from Raymond Hettinger <raymond.hettin...@gmail.com>:
By default, isqrt(n) gives the floor of the exact square of n. It would be nice to have a flag to give a rounded result: y = isqrt(n, round=True) Alternatively, set a mode argument to one of {'floor', 'round', 'ceil'}: y = isqrt(n, mode='round') I would like something better than this: def risqrt(x): 'Big integer version of: round(sqrt(x)).' y = isqrt(x) s = y ** 2 return y if x <= s + y else y + 1 def cisqrt(x): 'Big integer version of: ceil(sqrt(x)).' return isqrt(x - 1) + 1 My use case arose when building a table of square roots incorporated in arbitrary precision functions implemented with scaled integer arithmetic: def get_root_table(base, steps, scale): s = [] x = round(base * scale) for i in range(steps): x = risqrt(x * scale) s.append(x) return s ---------- assignee: mark.dickinson components: Library (Lib) messages: 409243 nosy: mark.dickinson, rhettinger, tim.peters priority: normal severity: normal status: open title: Optionally support rounding for math.isqrt() type: enhancement versions: Python 3.11 _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue46187> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com