0

Possible Duplicate:
Floating Point Limitations

Hi there,

maybe this has a very simple answer. I was playing around with the Python interpreter.

>>> 1
1
>>> 1.1
1.1000000000000001
>>> 1.2
1.2

Why 1.1 was interpreted as 1.1000000000000001?

Community
  • 1
  • 1
Donovan
  • 6,002
  • 5
  • 41
  • 55
  • 1
    because you still did not switch to python 3 – joaquin Apr 22 '11 at 09:58
  • 4
    Please search. Questions of floating point are common. Perhaps every week someone asks this question. For example, all of these are the same question: http://stackoverflow.com/search?q=%5Bpython%5D+floating-point. – S.Lott Apr 22 '11 at 10:00

3 Answers3

1

From The Floating-Point Guide:

Why don’t my numbers, like 0.1 + 0.2 add up to a nice round 0.3, and instead I get a weird result like 0.30000000000000004?

Because internally, computers use a format (binary floating-point) that cannot accurately represent a number like 0.1, 0.2 or 0.3 at all.

When the code is compiled or interpreted, your “0.1” is already rounded to the nearest number in that format, which results in a small rounding error even before the calculation happens.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
0

That is due to the way the numbers are stored internally. The format is specified in the IEEE_754-2008 speficiation.

Some more information can be found on single precision floats here

Ikke
  • 99,403
  • 23
  • 97
  • 120
0

you can look at here for Floating Point Arithmetic Issues and Limitations

In any case in python 3 you have:

>>> 1
1
>>> 1.1
1.1
>>> 1.2
1.2
>>> 
joaquin
  • 82,968
  • 29
  • 138
  • 152