Comparing a Single to a Double in VB.Net
Here's my test code:
Dim testSingle As Single = 7.2
Dim testSingleF As Single = 7.2F
Dim testDouble As Double = 7.2
If testSingle = testDouble Then开发者_开发技巧 ' this is false
Label1.Text = "true"
Else
Label1.Text = "false"
End If
If testSingleF = testDouble Then ' this is false
Label2.Text = "true"
Else
Label2.Text = "false"
End If
If testSingle = 7.2F Then ' this is true
Label3.Text = "true"
Else
Label3.Text = "false"
End If
As you can see by my comments, the first two statements are false, and the third is true. Why is that? The precision shouldn't matter as it's such a small number.
What's going on here?
Yes, precision matters even though it is a small number. Why? Because 7.2 has an infinite number of digits in binary notation: 7.2
(dec) = 111.001100110011...
(bin) -- just like 10/3 = 3.333... in decimal notation.
Thus, Single
and Double
are bad choices if you need to accurately represent non-integer numbers. You have the following options:
- Use
Double
, but never compare numbers for absolute equalitya = b
. Instead, check ifAbs(a-b)
is smaller than some small threshold. - Use the
Decimal
data type, which has been created exactly for this purpose. In a nutshell, it stores72
(which can be represented exactly in binary) plus the information that there is one digit at the right of the decimal point. Thus, these problems don't occur here (at least not for numbers which can be represented accurately in decimal notation. 10/3 is still a problem here...)
Further information can be found, for example, on Wikipedia.
精彩评论