Thursday, June 16, 2011

Is 0.1+0.2==0.3

Recently a friend of mine brought it up. Try the following in Javascript and to my amusement the answer is false.

What are our computers doing... funny :). Probably the compilers fault.

javascript:var a=0.1;var b=0.2; alert(a+b==0.3).


This one is anologous to the other one posted in http://ozinisle.blogspot.com/2010/05/what-is-20-11-any-idea.html


Hope you enjoyed :)

4 comments:

  1. This is a mathematical oddity caused by the fact that 0.1 cannot be expressed exactly in binary 0.1 in bin is 0.0 0011 0011 0011 0011 0011 ... (try it out here http://www.exploringbinary.com/binary-converter/) a similar thing happens with 11111111111111113 == 11111111111111111 resolves to true because the highest bits are truncated and both 11111111111111111 and 11111111111111113 become 11111111111111112

    Knowing this makes me really really really sad doesnt it ..

    != Steve

    ReplyDelete
  2. Yes it is. I am puzzled. Hope people and business dont face catastrophe cos of this. But the positive side, that is an awesome stuff.... Thats a really big number to test with... Is this one a accidental discovery or a planned attempt ????

    Either ways I am amazed

    ReplyDelete
  3. Hope people and business dont face catastrophe cos of this

    Well, unfortunately they do. Look at home-grown applications developed to deal with currency values for example. Many corporate developers screw this right up.

    ReplyDelete
  4. Believe me.... i meant it. But still... I think we should be able to handle this . After all the compilers are man made and these issues must have a pattern. Some tinkering and caution should help us SAVE THE WORLD (lol)

    ReplyDelete