Private Sub Form_Click()
Dim n As Integer, i As Integer
Dim a As Double, s As Double
n = 1
For i = 1 To 10000
a = 1 /拦陪 (2 * i - 1)
s = s + n * a
n = -n
Next i
Print "虚谨pi="简誉蠢 & s * 4
End Sub
调用函局派数裤厅Pi求近似胡腊隐π
pi(5000)=3.14119265360579
pi(99999999)=3.14159263359032
函数pi及示例代码如下:
Private Sub Command1_Click()
x = pi(5000)
Print x
End Sub
Function pi(i As Long) As Double
Dim m As Long, l As Long, pi4 As Double
For l = 1 To i Step 2
If m >= 0 Then m = -l Else m = l
pi4 = pi4 - 1 / m
Next
pi = 4 * pi4
End Function