Why does round(2.49999999999999992) returns 3
python, python-2.7, rounding
Solution
As @Pavel Anossov says in his comment `there's no such thing as 2.49999999999999992 in IEEE 754, 2.49999999999999992 == 2.5.`. Float might always be critical for your calculations, because in any case (32/64/128 bit float), you have a precision limit. This is obviously also limited for Python floats.
There are different options to deal with that, you could e.g. use the decimal library:
>>> from decimal import *
>>> getcontext().prec = 6
>>> Decimal(1) / Decimal(7)
Decimal('0.142857')
>>> getcontext().prec = 28
>>> Decimal(1) / Decimal(7)
Decimal('0.1428571428571428571428571429')
It's possible to set the precision yourself in that case. decimal is in the standard library.
There are also third party libraries like bigfloat, that you could use (I have no experience with it):
>>> from bigfloat import *
>>> sqrt(2, precision(100)) # compute sqrt(2) with 100 bits of precision
But as you can see, you always have to choose a precision. If you really don't want to lose any kind of precision, use fractions (also in the standard library):
>>> from fractions import Fraction
>>> a = Fraction(16, -10)
>>> a
Fraction(-8, 5)
>>> a / 23
Fraction(-8, 115)
>>> float(a/23)
-0.06956521739130435
Problem
I'm working on the math program and I have a quite big problem with round. So after my program did some math, it rounds the result. Everything works fine but if the `result == 2.49999999999999992 `, round function return `3.0` instead of `2.0`. How can I fix that? Thanks.