Skip to content

πŸ” Rounding Secret

Do you know how round works in python under the hood? I didn't know either and I thought it was interesting.

Let's do some experiments with the round with some calcs:

\(\frac{7}{2}=\lfloor{3.5}\rceil\implies4\)

Python Example

# >>>$ round(7/2)
4

\(\frac{3}{2}=\lfloor{1.5}\rceil\implies2\)

Python Example

# >>>$ round(3/2)
2

\(\frac{9}{2}=\lfloor{4.5}\rceil\implies5\)

# >>>$ round(9/2)
4

Attention

Why python doesn't round to 5? πŸ€”

\(\frac{5}{2}=\lfloor{2.5}\rceil\implies3\)

# >>>$ round(5/2)
2

Attention

Why python doesn't round to 3? πŸ€”

Why? πŸ€”

This Result was not expected, right? But everything has a reason πŸ€“

This is because, in Python, the round function implements banker's rounding, where all the half values are rounded to the closest even number. IEEE 754 recommends using this rounding.

Info

Institute of Electrical and Electronics Engineers - (IEEE)

Round Python Docs

Quote

round(number[, ndigits])

Return number rounded to ndigits precision after the decimal point. If ndigits is omitted or is None, it returns the nearest integer to its input.

For the built-in types supporting round(), values are rounded to the closest multiple of 10 to the power minus ndigits; 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). Any integer value is valid for ndigits (positive, zero, or negative). The return value is an integer if ndigits is omitted or None. Otherwise, the return value has the same type as number.

For a general Python object number, round delegates to number.__round__.

Note

The behavior of round() for floats can be surprising: for example, round(2.675, 2) gives 2.67 instead of the expected 2.68. This is not a bug: it’s a result of the fact that most decimal fractions can’t be represented exactly as a float. See Floating Point Arithmetic: Issues and Limitations for more information.

After knowing this I'll never use the round in the same way again. πŸ˜„

πŸ“– Recommended reading: Is bankers rounding really more numerically stable?

Comments