The problem with π (iv) – Fortran to Ada
The Fortran program can also be translated to Ada. Below is the Ada program. It contains some code to output the resulting value of pi to the text file piCalc1000ADA.txt
. In this code a
is a “dynamic” array of integers, which is allocated using a declare
block. In reality a
is just of type pia
, declared at a later point in the program.
with ada.Text_IO; use Ada.Text_IO; with ada.Integer_Text_IO; use Ada.Integer_Text_IO; with ada.strings.unbounded; use ada.strings.unbounded; with ada.strings.unbounded.Text_IO; use ada.strings.unbounded.Text_IO; procedure piSpigotDYN is n, len : integer; q, x, nines, predigit : integer; type pia is array(integer range <>) of integer; infp : file_type; begin create(infp,out_file,"piCalc1000ADA.txt"); n := 1000; len := 10 * n / 3; declare a : pia(1..len); begin a := (1..len => 2); nines := 0; predigit := 0; for j in 1..n loop q := 0; for i in reverse 1..len loop x := 10 * a(i) + q * i; a(i) := x mod (2*i-1); q := x / (2*i-1); end loop; a(1) := q mod 10; q := q / 10; if q = 9 then nines := nines + 1; elsif q = 10 then put(infp,predigit+1,width=>1); for k in 1..nines loop put(infp,0,width=>1); end loop; predigit := 0; nines := 0; else put(infp,predigit,width=>1); predigit := q; if nines /= 0 then for k in 1..nines loop put(infp,9,width=>1); nines := 0; end loop; end if; end if; end loop; put(infp,predigit,width=>1); close(infp); end; end piSpigotDYN;