Averages/Arithmetic mean: Difference between revisions
m
Fixed lang tags (using MediaWiki::API).
(Added Scala) |
Underscore (talk | contribs) m (Fixed lang tags (using MediaWiki::API).) |
||
Line 39:
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}}
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386 - note that some necessary LONG REAL operators are missing from ELLA's library.}}
<lang algol68>PROC mean = (REF[]REAL p)REAL:
# Calculates the mean of qty REALs beginning at p. #
IF LWB p > UPB p THEN 0.0
Line 52 ⟶ 51:
[6]REAL test := (1.0, 2.0, 5.0, -5.0, 9.5, 3.14159);
print((mean(test),new line))
)</lang>▼
▲</lang>
=={{header|AmigaE}}==
Line 216 ⟶ 214:
=={{header|Clojure}}==
<lang
(let [length (count sq)]
(if (zero? length)
0
(/ (reduce + sq) length)))
)</lang>▼
▲</lang>
=={{header|Common Lisp}}==
Line 290 ⟶ 286:
=={{header|Erlang}}==
<lang erlang>mean([]) -> 0;
mean(
=={{header|Factor}}==
Line 307 ⟶ 301:
=={{header|Forth}}==
<lang forth>
=={{header|Fortran}}==
In ISO Fortran 90 or later, use the SUM intrinsic, the SIZE intrinsic and the MAX intrinsic (to avoid divide by zero):
<lang fortran>
=={{header|F_Sharp|F#}}==
Line 386 ⟶ 380:
If you want to avoid keeping the list in memory and traversing it twice:
<lang haskell>{-# LANGUAGE BangPatterns #-}
import Data.List (foldl')
mean :: (Real n, Fractional m) => [n] -> m
mean xs = let (s,l) = foldl' f (0, 0) xs in realToFrac s / l
where f (!s,!l) x = (s+x,l+1)</lang>
=={{header|IDL}}==
Line 398 ⟶ 390:
If truly only the mean is wanted, one could use
<lang idl>
But <tt>mean()</tt> is just a thin wrapper returning the zeroth element of <tt>moment()</tt> :
Line 507 ⟶ 499:
=={{header|Mathematica}}==
Modify the built-in Mean function to give 0 for empty vectors (lists in Mathematica):
▲ Unprotect[Mean];
▲ Mean[{}] := 0
Examples:
<lang
gives (a set of integers gives back an integer or a rational, a set of floats gives back a float, a set of rationals gives a rational back, a list of symbols and numbers keeps the symbols exact and a mix of exact and approximate numbers gives back an approximate number):
<lang
▲ 4.53333
▲ -1.3835
▲ 0
▲ 77/240
▲ 1/5 (-3+2 a+c+Pi)
=={{header|MATLAB}}==
Line 655 ⟶ 641:
=={{header|Pop11}}==
<lang pop11>define mean(v);▼
▲define mean(v);
lvars n = length(v), i, s = 0;
if n = 0 then
Line 666 ⟶ 651:
endif;
return(s/n);
enddefine;</lang>
=={{header|PowerShell}}==
Line 701 ⟶ 685:
Output:
2.875</lang>
Line 714 ⟶ 698:
(Notice how the second call gave the wrong result)
0.0</lang>
{{works with|Python|2.4}}
Output:
=={{header|R}}==
Line 782 ⟶ 765:
=={{header|Slate}}==
<lang slate>[|:list| (list reduce: #+ `er ifEmpty: [0]) / (list isEmpty ifTrue: [1] ifFalse: [list size])] applyWith:
[|:list| (list reduce: #+ `er ifEmpty: [0]) / (list isEmpty ifTrue: [1] ifFalse: [list size])] applyWith:
▲[|:list| (list reduce: #+ `er ifEmpty: [0]) / (list isEmpty ifTrue: [1] ifFalse: [list size])] applyWith: {}.
=={{header|Smalltalk}}==
Line 831 ⟶ 812:
in
(real total / length)
end;</lang>
=={{header|Tcl}}==
Line 844 ⟶ 824:
=={{header|TI-89 BASIC}}==
=={{header|UnixPipes}}==
Line 894 ⟶ 874:
=={{header|Vedit macro language}}==
The numeric data is stored in current edit buffer as ASCII strings, one value per line.
<lang vedit>#1 = 0 // Sum
#2 = 0 // Count
BOF
Line 904 ⟶ 883:
}
if (#2) { #1 /= #2 }
Num_Type(#1)</lang>
|