serhii.net

In the middle of the desert you can say anything you want

18 Oct 2021

211018-1510 Python rounding behaviour

round() has weirdly unexpected behaviour that I’m ashamed I didn’t notice or know about:

if two multiples are equally close, rounding is done toward the even choice (so, for example, both round(0.5) and round(-0.5) are 0, and round(1.5) is 2) 1

So:

>>> round(1.5)
2
>>> round(2.5)
2
>>> round(3.5)
4
Nel mezzo del deserto posso dire tutto quello che voglio.
comments powered by Disqus