Averages/Arithmetic mean: Difference between revisions

m
Lang tags
m (F# added List.average)
m (Lang tags)
Line 3:
=={{header|Ada}}==
This example shows how to pass a zero length vector as well as a larger vector.
<lang ada>with Ada.Float_Text_Io; use Ada.Float_Text_Io;
with Ada.Text_IO; use Ada.Text_IO;
 
procedure Mean_Main is
type Vector is array(Positive range <>) of Float;
function Mean(Item : Vector) return Float is
Sum : Float := 0.0;
Result : Float := 0.0;
begin
for I in Item'range loop
Sum := Sum + Item(I);
end loop;
if Item'Length > 0 then
Result := Sum / Float(Item'Length);
end if;
return Result;
end Mean;
A : Vector := (3.0, 1.0, 4.0, 1.0, 5.0, 9.0);
begin
Put(Item => Mean(A), Fore => 1, Exp => 0);
New_Line;
-- test for zero length vector
Put(Item => Mean(A(1..0)), Fore => 1, Exp => 0);
New_Line;
end Mean_Main;</lang>
Output:
3.83333
Line 37:
{{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>
<pre>
PROC mean = (REF[]REAL p)REAL:
# Calculates the mean of qty REALs beginning at p. #
Line 51:
print((mean(test),new line))
)
</prelang>
 
=={{header|AmigaE}}==
Line 111:
=={{header|APL}}==
{{works with|APL2}}
<lang apl> X←3 1 4 1 5 9
(+/X)÷⍴X
3.833333333</lang>
 
=={{header|BASIC}}==
{{works with|QuickBasic|4.5}}
Assume the numbers are in a DIM named nums.
<lang qbasic>mean = 0
sum = 0;
FOR i = LBOUND(nums) TO UBOUND(nums)
sum = sum + nums(i);
NEXT i
size = UBOUND(nums) - LBOUND(nums) + 1
PRINT "The mean is: ";
IF size <> 0 THEN
PRINT (sum / size)
ELSE
PRINT 0
END IF</lang>
 
=={{header|C}}==
Line 211:
=={{header|Common Lisp}}==
 
<lang lisp>(defun mean (sequence)
(let ((length (length sequence)))
(if (zerop length)
0
(/ (reduce #'+ sequence) length))))</lang>
=={{header|D}}==
Using template to make the mean function work for higher-rank array.
<prelang d>module mean ;
import std.stdio ;
 
Line 246:
writefln("array : ", array.mean()) ;
writefln("multi : ", multi.mean()) ;
}</prelang>
=={{header|E}}==
 
Line 298:
=={{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 [http://en.wikipedia.org/wiki/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 rec f a n list =
match list with
| [] -> a
| h :: t -> f (avg a (float h) n) (n + 1) t
f 0. 0 list</lang>
 
Checking this:
Line 331:
=={{header|Haskell}}==
This function works if the element type is an instance of Fractional:
<lang haskell>mean :: (Fractional a) => [a] -> a
mean [] = 0
mean xs = sum xs / Data.List.genericLength xs<lang>
 
But some types, e.g. integers, are not Fractional; the following function works for all Real types:
<lang haskell>meanReals :: (Real a, Fractional b) => [a] -> b
meanReals = mean . map realToFrac</lang>
 
=={{header|IDL}}==
Line 343:
If truly only the mean is wanted, one could use
 
<lang idl> x = [3,1,4,1,5,9]
print,mean(x)</lang>
 
But <tt>mean()</tt> is just a thin wrapper returning the zeroth element of <tt>moment()</tt> :
 
<lang idl>print,moment(x)
; ==>
3.83333 8.96667 0.580037 -1.25081</lang>
 
which are mean, variance, skewness and kurtosis.
Line 358:
=={{header|J}}==
 
<lang j>mean=: +/ % #</lang>
 
That is, sum divided by the number of items. The verb also works on higher-ranked arrays. For example:
 
<lang j> mean 3 1 4 1 5 9
3.83333
mean $0 NB. $0 is a zero-length vector
0
x=: 20 4 ?@$ 0 NB. a 20-by-4 table of random (0,1) numbers
mean x
0.58243 0.402948 0.477066 0.511155</lang>
 
The computation can also be written as a loop. It is shown here for comparison only and is highly non-preferred compared to the version above.
 
<lang j>mean1=: 3 : 0
z=. 0
for_i. i.#y do. z=. z+i{y end.
z % #y
)
mean1 3 1 4 1 5 9
3.83333
mean1 $0
0
mean1 x
0.58243 0.402948 0.477066 0.511155</lang>
 
=={{header|Java}}==
{{works with|Java|1.5+}}
 
Assume the numbers are in a double array called "nums".
<lang java5>...
...
double mean = 0;
double sum = 0;
for(double i : nums){
sum += i;
}
System.out.println("The mean is: " + ((nums.length != 0) ? (sum / nums.length) : 0));
...</lang>
 
=={{header|JavaScript}}==
<lang javascript>function mean(array) {
var sum = 0;
for(var i in array)
sum += array[i];
return array.length ? sum / array.length : 0;
}
 
alert( mean( [1,2,3,4,5] ) ); // 3</lang>
 
{{libheader|Functional}}
<lang javascript>function mean(a) {
return a.length ? Functional.reduce('+', 0, a) / a.length : 0;
}</lang>
}
 
=={{header|Logo}}==
<lang logo>to average :l
if empty? :l [output 0]
output quotient apply "sum :l count :l
end
print average [1 2 3 4] ; 2.5</lang>
 
=={{header|Lucid}}==
 
[http://en.wikipedia.org/wiki/Lucid_(programming_language)]
<lang lucid>avg(x)
where
sum = first(x) fby sum + next(x);
n = 1 fby n + 1;
avg = sum / n;
end</lang>
 
=={{header|M4}}==
Line 440 ⟶ 442:
 
=={{header|MAXScript}}==
<lang maxscript>fn mean data =
(
total = 0
for i in data do
(
total += i
)
if data.count == 0 then 0 else total as float/data.count
)
 
print (mean #(3, 1, 4, 1, 5, 9))</lang>
 
=={{header|Nial}}==
in the standard way, mean is
<lang nial>mean is / [sum, tally]
 
mean 6 2 4
= 4</lang>
but it fails with 0 length vectors. so using a tally with a minimum value 1
 
<lang nial>dtally is recur [ empty rest, 1 first, 1 first, plus, rest ]
mean is / [sum, dtally]
 
mean []
=0</lang>
=0
 
=={{header|OCaml}}==
Line 709 ⟶ 711:
 
=={{header|V}}==
<lang v>[mean
[sum 0 [+] fold].
dup sum
swap size [[1 <] [1]] when /
].</lang>
].
Anonymous user