8000 Factor out division. Update proof link. Remove internal type declaration · python/cpython@14ff0d1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 14ff0d1

Browse files
committed
Factor out division. Update proof link. Remove internal type declaration
1 parent 95330ee commit 14ff0d1

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

Lib/statistics.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,12 +316,15 @@ def _isqrt_frac_rto(n: int, m: int) -> float:
316316

317317
def _sqrt_frac(n: int, m: int) -> float:
318318
"""Square root of n/m as a float, correctly rounded."""
319-
# See algorithm sketch at: https://bugs.python.org/msg406911
320-
q: int = (n.bit_length() - m.bit_length() - _sqrt_shift) // 2
319+
# See principle and proof sketch at: https://bugs.python.org/msg407078
320+
q = (n.bit_length() - m.bit_length() - _sqrt_shift) // 2
321321
if q >= 0:
322-
return (_isqrt_frac_rto(n, m << 2 * q) << q) / 1
322+
numerator = _isqrt_frac_rto(n, m << 2 * q) << q
323+
denominator = 1
323324
else:
324-
return _isqrt_frac_rto(n << -2 * q, m) / (1 << -q)
325+
numerator = _isqrt_frac_rto(n << -2 * q, m)
326+
denominator = 1 << -q
327+
return numerator / denominator # Convert to float
325328

326329

327330
# === Measures of central tendency (averages) ===

0 commit comments

Comments
 (0)
0