Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in VB Why (1 = 1) is False

Tags:

vb.net

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?

like image 615
Bartzilla Avatar asked Dec 12 '22 09:12

Bartzilla


2 Answers

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.

like image 59
Oded Avatar answered Jan 08 '23 05:01

Oded


.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.

like image 37
Joel Coehoorn Avatar answered Jan 08 '23 06:01

Joel Coehoorn