As it turns out, Python does not do commercial rounding.
What is commercial rounding?
For most digits, the direction of rounding is clear. There are some special cases though: Do I round $0.5$ to $0$ or to $1$?
In commercial rounding, this “$0.5$” problem is solved by “rounding away from zero”, meaning: For positive numbers, round up; for negative numbers, round down.
This makes perfect sense for a mathematician, but others might need examples, so here are some1:
- $0.5$ goes to $1$.
- $-0.5$ goes to $-1$ (away from zero).
The same obviously goes in other positions too: $0.05$ goes to $0.1$, etc.
What does Python do?
Here, actually mathematical rounding happens: Round to the next even possibility. Meaning:
- $0.5$ goes to $0$ (!).
- $1.5$ goes to $2$.
- $2.5$ goes to $2$ (!).
This also makes sense in some settings, which I will discuss in a later post.
How do I implement commercial rounding in Python?
I just wrote my own function,which does exactly that and is (in the cases I need) a drop-in replacement for Python’s builtin round
function:
|
|
Just define this at the beginning of your module, and then use the round
function as usual.
Note!
I am not fully sure about this approach: In the end, we again return a floating point number. Why will this not be approximated?
I read that in a paper once, and found it funny. Sorry if you feel offended. ↩︎