Averages/Arithmetic mean: Difference between revisions
Content deleted Content added
Merge RPL/2 and RPL, CLEAR added to first implementation to make it always work |
Added solution for EDSAC |
||
Line 1,059: | Line 1,059: | ||
AveVal(SetVals) //returns 30.0 ; |
AveVal(SetVals) //returns 30.0 ; |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|EDSAC order code}}== |
|||
Extends the RC task by finding the arithmetic mean for each of several data sets. Each data set is preceded by the number of data. A count of 0 is not an error but signals that there are no more data sets. |
|||
The program needs to avoid the possibility of arithmetic overflow, as pointed out in the F# solution. The moving average used there is not well-suited to EDSAC, on which division had to be done by calling a subroutine. After reading the number of data N, and leaving the trivial case N = 1 for separate treatment, the program first calculates 1/N, then multiplies each value by 1/N before adding it into the result. |
|||
<syntaxhighlight lang="edsac"> |
|||
[Averages/Arithmetic mean - Rosetta Code] |
|||
[EDSAC program (Initial Orders 2) to find and print the average of |
|||
a sequence of 35-bit fractional values. |
|||
Values are read from tape, preceded by an integer count.] |
|||
[Library subroutine M3, runs at load time and is then overwritten. |
|||
Prints header; here, last character sets teleprinter to figures.] |
|||
PF GK IF AF RD LF UF OF E@ A6F G@ E8F EZ PF |
|||
*!!!!!COUNT!!!!!!AVERAGE@&#.. [PZ] |
|||
[Main routine: must be at even address] |
|||
T214K GK |
|||
[0] PF PF [average value] |
|||
[2] PF PF [reciprocal of data count] |
|||
[4] PF [data count] |
|||
[5] PD [17-bit constant 1; also serves as '0' for printing] |
|||
[6] @F [carriage return] |
|||
[7] &F [line feed] |
|||
[8] !F [space] |
|||
[9] MF [dot (in figures mode)] |
|||
[10] K4096F [teleprinter null] |
|||
[Entry and outer loop] |
|||
[11] A11@ |
|||
G56F [call library subroutine R4, sets 0D := data count N] |
|||
SD E64@ [exit if N = 0] |
|||
T4F [clear acc] |
|||
AF T4@ [load and save N (assumed < 2^16)] |
|||
[18] A18@ G156F [print N (clears acc)] |
|||
TD [clear whole of 0D, including sandwich bit] |
|||
T4D [same for 4D] |
|||
A4@ S2F [acc := N - 2] |
|||
G66@ [jump to special action if N = 1] |
|||
A2F [restore N after test] |
|||
T5F [store N in 4D high word] |
|||
A5@ T1F [store 1 in 0D high word] |
|||
[29] A29@ G120F [call library subroutine D6, sets 0D := 0D/4D] |
|||
AD T2#@ [load and save 1/N] |
|||
T#@ [clear average] |
|||
S4@ [load -N] |
|||
[Inner loop] |
|||
[35] T4@ [update negative loop counter] |
|||
[36] A36@ G78F [read next datum to 0D (clears acc)] |
|||
H2#@ [mult reg := 1/N] |
|||
VD [acc := datum/N] |
|||
A#@ T#@ [add into average] |
|||
A4@ A5@ [increment negative loop counter] |
|||
G35@ [loop until counter = 0] |
|||
[45] O8@ O8@ [print 2 spaces] |
|||
[Print the average value. |
|||
NB: Library subroutine P1 requires non-negative input and prints only the |
|||
digits after the decimal point. Formatting has to be done by the caller.] |
|||
[47] A#@ [load average (order also serves as minus sign)] |
|||
G52@ [jump if average < 0] |
|||
TD [pass average to subroutine P1] |
|||
O65@ [print plus sign (or could be space)] |
|||
E56@ [join common code] |
|||
[52] TD [average < 0; clear acc] |
|||
S#@ TD [pass abs(average) to subroutine P1] |
|||
O47@ [print minus sign] |
|||
[56] O5@ O9@ [common code: print '0.'] |
|||
[58] A58@ G192F [call P1 to print abs(average)] |
|||
P8F [8 decimal places] |
|||
O6@ O7@ [print CR, LF] |
|||
E11@ [loop back always (because acc = 0)] |
|||
[Jump to here if data count = 0, means end of data] |
|||
[64] O10@ [print null to flush teleprinter buffer] |
|||
[65] ZF [halt the machine (order also serves as plus sign)] |
|||
[Jump to here if data count = 1] |
|||
[66] TF [clear acc] |
|||
[67] A67@ G78F [read datum to 0D] |
|||
AD T#@ [average := datum] |
|||
E45@ [jump to print the average] |
|||
[The following puts the entry address into location 50, |
|||
so that it can be accessed via the X parameter (see end of program). |
|||
This is done in case the data is input from a separate tape.] |
|||
T50K P11@ T11Z |
|||
[Library subroutine R4. |
|||
Input of one signed integer, returned in 0D.] |
|||
T56K |
|||
GKA3FT21@T4DH6@E11@P5DJFT6FVDL4FA4DTDI4FA4FS5@G7@S5@G20@SDTDT6FEF |
|||
[Library subroutine R3. |
|||
Input of one long signed decimal fraction, returned in 0D.] |
|||
T78K |
|||
GKT45KP26@TZA3FTHTDT4DA6HT9@H1HS4HT6FIFAFS4HE7HT7FV4DL8FADT4DA6FA5HG8@ |
|||
H2#HN4DLDYFTDT28#ZPFT27ZTFP610D@524DP5DPDIFS4HG37@S4DT4DT7FA1HT9@E18@ |
|||
[Library subroutine D6 - Division, accurate, fast. |
|||
36 locations, workspace 6D and 8D. |
|||
0D := 0D/4D, where 4D <> 0, -1.] |
|||
T120K |
|||
GKA3FT34@S4DE13@T4DSDTDE2@T4DADLDTDA4DLDE8@RDU4DLDA35@ |
|||
T6DE25@U8DN8DA6DT6DH6DS6DN4DA4DYFG21@SDVDTDEFW1526D |
|||
[Library subroutine P7: print strictly positive integer in 0D.] |
|||
T156K |
|||
GKA3FT26@H28#@NDYFLDT4DS27@TFH8@S8@T1FV4DAFG31@SFLDUFOFFFSF |
|||
L4FT4DA1FA27@G11@T28#ZPFT27ZP1024FP610D@524D!FO30@SFL8FE22@ |
|||
[Library subroutine P1: print non-negative fraction in 0D, without '0.'] |
|||
T192K |
|||
GKA18@U17@S20@T5@H19@PFT5@VDUFOFFFSFL4FTDA5@A2FG6@EFU3FJFM1F |
|||
[========================================================================== |
|||
On the original EDSAC, the following (without the whitespace and comments) |
|||
might have been input on a separate tape.] |
|||
E25K TX GK |
|||
EZ [define entry point] |
|||
PF [acc = 0 on entry] |
|||
[Counts and data values to be read by library subroutines R3 and R4 respectively. |
|||
Note (1) Sign comes *after* value (2) In the data, leading '0.' is omitted.] |
|||
7+ 1-2-3-4-5+2-3- |
|||
1+ 987654321+ |
|||
9+ 01+04+09+16+25+36+49+64+81+ |
|||
9+ 01-04+09-16+25-36+49-64+81- |
|||
[Daily minimum temperature (unit = 10 deg. C), Cambridge, UK, January 2000] |
|||
31+ 34+14+49+00+04+48+05+48+23-35-07-75+19+03+ |
|||
26+27+17-06-52+22-17+18+15+03-33-11-04-01-44+89+95+ |
|||
0+ |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
COUNT AVERAGE |
|||
7 -0.14285714 |
|||
1 +0.98765432 |
|||
9 +0.31666666 |
|||
9 -0.05000000 |
|||
31 +0.16774193 |
|||
</pre> |
|||
=={{header|Elena}}== |
=={{header|Elena}}== |