0

How the less than or equal to condition checking performed on a Double variable.

I have two variable first, second.

Double first = 20.0;
Double second = 20.0;
if(first <= second){ 
    //This is not going inside the if part
}
aioobe
  • 413,195
  • 112
  • 811
  • 826
Sujith
  • 1,127
  • 1
  • 13
  • 20

3 Answers3

3

Probably inherent inaccuracy of floating point representations. See Why am I getting the wrong result when using float? among many other similar questions

Community
  • 1
  • 1
The Archetypal Paul
  • 41,321
  • 20
  • 104
  • 134
0

Not sure if this is the case, but floating points are never represented exactly in memory. Though, the two value are very close to each other, it may happen that first is slightly greater than second, causing the if condition to be false.

Shamim Hafiz - MSFT
  • 21,454
  • 43
  • 116
  • 176
  • 1
    They *are* represented exactly in memory - it's just that the exact value may be an approximation to the original data. For example, the number represented in `double d = 0.1;` is an exact value - but it isn't exactly 0.1. – Jon Skeet Nov 19 '10 at 09:53