Taylor’s Expansion

I was just messing about earlier and having found a short PASCAL program to find e using Taylor’s expansion series, I wondered how long it would take me to rewrite it in ALGOL 68.
Since it is a very long time since I last used ALGOL 68 the answer was a little longer than I thought, because I’d forgotten just how fussy ALGOL 68 was about data types and coercion.

{ Taylors Expansion - 23 Jan 13

  Copyright (C) 2013  - MEJT

  This program is free software: you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by the Free
  Software Foundation, either version 3 of the License, or (at your option)
  any later version.

  This program is distributed in the hope that it will be useful, but 
  WITHOUT ANY WARRANTY; without even the implied warranty of ERCHANTABILITY
  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  for more details. 

  You should have received a copy of the GNU General Public License along
  with this program.  If not, see http://www.gnu.org/licenses/ 

}

PROGRAM taylors

BEGIN

   { Easier to declare as a variable than explicitly specifying the size of
     the constant every tine it is used.}

   REAL one := 1.0;
   REAL zero := 0.0; 
   REAL error := 0.0; { Could be greater then zero. }

   REAL count, last, next, diff;

   { Recursive routine to calculate the factorial of a REAL. }

   PROC factorial = (REAL number) REAL:
      IF number <= 1  THEN
         one
      ELSE
         number * factorial (number -1)
      FI;

   { Recursive routine to calculate the next term in the series. }

   PROC taylors = (REAL number) REAL:
      IF number < 1  THEN
         one
      ELSE
          (one/factorial(number) + taylors(number -1))
      FI;

   { Program to display succesive terms in Taylor's expansion series. }

   count := one;
   diff := one;
   last := zero;

   WHILE (diff > error) DO
      next := taylors(count);
      diff := next - last;
      write ((fixed (count, -3, 0), "  ", fixed (next, -18, 16), "  "));
      write ((fixed (diff, -18, 16), "  ",  newline));
      last := next;
      count := count + one
  OD
  
END

FINISH

Algol68g Notes

To run this example on a modern system using Algol68g then you will need to make a couple of minor changes to my code.

  • Comments should be delimited using ‘#’ rather than ‘{‘ and ‘}’
  • The ‘PROGRAM’ and ‘FINISH’ statements should be omitted, but you do need to start your program with an opening bracket (or ‘BEGIN’).
Advertisements
This entry was posted in Programming and tagged . Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s