4

I just came across this piece of code:

Dim d As Double

For i = 1 To 10
  d = d + 0.1
Next

MsgBox(d)
MsgBox(d = 1)
MsgBox(1 - d)

Can anyone explain me the reason for that? Why d is set to 1?

Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
Bartzilla
  • 2,768
  • 4
  • 28
  • 37

3 Answers3

8

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.

When comparing floating point values, you need to use a minimum value by which the values can differ and still be considered the same value (this value is normally known as the epsilon). This value depends on the application.

I suggest reading What Every Computer Scientist Should Know About Floating-Point Arithmetic for an in-depth discussion.


As for comaring 1 to 1.0 - these are different types so will not compare to each other.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
3

.1 (1/10th) is a repeating fraction when converted to binary:

.0001100110011001100110011001100110011.....

It would be like trying to show 1/3 as a decimal: you just can't do it accurately.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
2

This is because a double is always only an approximation of the value and not the exact value itself (like a floating point value). When you need an exact decimal value, instead use a Decimal.

Contrast with:

Dim d As Decimal

For i = 1 To 10
    d = d + 0.1
Next

MsgBox(1)
MsgBox(d = 1)
MsgBox(1 - d)
TomMhC
  • 133
  • 6