-4

Possible Duplicate:
How is floating point stored? When does it matter?
Python rounding error with float numbers

I am trying to understand why we get floating point representation error in python. I know this is not new question here but honestly I am finding hard time to understand it. I am going through the official document of python http://docs.python.org/tutorial/floatingpoint.html on section Representation Error at bottom of the page.

But I am not able to get how this expression J/2**N comes into picture and why in my interpreter I am getting this value. 0.1--->0.10000000000000001 The closest question I found is floating point issue and How are floating point numbers are stored in memory? but not able to understand.

Can anyone please in detail and simple language? Appreciate any help. Thanks, Sunil

Community
  • 1
  • 1
SRC
  • 590
  • 6
  • 22
  • 3
    See http://stackoverflow.com/search?q=0.1 – sth Oct 05 '11 at 19:20
  • 1
    That particular formula has to do with the specific way floating point numbers are stored on computers. I would ignore it if you don't understand -- the important part is that `0.1` doesn't have an exact binary representation so it gets rounded to something that is closer to `0.10000000000000001` than it is to `0.1`. – agf Oct 05 '11 at 19:42

1 Answers1

1

You can think of 0.1 being a rational number for a computer - a rational number whose decimal expansion is not finite.

Take 1/3 for instance. For us humans, we know that it means "one third" (no more, no less). But if we were to write it down without fractions, we would have to write 0.3333... and so on. In fact, there is no way we can represent exactly one third with a decimal notation. So there are numbers we can write using decimal notation, and numbers we can't. For the latter, we have to use fractions - and we can do so because we have been taught maths at school.

On the other hand, the computer works with bits (only 2 digits: 1 and 0), and can only work with a binary notation - no fractions. Because of the different basis (2 instead of 10), the concept of a finite rational number is somewhat shifted: numbers that we can represent exactly in decimal notation may not be represented exactly in binary notation, and vice versa. What looks like a simple case for us (1/10=one tenth=0.1, exactly) is not necessarily an easy case for a CPU.

  • `0.4 in base 3` there is no 4 in base 3. It's 0.1 -- 1/3. `1/3 in base 12 is ... 0.1, exactly` -- no, `0.1` in base 12 is 1/12, not 1/3. – agf Oct 05 '11 at 19:45
  • @agf: you're right for the base 3 thing. Wrong for the other, since I'm talking about "one third (base 10)". This is actually written "zero point one" in base 12. Anyway, I'm removing this asterisk because it's too confusing. –  Oct 05 '11 at 19:50
  • "one third (base 10)" is _not_ written as "zero point one" in base 12. It's written as zero point four. See https://secure.wikimedia.org/wikipedia/en/wiki/Duodecimal for a chart containing examples. – agf Oct 05 '11 at 19:54
  • @agf: argh, you're right again. Apologies and thanks for the link. –  Oct 05 '11 at 19:58