Yesterday I asked why a adding 10 times 0.10 to a double is not equal to int 1;
I got an excellent answer. The overview is because:
- Floating point types and integer types cannot be compared directly, as their binary representations are different.
- The result of adding 0.1 ten times as a floating point type may well be a value that is close to 1, but not exactly
I can see the reason why now. However, if I do something like:
Dim d As Double
For i = 1 To 4
d = d + 0.25
Next
MsgBox(d) 'output 1
MsgBox(d = 1) 'True
MsgBox(1 - d) ' 0
Console.WriteLine(d) '1
In this case I really obtain equality between double 1.0 and int 1. I thought then that double were only approximations so I would expect d to be somehow a value close to 1 as in the original first question. Why is not a good idea to compare directly different data types (in this case double - integer) and why I this time I obtain equality ??