Tuesday, July 30, 2024

Had fun building demo programs - particularly the ones that calculated Pi

RELATIVELY NEW SPIGOT ALGORITHM IS A GREAT FIT FOR THE IBM 1130

In 1995, Stanley Rabinowitz and Stan Wanger published a paper on a spigot algorithm for calculating the value of Pi. It has a number of features which align well with the limitations of the 1130 hardware. 

This algorithm does not save any of the prior digits, calculating the next digit without needing to have stored a perhaps very long string of prior digits. An 1130 system with 4K or 8K words is going to have trouble with memory intensive approaches.

This algorithm strictly uses integer arithmetic, no floating point at all. The 1130 machine architecture only supports integers. Using multiplication and division instead of more complex mathematical operations, the algorithm is easily implemented. 

The number of digits that can be accurately produced by the algorithm from the paper is related to the number of stages used to calculate each digit. A stage has a fraction and a running value, accepting a carry from earlier stages and generating a carry value for the next stage. 

Each digit being produced involves some simple operations applied from the rightmost stage to the leftmost and the final stage value is the digit that is output. Starting over from right to left creates the next sequential digit of Pi. 

Each stage stores a fixed numerator and denominator of a fraction, plus stores the working value of the stage. Thus we need three words for each stage. To produce N digits accurately, this algorithm needs 3Pi*N words. If you want 100 digits correctly, it needs about 960 words of memory (320 stages). Thus even a 4K machine could generate more than 400 digits of Pi.  

The time it takes to calculate a round is 245 microseconds. Thus a round of 320 stages requires 78.4 milliseconds and this produces digits at the rate of 12.76 per second. The entire 100 digit output would take only 7.84 seconds. Not bad for a machine with an execution rate of only 33.3K multiplies or 12.5K divides per second. 

Since the demo monitor will be supporting multiple demonstrations, the programs I wrote only had 80 stages and thus produced just 25 digits of Pi accurately in a half a second (not counting typewriter output delays). The typewriter operates at 10 to 15 characters per second, so the actual demo takes a couple of seconds. 


2 comments:

  1. Hey Carl, as usual great progress you work animal :). If you happen to have the code of the pi program I would love to run this on our machine too. (Fortran or ASM?). I guess you developed it in the emulator? Have a nice day, Alex.

    ReplyDelete
    Replies
    1. Hi Alex

      I will send you the entire package.

      I wrote it in assembler and then converted it into a load file for the IBM 1130 simulator (which I also can load into a real 1130 with the exceedingly slow console loader).

      The version that simply displays each digit in the ACC with a WAIT instruction between them can be run without anything else, but the version that types on the console requires the demo monitor which handles interrupts and provides support routines such as printing a string of 1053 characters.

      Carl

      Delete