Advertisement
hectic101

AGM function with comments (While Loop)

Apr 12th, 2015
1,147
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function AGM(a As Double, b As Double) As Double
  2.     'Need vars: newA newB - Double
  3.     Dim newA As Double
  4.     Dim newB As Double
  5.     'Need var: epsilon
  6.     Dim epsilon As Double
  7.     epsilon = 0.00001
  8.    
  9.     'While |a-b| > epsilon
  10.     While abs(a - b) > epsilon
  11.         'Calc newA
  12.         newA = (a+b)/2
  13.         'Calc newB
  14.         newB = sqrt(a*b)
  15.         'Update a
  16.         a = newA
  17.         'Update b
  18.         b = newB
  19.     'Wend
  20.     Wend
  21.     AGM = a
  22.  
  23. End Function
Advertisement
RAW Paste Data Copied
Advertisement