这段VB代码错在哪里

来源:百度知道 编辑:UC知道 时间:2024/09/23 18:25:22
Dim s As Integer, n As Integer, t As Integer
s = 1 / 2
t = 1
n = 1
Do
n = n + 1
t = t * n
s = s + 1 / ((2 ^ n) * t)
Loop Until 1 / ((2 ^ n) * t) < 0.000001
Print s

s 应该用 Double 或者Single 类型
n 用 Long, n 用 Integer n 算到9 就溢出了

Private Sub Command1_Click()
Dim s As Double, n As Long, t As Long
s = 1 / 2
t = 1
n = 1
Do
n = n + 1
t = t * n
s = s + 1 / ((2 ^ n) * t)
Loop Until 1 / ((2 ^ n) * t) < 0.000001
Print s
End Sub

Loop Until 1.0f / ((2 ^ n) * t) < 0.000001

s As Integer 整型 必须为整数,
s=1/2不为整数,
s As Double 就ok,代码上面有

s不该是整型!

类型问题