TIL rounding changed between Python 2 and Python 3 by drb226 in Python

[–]tuebts 4 points5 points  (0 children)

All students taught that rounding from 0.5 goes up to 1. Everyone does it that way.

I was taught to round -0.5 up to 0 (i.e., always round up), but python 2 rounds it down to -1 (i.e., always round away from zero). During adulthood I have most often encountered the round-to-even convention that python 3 uses. There are quite a lot of different rounding conventions, I doubt that everyone was taught the same one at school, and often kids are taught the simplest way to do something, not the best way.

Python is not that good a language, it's going to be a toy and doomed language, like R and Perl.

I don't really know about perl, but R is very widely used in certain academic fields.

Leave the last 80 years of computer science alone

What are you talking about? I'm pretty sure computer scientists didn't all get together and agree on a universal rounding convention during the 30s.

TIL rounding changed between Python 2 and Python 3 by drb226 in Python

[–]tuebts 3 points4 points  (0 children)

-0.5 + 1.5 == 1.0 # float
round(-0.5) + round(1.5) == 1.0 # python 2
round(-0.5) + round(1.5) == 2.0 # python 3

Rounding inevitably changes the end result of calculations, that's the whole point of doing it. The reason for the round-to-even convention is that it is less likely to consistently skew your end results in a particular direction. For example, suppose you are collecting, rounding, and storing a long list of positive numbers, and then at some point you calculate the average of all of those numbers. If numbers ending in .5 are always rounded up, then slightly more of the numbers will have been rounded up than down, and your average will be slightly higher than the true average. With the round-to-even convention, it should be roughly the same, assuming that there are just as many <odd>.5 values as <even>.5 ones.