[issue45876] Improve accuracy of stdev functions in statistics

Raymond Hettinger report at bugs.python.org
Wed Nov 24 19:37:05 EST 2021


Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:

> Here's a reference for this use of round-to-odd: 
> https://www.lri.fr/~melquion/doc/05-imacs17_1-expose.pdf

Thanks Mark.  It looks like I'll be getting a little education over the Thanksgiving holiday :-)

Shown below is the code that I'm thinking of using to test for correct rounding.  Is this the right way to do it?

# Verify correct rounding.  Find exact values for half the distance
# to the two adjacent representable floats.  The unrounded function
# input should fall between the exact squares of those values.

for i in range(10_000_000):
    numerator: int = randrange(10 ** randrange(40)) + 1
    denonimator: int = randrange(10 ** randrange(40)) + 1
    x: Fraction = Fraction(numerator, denonimator)

    root: float = sqrt_frac(numerator, denonimator)

    r_up: float = math.nextafter(root, math.inf)
    half_way_up: Fraction = (Fraction(root) + Fraction(r_up)) / 2
    half_way_up_squared: Fraction = half_way_up ** 2
    
    r_down: float = math.nextafter(root, -math.inf)
    half_way_down: Fraction = (Fraction(root) + Fraction(r_down)) / 2
    half_way_down_squared: Fraction = half_way_down ** 2

    assert r_down < root < r_up
    assert half_way_down_squared <= x <= half_way_up_squared

----------

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


More information about the Python-bugs-list mailing list