Cumulative standard deviation: Difference between revisions

Content added Content deleted
(→‎[[Standard_Deviation#ALGOL 68]]: add a code sample in an object oriented style)
Line 46: Line 46:


<!-- {{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8.8d.fc9.i386]}} -->
<!-- {{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8.8d.fc9.i386]}} -->
Note: the use of a UNION to mimic C's enumerated types is "experimental" and probably not typical of "production code". However it is a good example of '''ALGOL 68'''s ''conformity CASE clause''.
Note: the use of a UNION to mimic C's enumerated types is "experimental" and probably not typical of "production code". However it is a example of '''ALGOL 68'''s ''conformity CASE clause'' useful for classroom dissection.
<lang Algol68>MODE VALUE = STRUCT(CHAR value),
<lang Algol68>MODE VALUE = STRUCT(CHAR value),
STDDEV = STRUCT(CHAR stddev),
STDDEV = STRUCT(CHAR stddev),
Line 98: Line 98:
printf(($"standard dev := "g(0,6)l$, sd))
printf(($"standard dev := "g(0,6)l$, sd))
)</lang>Output:
)</lang>Output:
<pre>
<pre>
standard dev := 2.000000
standard dev := 2.000000
</pre>
{{trans|python}}

{{works with|ALGOL 68|Standard - no extensions to language used}}

{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}}

<!-- {{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8.8d.fc9.i386]}} -->
A code sample in an object oriented style:
<lang Algol68>MODE STAT = STRUCT(
LONG REAL sum,
LONG REAL sum2,
INT num
);

OP INIT = (REF STAT new)REF STAT:
(init OF class stat)(new);

MODE CLASSSTAT = STRUCT(
PROC (REF STAT, LONG REAL #value#)VOID plusab,
PROC (REF STAT)LONG REAL stddev, mean, variance, count,
PROC (REF STAT)REF STAT init
);
CLASSSTAT class stat;

plusab OF class stat := (REF STAT self, LONG REAL value)VOID:(
num OF self +:= 1;
sum OF self +:= value;
sum2 OF self +:= value*value
);

OP +:= = (REF STAT lhs, LONG REAL rhs)VOID: # some syntatic sugar #
(plusab OF class stat)(lhs, rhs);

stddev OF class stat := (REF STAT self)LONG REAL:
long sqrt((variance OF class stat)(self));

OP STDDEV = ([]LONG REAL value)LONG REAL: ( # more syntatic sugar #
REF STAT stat = INIT LOC STAT;
FOR i FROM LWB value TO UPB value DO
stat +:= value[i]
OD;
(stddev OF class stat)(stat)
);

mean OF class stat := (REF STAT self)LONG REAL:
sum OF self/LONG REAL(num OF self);

variance OF class stat := (REF STAT self)LONG REAL:(
LONG REAL m = (mean OF class stat)(self);
sum2 OF self/LONG REAL(num OF self)-m*m
);

count OF class stat := (REF STAT self)LONG REAL:
num OF self;

init OF class stat := (REF STAT self)REF STAT:(
sum OF self := sum2 OF self := num OF self := 0;
self
);

[]LONG REAL value = ( 2,4,4,4,5,5,7,9 );
main:
(
printf(($"standard deviation operator = "g(0,6)l$, STDDEV value));

REF STAT stat = INIT LOC STAT;

FOR i FROM LWB value TO UPB value DO
stat +:= value[i]
OD;
printf(($"standard deviation = "g(0,6)l$, (stddev OF class stat)(stat)));
printf(($"mean = "g(0,6)l$, (mean OF class stat)(stat)));
printf(($"variance = "g(0,6)l$, (variance OF class stat)(stat)));
printf(($"count = "g(0,6)l$, (count OF class stat)(stat)))
)</lang>Output:
<pre>
standard deviation operator = 2.000000
standard deviation = 2.000000
mean = 5.000000
variance = 4.000000
count = 8.000000
</pre>
</pre>