MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / _divide_and_round

Function _divide_and_round

tools/python-3.11.9-amd64/Lib/datetime.py:553–570  ·  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

551 type(x).__name__, type(y).__name__))
552
553def _divide_and_round(a, b):
554 """divide a by b and round result to the nearest integer
555
556 When the ratio is exactly half-way between two integers,
557 the even integer is returned.
558 """
559 # Based on the reference implementation for divmod_near
560 # in Objects/longobject.c.
561 q, r = divmod(a, b)
562 # round up if either r / b > 0.5, or r / b == 0.5 and q is odd.
563 # The expression r / b > 0.5 is equivalent to 2 * r > b if b is
564 # positive, 2 * r < b if b negative.
565 r *= 2
566 greater_than_half = r > b if b > 0 else r < b
567 if greater_than_half or r == b and q % 2 == 1:
568 q += 1
569
570 return q
571
572
573class timedelta:

Callers 2

__mul__Method · 0.85
__truediv__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected