home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / turbo55 / install / demos.arc / FIB8087.PAS < prev    next >
Pascal/Delphi Source File  |  1989-05-02  |  1KB  |  45 lines

  1.  
  2. { Copyright (c) 1985, 1989 by Borland International, Inc. }
  3.  
  4. {$N+,E+}
  5.  
  6. program Fib8087;
  7. {
  8.   Sample program from P-335 in the Owner's Handbook that
  9.   demonstrates how to avoid 8087 stack overflow in recursive
  10.   functions that use the 8087 math co-processor. Local variables
  11.   are used to store temporary results on the 8086 stack.
  12. }
  13.  
  14. var
  15.   i : integer;
  16.  
  17. function Fib(N : integer) : extended;
  18. { calculate the fibonacci sequence for N }
  19. var
  20.   F1, F2 : extended;
  21. begin
  22.   if N = 0 then
  23.     Fib := 0.0
  24.   else
  25.     if N = 1 then
  26.       Fib := 1.0
  27.     else
  28.     begin
  29.       (* Use this line instead of the 3 lines that follow this
  30.          comment to cause an 8087 stack overflow for values of
  31.          N >= 8:
  32.       Fib := Fib(N - 1) + Fib(N - 2);  { will cause overflow for N > 8 }
  33.       *)
  34.  
  35.       F1 := Fib(N - 1);         { store results in temporaries on 8086 }
  36.       F2 := Fib(N - 2);         { stack to avoid 8087 stack overflow }
  37.       Fib := F1 + F2;
  38.     end;
  39. end; { Fib }
  40.  
  41. begin
  42.   for i := 0 to 15 do
  43.     Writeln(i, '. ', Fib(i));
  44. end.
  45.