'
'
' This is a sample program to use in the MMDebug tutorial  
'
'
dim sqr_lookup(25)

Prog_Start:
          hires_Pi = mypi()
          
          For a=1 To 10
              For b=1 To a
                  Print " ";
                  tmp_value = mysin(a)
              Next b
              root = squareroot(a)
              sqr_lookup(a) = root
              Print "squareroot(";a;") =  "; root
          Next a

          Print
          cnt = cnt + 1
          if cnt < 3 then qwerty = 37.342332 : c = 10.3 : s$ = s$ + "|" + str$(root) : goto Prog_Start
          Print
          Print "All Done!!!"
          Print


Function factorial(f)
local cnt%
         If f < 2 Then 
            factorial = 1  
         Else 
            if f < 10 then
               factorial = f * factorial(f-1)
            else
               factorial = factorial(9)
               for cnt% = 10 to f - 10
                   factorial = factorial * cnt%
               next cnt%
            endif
         endif
End Function


Function squareroot( n )
         Local u, l, cnt, t
         
         If n < 0 Then
            Print "?error in squareroot() function!   Parameter is negative: "; n
            goto Bad_error
         EndIf

         u = n+1 : l = 0 : cnt = 0     ' Setup all the initial values for the iteration
         
keep_going:
         cnt = cnt+1
         t = (u + l) / 2

         If t*t < n  Then l = t Else u = t

         If Abs(t*t - n) >  .00000000000001 Then GoTo keep_going

'
'        Print "Squareroot(";n;") took ";cnt;" iterations."
'
         squareroot = t
End Function


function mysin(theta)
local iii%, sss, last_sss
  sss = theta
  last_sss = theta
  for iii%=3 to 9999 step 4
    sss = sss - (theta ^ iii%) / factorial(iii%) 
    sss = sss + (theta ^ (iii% + 2)) / factorial(iii% + 2)
    if abs(sss - last_sss) < .00000000000000001 then exit for
    endif
    last_sss = sss
  next iii%
  mysin = sss
end function


function mycos(theta)
local iii%, ccc, last_ccc
  ccc = 1
  last_ccc = 1 + theta
  for iii%=2 to 9998 step 4
    ccc = ccc - (theta ^ iii%) / factorial(iii%) + (theta ^ (iii% + 2)) / factorial(iii% + 2)
    if abs(ccc - last_ccc) < .00000000000000001 then exit for
    last_ccc = ccc
  next iii%
  mycos = ccc
end function


function mypi()
local k%, s, last_s
  s = 0
  last_s = 0
  for k%=0 to 9999
    s = s + ((-1/4)^ k% ) * (((1/(2*k%+1)) + (2/(4*k%+1)) + (1/(4*k%+3))))
    if abs(s - last_s) < .00000000000000001 then exit for
    last_s = s
  next k%
  mypi = s
end function

Bad_Error:
           End
           
           
