开发者

in VB Why (1 = 1) is False

开发者 https://www.devze.com 2023-04-05 23:34 出处:网络
I just开发者_JAVA百科 came across this piece of code: Dim d As Double For i = 1 To 10 d = d + 0.1 Next

I just开发者_JAVA百科 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?


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.


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


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

精彩评论

暂无评论...
验证码 换一张
取 消