Averages/Arithmetic mean: Difference between revisions

Content added Content deleted
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 266: Line 266:
RealF(s,mean([1.0, 2.0, 3.0, 4.0, 5.0]), 2))
RealF(s,mean([1.0, 2.0, 3.0, 4.0, 5.0]), 2))
ENDPROC</lang>
ENDPROC</lang>

=={{header|AntLang}}==
AntLang has a built-in avg function.
<lang AntLang>avg[list]</lang>

=={{header|APL}}==
{{works with|APL2}}
<lang apl> X←3 1 4 1 5 9
(+/X)÷⍴X
3.833333333</lang>


=={{header|Applesoft BASIC}}==
=={{header|Applesoft BASIC}}==
Line 286: Line 296:
N = A(0) : IF N THEN S = 0 : FOR I = 1 TO N : S = S + A(I) : NEXT : ? S / N
N = A(0) : IF N THEN S = 0 : FOR I = 1 TO N : S = S + A(I) : NEXT : ? S / N
</lang>
</lang>
=={{header|AntLang}}==
AntLang has a built-in avg function.
<lang AntLang>avg[list]</lang>


=={{header|Arturo}}==
=={{header|Arturo}}==
Line 348: Line 355:
zero-length input !
zero-length input !
</pre>
</pre>

=={{header|APL}}==
{{works with|APL2}}
<lang apl> X←3 1 4 1 5 9
(+/X)÷⍴X
3.833333333</lang>


=={{header|Babel}}==
=={{header|Babel}}==
Line 952: Line 953:
AveVal(SetVals) //returns 30.0 ;
AveVal(SetVals) //returns 30.0 ;
</lang>
</lang>

=={{header|Elena}}==
=={{header|Elena}}==
ELENA 5.0:
ELENA 5.0:
Line 1,049: Line 1,051:
10
10
</pre>
</pre>

=={{header|F_Sharp|F#}}==
The following computes the running mean using a tail-recursive approach. If we just sum all the values then divide by the number of values then we will suffer from overflow problems for large lists. See [[wp:Moving_average|wikipedia]] about the moving average computation.
<lang fsharp>let avg (a:float) (v:float) n =
a + (1. / ((float n) + 1.)) * (v - a)

let mean_series list =
let a, _ = List.fold_left (fun (a, n) h -> avg a (float h) n, n + 1) (0., 0) list in
a</lang>

Checking this:
<lang fsharp> > mean_series [1; 8; 2; 8; 1; 7; 1; 8; 2; 7; 3; 6; 1; 8; 100] ;;
val it : float = 10.86666667
> mean_series [] ;;
val it : float = 0.0</lang>

We can also make do with the built-in ''List.average'' function:
<lang fsharp>List.average [4;1;7;5;8;4;5;2;1;5;2;5]</lang>


=={{header|Factor}}==
=={{header|Factor}}==
Line 1,197: Line 1,217:
mean[x] := length[x] > 0 ? sum[x] / length[x] : undef
mean[x] := length[x] > 0 ? sum[x] / length[x] : undef
</lang>
</lang>

=={{header|F_Sharp|F#}}==
The following computes the running mean using a tail-recursive approach. If we just sum all the values then divide by the number of values then we will suffer from overflow problems for large lists. See [[wp:Moving_average|wikipedia]] about the moving average computation.
<lang fsharp>let avg (a:float) (v:float) n =
a + (1. / ((float n) + 1.)) * (v - a)

let mean_series list =
let a, _ = List.fold_left (fun (a, n) h -> avg a (float h) n, n + 1) (0., 0) list in
a</lang>

Checking this:
<lang fsharp> > mean_series [1; 8; 2; 8; 1; 7; 1; 8; 2; 7; 3; 6; 1; 8; 100] ;;
val it : float = 10.86666667
> mean_series [] ;;
val it : float = 0.0</lang>

We can also make do with the built-in ''List.average'' function:
<lang fsharp>List.average [4;1;7;5;8;4;5;2;1;5;2;5]</lang>


=={{header|GAP}}==
=={{header|GAP}}==
Line 1,708: Line 1,710:
print "Arithmetic mean: ";mean
print "Arithmetic mean: ";mean
</lang>
</lang>

=={{header|Limbo}}==
=={{header|Limbo}}==
<lang Limbo>implement Command;
<lang Limbo>implement Command;
Line 2,109: Line 2,112:
RETURN sum / FLOAT(Samples)
RETURN sum / FLOAT(Samples)
END Average;</lang>
END Average;</lang>

=={{header|MUMPS}}==
=={{header|MUMPS}}==
<lang MUMPS>MEAN(X)
<lang MUMPS>MEAN(X)
Line 2,289: Line 2,293:
=> 1.9619999999999997
=> 1.9619999999999997
</lang>
</lang>

=={{header|Oberon-2}}==
=={{header|Oberon-2}}==
Oxford Oberon-2
Oxford Oberon-2
Line 2,326: Line 2,331:
14.55
14.55
</pre>
</pre>

=={{header|Objeck}}==
=={{header|Objeck}}==
<lang objeck>
<lang objeck>
Line 2,538: Line 2,544:
print avg(qw(3 1 4 1 5 9)), "\n";</lang>
print avg(qw(3 1 4 1 5 9)), "\n";</lang>

=={{header|Perl 6}}==
{{works with|Rakudo|2015.10-11}}

<lang perl6>multi mean([]){ Failure.new('mean on empty list is not defined') }; # Failure-objects are lazy exceptions
multi mean (@a) { ([+] @a) / @a }</lang>


=={{header|Phix}}==
=={{header|Phix}}==
Line 2,563: Line 2,563:
else
else
echo "0\n";</lang>
echo "0\n";</lang>

=={{header|PL/I}}==
<lang pli>arithmetic_mean = sum(A)/dimension(A,1);</lang>


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
Line 2,575: Line 2,572:
<pre>: (mean (range 1 1000))
<pre>: (mean (range 1 1000))
-> 500</pre>
-> 500</pre>

=={{header|PL/I}}==
<lang pli>arithmetic_mean = sum(A)/dimension(A,1);</lang>


=={{header|Pop11}}==
=={{header|Pop11}}==
Line 2,740: Line 2,740:
(mean #(3 4 5 8)) ; -> 5
(mean #(3 4 5 8)) ; -> 5
</lang>
</lang>

=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2015.10-11}}

<lang perl6>multi mean([]){ Failure.new('mean on empty list is not defined') }; # Failure-objects are lazy exceptions
multi mean (@a) { ([+] @a) / @a }</lang>


=={{header|REBOL}}==
=={{header|REBOL}}==
Line 3,163: Line 3,170:
mean(a)
mean(a)
16715.75</lang>
16715.75</lang>

=={{header|Swift}}==
=={{header|Swift}}==
<lang swift>func meanDoubles(s: [Double]) -> Double {
<lang swift>func meanDoubles(s: [Double]) -> Double {
Line 3,209: Line 3,217:
alert( mean( [] ) );
alert( mean( [] ) );
</lang>
</lang>

=={{header|UnixPipes}}==
{{incorrect|UnixPipes|There is a race between parallel commands. <code>cat count</code> might try to read the file before <code>wc -l >count</code> writes it. This may cause an error like ''cat: count: No such file or directory'', then ''bc: stdin:1: syntax error: ) unexpected''.}}

Uses [[ksh93]]-style process substitution. Also overwrites the file named <tt>count</tt> in the current directory.
{{works with|bash}}
<lang bash>term() {
b=$1;res=$2
echo "scale=5;$res+$b" | bc
}

sum() {
(read B; res=$1;
test -n "$B" && (term $B $res) || (term 0 $res))
}

fold() {
func=$1
(while read a ; do
fold $func | $func $a
done)
}

mean() {
tee >(wc -l > count) | fold sum | xargs echo "scale=5;(1/" $(cat count) ") * " | bc
}

(echo 3; echo 1; echo 4) | mean</lang>


=={{header|UNIX Shell}}==
=={{header|UNIX Shell}}==
Line 3,265: Line 3,245:
printf "test 5: "; mean - # expr: syntax error
printf "test 5: "; mean - # expr: syntax error
printf "test 6: "; mean 1 2 A 3 # expr: non-numeric argument</lang>
printf "test 6: "; mean 1 2 A 3 # expr: non-numeric argument</lang>

=={{header|UnixPipes}}==
{{incorrect|UnixPipes|There is a race between parallel commands. <code>cat count</code> might try to read the file before <code>wc -l >count</code> writes it. This may cause an error like ''cat: count: No such file or directory'', then ''bc: stdin:1: syntax error: ) unexpected''.}}

Uses [[ksh93]]-style process substitution. Also overwrites the file named <tt>count</tt> in the current directory.
{{works with|bash}}
<lang bash>term() {
b=$1;res=$2
echo "scale=5;$res+$b" | bc
}

sum() {
(read B; res=$1;
test -n "$B" && (term $B $res) || (term 0 $res))
}

fold() {
func=$1
(while read a ; do
fold $func | $func $a
done)
}

mean() {
tee >(wc -l > count) | fold sum | xargs echo "scale=5;(1/" $(cat count) ") * " | bc
}

(echo 3; echo 1; echo 4) | mean</lang>


=={{header|Ursa}}==
=={{header|Ursa}}==
Line 3,292: Line 3,300:
output:
output:
<pre>1.600000e+00</pre>
<pre>1.600000e+00</pre>



=={{header|V}}==
=={{header|V}}==
Line 3,372: Line 3,379:
mean[1] = 0
mean[1] = 0
mean[] = Fout 2007</pre>
mean[] = Fout 2007</pre>

=={{header|VBScript}}==
=={{header|VBScript}}==
<lang vb>
<lang vb>
Line 3,440: Line 3,448:
Output:
Output:
<pre>2</pre>
<pre>2</pre>

=={{header|Wren}}==
<lang wren>class Arithmetic {
static mean(arr) {
if (arr.count == 0) Fiber.abort("Length must be greater than zero")
return arr.reduce(Fn.new{ |x,y| x+y }) / arr.count
}
}
Arithmetic.mean([1,2,3,4,5]) // 3
</lang>


=={{header|Wortel}}==
=={{header|Wortel}}==
Line 3,466: Line 3,464:
Returns:
Returns:
<pre>[3.5714285714285716 3.5714285714285716]</pre>
<pre>[3.5714285714285716 3.5714285714285716]</pre>

=={{header|Wren}}==
<lang wren>class Arithmetic {
static mean(arr) {
if (arr.count == 0) Fiber.abort("Length must be greater than zero")
return arr.reduce(Fn.new{ |x,y| x+y }) / arr.count
}
}
Arithmetic.mean([1,2,3,4,5]) // 3
</lang>


=={{header|XLISP}}==
=={{header|XLISP}}==