Skip to content

Silly arithmetic

Akkiraju Ivaturi

Akkiraju Ivaturi

Asked Sep 7, 2012, 7:21 PM EDT1 answer
Share
LinkedInX

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); 

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Akkiraju Ivaturi
    0
    All the values here are stored as binary floating point. While 1.0 can be stored exactly, 1.000001 is actually stored as 1.0000009999999999177333620536956004798412322998046875, and 0.000001 is actually stored as 0.000000999999999999999954748111825886258685613938723690807819366455078125. The difference between them isn't exactly 1.0, and in fact the difference can't be stored exactly either. Learn more about binary floating point
    0