我有一个程序段:For循环
dim y1BeginData as single=-4.0f
dim y1KdValue as singel=0.8f
......
for ykdIdx as integer=0 to 10
dim ykdV as single=y1BeginData+ykdIdx*y1KdValue
......
next
Visual Basic 复制代码
Dim oneThird As Double = 1.0 / 3.0
Dim pointThrees As Double = 0.333333333333333
' The following comparison does not indicate equality.
Dim exactlyEqual As Boolean = (oneThird = pointThrees)
' The following comparison indicates equality.
Dim closeEnough As Double = 0.000000000000001
Dim absoluteDifference As Double = Math.Abs(oneThird - pointThrees)
Dim practicallyEqual As Boolean = (absoluteDifference < closeEnough)
使用文本类型字符 D 将文本强制转换为 Decimal,以防文本的值相对于 Long 数据类型太大。
下面的示例演示了浮点操作数潜在的不精确性。
Visual Basic 复制代码
Dim two As Double = 2.0
Dim zeroPointTwo As Double = 0.2
Dim quotient As Double = two / zeroPointTwo
Dim doubleRemainder As Double = two Mod zeroPointTwo
MsgBox("2.0 is represented as " & two.ToString("G17") _
& vbCrLf & "0.2 is represented as " & zeroPointTwo.ToString("G17") _
& vbCrLf & "2.0 / 0.2 generates " & quotient.ToString("G17") _
& vbCrLf & "2.0 Mod 0.2 generates " _
& doubleRemainder.ToString("G17"))
Dim decimalRemainder As Decimal = 2D Mod 0.2D
MsgBox("2.0D Mod 0.2D generates " & CStr(decimalRemainder))