Why is rounding done like this?

75 Views Asked by At

Why is Python rounding like this?

>>> 1.12345678901234567890
1.1234567890123457
>>> 0.0001100110011001100110011001100110011001100110011
0.00011001100110011001
1

There are 1 best solutions below

0
On BEST ANSWER

You should have a look at how floats are handled, and what limitations they have. https://docs.python.org/3/tutorial/floatingpoint.html is a Python specific explanation.

In your particular example, Python chooses to use a representation that has a limited number of digits (depends on the Python version). Internally, different float values may share the same (rounded) representation.