MCPcopy Index your code
hub / github.com/python/cpython / _divide_and_round

Function _divide_and_round

Lib/_pydatetime.py:604–621  ·  view source on GitHub ↗

divide a by b and round result to the nearest integer When the ratio is exactly half-way between two integers, the even integer is returned.

(a, b)

Source from the content-addressed store, hash-verified

602 )
603
604def _divide_and_round(a, b):
605 """divide a by b and round result to the nearest integer
606
607 When the ratio is exactly half-way between two integers,
608 the even integer is returned.
609 """
610 # Based on the reference implementation for divmod_near
611 # in Objects/longobject.c.
612 q, r = divmod(a, b)
613 # round up if either r / b > 0.5, or r / b == 0.5 and q is odd.
614 # The expression r / b > 0.5 is equivalent to 2 * r > b if b is
615 # positive, 2 * r < b if b negative.
616 r *= 2
617 greater_than_half = r > b if b > 0 else r < b
618 if greater_than_half or r == b and q % 2 == 1:
619 q += 1
620
621 return q
622
623
624class timedelta:

Callers 2

__mul__Method · 0.85
__truediv__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…