Discovered in 2013 by then-UPMC-student Jérémie Lumbroso, and independently re-discovered by an Apple programmer last Fall (with much ado on Twitter and Hacker News), the basic idea is as follows: Take your (infinite) random bitstream as the binary digits of random $r\in\left[0,1\right)$…
Python: Rounding fractions half-up
By default, Python's fractions
rounds halves to the nearest even number. If you, instead, want to round a fraction but send halves up, here's how that's done:
def roundhalfup(x: Fraction) -> int:
"""
Rounds x to the nearest integer, with ties being rounded towards positive infinity
"""
return floor(x + Fraction(1, 2))