-3

Possible Duplicate:
Floating point inaccuracy examples
Floating point comparison

Computers are meant to be good at arithmetic, aren't they? Why does this print "False"?

double d1 = 1.000001;
double d2 = 0.000001;
Console.WriteLine((d1-d2)==1.0); 

or there result will be different in c sharp or java

Community
  • 1
  • 1
rahularyansharma
  • 11,156
  • 18
  • 79
  • 135
  • 11
    This is the standard floating-point question. Start with wikipedia. Read that. Then search here for the tag [floating-point]. http://stackoverflow.com/questions/tagged/floating-point. Then. After reading all that, please update your question so it's not a duplicate of 100's of others. – S.Lott Oct 20 '11 at 09:45
  • 2
    Here is the answer page for the teasers: http://www.yoda.arachsys.com/csharp/teasers-answers.html – naveen Oct 20 '11 at 09:46
  • 4
    Did you read the answers page, which has a link to my article on floating point? – Jon Skeet Oct 20 '11 at 09:46
  • See also http://docs.sun.com/source/806-3568/ncg%5Fgoldberg.html "What Every Computer Scientist Should Know About Floating-Point Arithmetic" – stuartd Oct 20 '11 at 09:46
  • @Jon sorry i miss that link sir – rahularyansharma Oct 20 '11 at 09:48

1 Answers1

1

I think result may vary depending on which processor and which language you're using.
This because float numbers (in general) are stored with a number of bits that try to represent that fractional number, but there can be some (very little) difference.
So when you do (d1-d2) you probably get a number really close to 1.0, but not exactly 1.0!!

Marco
  • 56,740
  • 14
  • 129
  • 152