求π【VB代码实现】

根据下面的公式,求π

精确到小数后6位。

代码1

Private Sub Command1_Click()
    Dim pai As Double, i As Double, item As Double
    pai = 0
    i = 1
    item = 1
    Do While item >= 0.000001
        pai = pai + (-1) ^ (i - 1) * item
        i = i + 1
        item = 1 / (2 * i - 1)
    Loop
    Label2.Caption = Str(4 * pai)
End Sub

代码2

Private Sub Command1_Click()
    Dim pai As Double, item As Double, i As Double
    Dim sign As Integer
    pai = 0
    i = 1
    sign = 1
    item = 1
    Do While item >= 0.000001
        pai = pai + sign * item
        sign = sign * -1
        i = i + 2
        item = 1 / i
    Loop
    Label2.Caption = Str(4 * pai)
End Sub

代码3

Private Sub Command1_Click()
    Dim pai As Double, item As Double, i As Double
    Dim sign As Integer
    pai = 1
    i = 1
    sign = 1
    item = 1
    Do While item >= 0.000001
        sign = sign * -1
        i = i + 2
        item = 1 / i
        pai = pai + sign * item
    Loop
    Label2.Caption = Str(4 * pai)
End Sub

  

posted @ 2017-06-05 11:24  daveylin  阅读(1093)  评论(0编辑  收藏  举报