用π/4=1-1/3+1/5-1/7+……级数求π的近似值(取前5000项)
发布网友
发布时间:2023-06-23 12:50
我来回答
共2个回答
热心网友
时间:2023-10-12 23:02
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
热心网友
时间:2023-10-12 23:02
调用函数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