'
'First, we define a sub-routine that reads in parameters from the spreadsheet,
' computes pi to a certain decimal accuracy,
' and writes the result to the spreadsheet.
'
Sub ComputePi()
'
'' Establish Communication With Spreadsheet ''
Set AccuracyCell = Worksheets("Approximating Pi").Range("ErrorBoundName")
Set ApproxCell = Worksheets("Approximating Pi").Range("MacroApproxName")
Set TermsNeededCell = Worksheets("Approximating Pi").Range("TermsNeededName")
' Read in the desired number of digits of accuracy
DigitsOfAccuracy = AccuracyCell.Value
' Sanity check on desired accuracy
If DigitsOfAccuracy <= 0 Then
ApproxCell.Value = "ERROR: Accuracy must be positive"
End
End If
'' Begin the Algorithm for computing pi ''
' Initialize s_0 = 4
CurrentApprox = 4
' Initialize a_1 = -4/3
n = 1
NextTerm = -4 / 3
' Initialize s_1 = s_0 + a_1
NextApprox = CurrentApprox + NextTerm
' Add more and more terms of the series, until adding the next term
' doesn't change the desired digits of the current approximation
'
' (By the alternating series estimation theorem,
' we will then have computed the desired number of digits of pi.)
'
While Not (EqualToDigits(CurrentApprox, NextApprox, DigitsOfAccuracy))
' The previous "next approximation" is now the "current approximation"
CurrentApprox = NextApprox
' Compute a_{n+1} and s_{n+1}
n = n + 1
NextTerm = 4 * (-1) ^ n / (2 * n + 1)
NextApprox = CurrentApprox + NextTerm
Wend
' The current approximation is correct
ApproxCell.Value = CurrentApprox
' The current approximation is the sum of the first "n" terms (we started with s_0)
TermsNeededCell.Value = n
'
'' End the Algorithm for computing pi ''
'
End Sub
'
' Next, we define a helper function that takes in two numbers,
' and checks they agree on the first "NumberDigits"-many digits
'
' RETURNS TRUE: if FirstNumber and SecondNumber agree on the first NumberDigits characters.
' OTHERWISE, returns FALSE.
' ASSUMES: FirstNumber and SecondNumber are both decimal numbers
' *with* a one's digit, and *with* *no* higher digits.
'
Function EqualToDigits(FirstNumber, SecondNumber, NumberDigits) As Boolean
' Obtain a string representation of the first NumberDigits+2 many digits in each number
' (each converted number appears to start with a blank space, and contains a decimal point character)
ChoppedFirstNumber = Left(Str(FirstNumber), NumberDigits + 2)
ChoppedSecondNumber = Left(Str(SecondNumber), NumberDigits + 2)
' Are the numbers identical up to the desired number of digits?
If (ChoppedFirstNumber = ChoppedSecondNumber) Then
EqualToDigits = True
Exit Function
Else
EqualToDigits = False
Exit Function
End If
End Function