<div dir="ltr">I've been working on a new project and used it as an excuse for starting in Python 3. One very surprising change that tripped me up initially is the behavior of round.<div><br></div><div><div>Python 2.7.6 (default, Mar 22 2014, 22:59:56) </div><div>>>> round(2.5)<br></div><div>3.0</div><div>>>> round(3.5)</div><div>4.0</div><div><br></div><div>Python 3.4.0 (default, Apr 11 2014, 13:05:11) <br></div><div>>>> round(2.5)<br></div><div>2</div><div>>>> round(3.5)</div><div>4</div></div><div><br></div><div>Specifically, Python 2 states: "if two multiples are equally close, rounding is done away from 0" while in Python 3, "if two multiples are equally close, rounding is done toward the even choice".<br></div><div><br></div><div>The reason for this is that rounding to even tie-breaking eliminates a bias for larger numbers, which could be significant. Round to even is the preferred method according to IEEE 754 (<a href="http://en.wikipedia.org/wiki/IEEE_floating_point#Rounding_rules">http://en.wikipedia.org/wiki/IEEE_floating_point#Rounding_rules</a>).</div><div><br></div><div>And while you can't change the behavior of round(), you can if you use Decimal's. In fact, you can choose from eight different rounding methods when using Decimals (<a href="https://docs.python.org/3/library/decimal.html#decimal.ROUND_CEILING">https://docs.python.org/3/library/decimal.html#decimal.ROUND_CEILING</a>).</div><div><br></div><div>Another difference with Python 3 is a convenience. When calling round with one argument (i.e. don't specify the number of decimal places to round to, which defaults to 0), it will coerce the return type to integer. So for the default case, no more needing to wrap in int() for the many times when round is used to convert a float to an int.  I.e. int(round(x)) can just be round(x).</div><div><br></div><div>-Eric</div><div><br></div></div>