By default, Python's fractions.Fraction 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:

from math import floor
from fractions import Fraction

_ONEHALF = Fraction(1, 2)

def roundhalfup(x: Fraction) -> int:
	Rounds x to the nearest integer, with ties being rounded towards positive infinity
	return floor(x + _ONEHALF)

Since fractions.Fraction implements .__floor__ itself, this does give precise results. At no point is the number converted into a float or other approximation.

Even if you're not dealing directly in Fractions, the library can still be useful:

def divroundhalfup(a: int, b: int) -> int:
	Returns the nearest integer to exactly a/b, with ties rounded up
	return floor(Fraction(2*a + b, 2*b))

Leave a Reply

Your email address will not be published.

Warning: This site uses Akismet to filter spam. Until or unless I can find a suitable replacement anti-spam solution, this means that (per their indemnification document) all commenters' IP addresses will be sent to Automattic, Inc., who may choose to share such with 3rd parties.
If this is unacceptable to you, I highly recommend using an anonymous proxy or public Wi-Fi connection when commenting.