0

I am using Python 3.5.3 and I have a strange rounding behavior

round(1.5)
Out[16]: 2

round(2.5)
Out[17]: 2

round(3.5)
Out[18]: 4

Why wasn't round(2.5) rounding to 3?

Nouman
  • 6,947
  • 7
  • 32
  • 60
desmond
  • 1,853
  • 4
  • 21
  • 27
  • You can always check if [the manual](https://docs.python.org/3.5/library/functions.html?#round) has anything to say. (This is the way of rounding many of us were taught at school, and the method of IEEE 754.) – molbdnilo Aug 12 '17 at 08:14
  • Or, more like it, [floating point arithmetic issues and limitations](https://docs.python.org/3/tutorial/floatingpoint.html#tut-fp-issues) – spectras Aug 12 '17 at 08:15
  • Possible duplicate of [Python 3.x rounding behavior](https://stackoverflow.com/questions/10825926/python-3-x-rounding-behavior) – chickity china chinese chicken Aug 12 '17 at 08:16

1 Answers1

0

The Python Documentation says:

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).

If you want to be able to round toward positive infinity if it is in the middle, try this:

import math
def round(number):
    if (math.ceil(number) - number >= number - math.floor(number)):
        return math.ceil(number)
    else:
        return math.floor(number)

Testable at: https://repl.it/KHJY

Oliver Ni
  • 2,606
  • 7
  • 29
  • 44
  • Those numbers are floats in python, subject to floating point arithmetic limitations. Most notably, numbers such as 2.5 and 3.5 don't exist in float, so what you will get is a close value. That may be higher or lower. – spectras Aug 12 '17 at 08:18
  • @spectras If you look at the link I put there, you can see in the Python documentation that is exactly what they say. This example is not due to floating point limitations. There are other examples that are, though, like `2.735` might round to `2.73` instead of `2.74` – Oliver Ni Aug 12 '17 at 08:20
  • Documentation talks about all numbers, but floats have their specific quirks on top of that. The documentation you linked specifically states that *“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."* – spectras Aug 12 '17 at 08:22
  • *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).* – Oliver Ni Aug 12 '17 at 08:22
  • And the example of `0.5` is deceiving: 0.5 has a perfect representation in float, so it does not hit floating point caveats. – spectras Aug 12 '17 at 08:22
  • His question was related to the fact that Python rounds to the even choice if it is equally between two numbers, not the floating point precision issue. – Oliver Ni Aug 12 '17 at 08:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/151769/discussion-between-oliver-ni-and-spectras). – Oliver Ni Aug 12 '17 at 08:23
  • after i post this, i realise it was answered in another post https://stackoverflow.com/questions/10825926/python-3-x-rounding-behavior . I thought this rounding behaviour (since i am not using long float figures) very non-intuitive as i wont know when it goes up or down – desmond Aug 12 '17 at 08:47