[New-bugs-announce] [issue28535] round seems to provide floor, not proper rounding
Mike Williamson
report at bugs.python.org
Tue Oct 25 16:41:35 EDT 2016
New submission from Mike Williamson:
Ran a test that I expected to pass. When the test failed, I was struck by the strange (incorrect) assertion claim when using unittest.assertAlmostEqual:
AssertionError: 32.78 != 32.775 within 2 places
Uhmm... yes it does!
I delved in, discovering that assertAlmostEquals simply calls round. So, I tried it with round, shown below:
>>> round(32.775, 2)
32.77
>>> round(32.785, 2)
32.78
>>> round(32.745, 2)
32.74
>>> round(32.746, 2)
32.75
I then looked at the documentation, to understand whether this odd behavior is indeed expected. I saw (on https://docs.python.org/3/library/functions.html#round ):
---
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).
---
However, as you can see, this is not the behavior I'm experiencing. So, it looks like a real bug.
I am attaching the two files where I was running the unit tests, but I'm not sure you really need them.
Thank you!
----------
components: Library (Lib)
files: bad_tests.py
messages: 279456
nosy: lazarillo
priority: normal
severity: normal
status: open
title: round seems to provide floor, not proper rounding
type: behavior
versions: Python 3.5
Added file: http://bugs.python.org/file45224/bad_tests.py
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue28535>
_______________________________________
More information about the New-bugs-announce
mailing list