This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author mark.dickinson
Recipients mark.dickinson, rhettinger, steven.daprano
Date 2021-11-23.21:06:41
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1637701601.87.0.953252074656.issue45876@roundup.psfhosted.org>
In-reply-to
Content
Hmm. isqrt_frac_rto is unnecessarily complicated. Here's a more streamlined version of the code.


import math

def isqrt_frac_rto(n, m):
    """
    Square root of n/m, rounded to the nearest integer using round-to-odd.
    """
    a = math.isqrt(n*m) // m
    return a | (a*a*m != n)

def sqrt_frac(n, m):
    """
    Square root of n/m as a float, correctly rounded.
    """
    q = (n.bit_length() - m.bit_length() - 109) // 2
    if q >= 0:
        return float(isqrt_frac_rto(n, m << 2 * q) << q)
    else:
        return isqrt_frac_rto(n << -2 * q, m) / (1 << -q)
History
Date User Action Args
2021-11-23 21:06:41mark.dickinsonsetrecipients: + mark.dickinson, rhettinger, steven.daprano
2021-11-23 21:06:41mark.dickinsonsetmessageid: <1637701601.87.0.953252074656.issue45876@roundup.psfhosted.org>
2021-11-23 21:06:41mark.dickinsonlinkissue45876 messages
2021-11-23 21:06:41mark.dickinsoncreate