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)
| 551 | type(x).__name__, type(y).__name__)) |
| 552 | |
| 553 | def _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 | |
| 573 | class timedelta: |
no outgoing calls
no test coverage detected