
'SUCCESSIVE APPROXIMATION USING NEGATIVE FEEDBACK
'================================================

  use console
  '
  macro ff(a)
  '==========
   '
   'NULL EXPRESSIONS
   '
   '0
   '1
   'a^5-2               'roots of 2
   '(a*a*a) + (a*a) -3 'compound roots
   'a*a*a-2            'cube root of 2
   abs (tan(a/4)-1)   'pi
   'abs((1/a)-a+1)     'phi
   'tan(rad(a))-2      'spherical angle of icosahedron facet edge
  end macro

  function approx(float e1,e2) as extended
  ========================================
  dim as double a,a1,a2,b1,b2,fbk
  dim as long i
  a1=e1      ' first estimate of answer
  a2=e2      ' second estimate of answer
  b1=ff(a1)  ' first result
  b2=ff(a2)  ' second result
  print i tab a2 cr
  do
    if i>=1000 then exit do 'restrict iterations
    if a1=a2 then exit do   'input precsion limit
    if b1=b2 then exit do   'feedback precision limit
    '
    'fbk=(a2-a1)/(b2-b1)    'feedback
    'a1=a2                  'new a1 input
    'a2=a2-b2*fbk           'new a2 input
    'b1=b2                  'new b1 output
      fld  qword a2
      fld  st0
      fld  st0
      fsub qword a1
      fld  qword b2
      fld st0
      fld st0
      fsub qword b1
      fxch st1
      fstp qword b1
      fdivp          'fbk
      fmulp          'qword b2
      fsubp          'qword a2 
      fstp qword a2
      fstp qword a1
    b2=ff(a2)               'new b2 output
    i++
    '
    print i tab a2 cr
  end do
  return a2
  end function

'extended r=approx(-2,-1) ' lower est, upper est 
extended r=approx(3.1414,3.1415) ' lower est, upper est 
'print str(r)
waitkey
  