Author Topic: Efficient Approximation  (Read 1513 times)

0 Members and 1 Guest are viewing this topic.

Charles Pegge

  • Guest
Efficient Approximation
« on: October 15, 2017, 03:51:43 AM »
An old example revisited with some FPU assembler. Using the FPU stack is, in theory, more efficient than loading variables from RAM.

This algorithm is quite complicated but it is highly efficient and converges on the answer with very few iterations.

Code: [Select]

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

  uses 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

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