Averages/Arithmetic mean

From Rosetta Code

Jump to: navigation, search
Task
Averages/Arithmetic mean
You are encouraged to solve this task according to the task description, using any language you may know.
Write a program to find the mean (arithmetic average) of a numeric vector. The program should work on a zero-length vector (with an answer of 0).

See also: Median, Mode

Contents

[edit] 6502 Assembly

Called as a subroutine (i.e., JSR ArithmeticMean), this calculates the integer average of up to 255 8-bit unsigned integers. The address of the beginning of the list of integers is in the memory location ArrayPtr and the number of integers is in the memory location NumberInts. The arithmetic mean is returned in the memroy location ArithMean.

ArithmeticMean:		PHA
TYA
PHA ;push accumulator and Y register onto stack
 
 
LDA #0
STA Temp
STA Temp+1 ;temporary 16-bit storage for total
 
LDY NumberInts
BEQ Done ;if NumberInts = 0 then return an average of zero
 
DEY ;start with NumberInts-1
AddLoop: LDA (ArrayPtr),Y
CLC
ADC Temp
STA Temp
LDA Temp+1
ADC #0
STA Temp+1
DEY
CPY #255
BNE AddLoop
 
LDY #-1
DivideLoop: LDA Temp
SEC
SBC NumberInts
STA Temp
LDA Temp+1
SBC #0
STA Temp+1
INY
BCS DivideLoop
 
STY ArithMean ;store result here
 
Done: PLA ;restore accumulator and Y register from stack
TAY
PLA
RTS ;return from routine

[edit] ActionScript

function mean(vector:Vector.<Number>):Number
{
var sum:Number = 0;
for(var i:uint = 0; i < vector.length; i++)
sum += vector[i];
return vector.length == 0 ? 0 : sum / vector.length;
}

[edit] Ada

This example shows how to pass a zero length vector as well as a larger vector.

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;

Output:

3.83333
0.00000

[edit] ALGOL 68

Translation of: C

Works with: ALGOL 68 version Standard - no extensions to language used Works with: ALGOL 68G version Any - tested with release mk15-0.8b.fc9.i386 Works with: ELLA ALGOL 68 version 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.

PROC mean = (REF[]REAL p)REAL:
# Calculates the mean of qty REALs beginning at p. #
IF LWB p > UPB p THEN 0.0
ELSE
REAL total := 0.0;
FOR i FROM LWB p TO UPB p DO total +:= p[i] OD;
total / (UPB p - LWB p + 1)
FI;
 
main:(
[6]REAL test := (1.0, 2.0, 5.0, -5.0, 9.5, 3.14159);
print((mean(test),new line))
)

[edit] AmigaE

Because of the way Amiga E handles floating point numbers, the passed list/vector must contain all explicitly floating point values (e.g., you need to write "1.0", not "1")

PROC mean(l:PTR TO LONG)
DEF m, i, ll
ll := ListLen(l)
IF ll = 0 THEN RETURN 0.0
m := 0.0
FOR i := 0 TO ll-1 DO m := !m + l[i]
m := !m / (ll!)
ENDPROC m
 
PROC main()
DEF s[20] : STRING
WriteF('mean \s\n',
RealF(s,mean([1.0, 2.0, 3.0, 4.0, 5.0]), 2))
ENDPROC

[edit] AutoHotkey

i = 10
Loop, % i {
Random, v, -3.141592, 3.141592
list .= v "`n"
sum += v
}
MsgBox, % i ? list "`nmean: " sum/i:0

[edit] AWK

# work around a gawk bug in the length extended use:
# so this is a more non-gawk compliant way to get
# how many elements are in an array
function elength(v)
{
l=0
for(el in v) l++
return l
}
 
function mean(v)
{
if (elength(v) < 1) { return 0 }
sum = 0
for(i=0; i < elength(v); i++) {
sum += v[i]
}
return sum/elength(v)
}
 
BEGIN {
# fill a vector with random numbers
for(i=0; i < 10; i++) {
vett[i] = rand()*10
}
print mean(vett)
}

[edit] APL

Works with: APL2

      X←3 1 4 1 5 9
(+/X)÷⍴X
3.833333333

[edit] BASIC

Works with: QuickBasic version 4.5

Assume the numbers are in a DIM named nums.

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

[edit] Brainf***

This example is under development. It was marked thus on 02/12/2009. Please help complete the example.

This code is an infinite loop if the average isn't a whole number. I don't have the time, can someone fix it to work with all numbers, not just single-digit numbers?

>,[-<+>>+<],<[->-<]>[-->+<]>.

[edit] C

This implementation uses a plain old static array of doubles for the numeric vector.

#include <stdio.h>
 
double mean(double *p, unsigned qty)
/* Calculates the mean of qty doubles beginning at p. */
{if (qty == 0) return 0;
double total = 0;
for (int i = 0 ; i < qty ; ++i) total += p[i];
return total / qty;}
 
int main(void)
{double test[6] = {1.0, 2.0, 5.0, -5.0, 9.5, 3.14159};
printf("%lg\n", mean(test, 6));
return 0;}

[edit] C++

Library: STL

#include <vector>
 
double mean(const std::vector<double>& numbers)
{
if (numbers.size() == 0)
return 0;
 
double sum = 0;
for (std::vector<double>::iterator i = numbers.begin(); i != numbers.end(); i++)
sum += *i;
return sum / numbers.size();
}

Shorter (and more idiomatic) version:

#include <vector>
#include <algorithm>
 
double mean(const std::vector<double>& numbers)
{
if (numbers.empty())
return 0;
return std::accumulate(numbers.begin(), numbers.end(), 0.0) / numbers.size();
}

Idiomatic version templated on any kind of iterator:

#include <iterator>
#include <algorithm>
 
template <typename Iterator>
double mean(Iterator begin, Iterator end)
{
if (begin == end)
return 0;
return std::accumulate(begin, end, 0.0) / std::distance(begin, end);
}

[edit] C#

using System.Linq;
 
static double avg(ICollection<int> i)
{
if (i == null || i.Count == 0) return 0;
return i.Sum() / (double)i.Count;
}
 
static void Main(string[] args)
{
int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8};
Console.WriteLine(avg(numbers));
}

C# already has a builtin Average function.

static void Main(string[] args)
{
int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8};
Console.WriteLine(numbers.Average());
}

[edit] Chef

Mean.
 
Chef has no way to detect EOF, so rather than interpreting
some arbitrary number as meaning "end of input", this program
expects the first input to be the sample size. Pass in the samples
themselves as the other inputs. For example, if you wanted to
compute the mean of 10, 100, 47, you could pass in 3, 10, 100, and
47. To test the "zero-length vector" case, you need to pass in 0.
 
Ingredients.
0 g Sample Size
0 g Counter
0 g Current Sample
 
Method.
Take Sample Size from refrigerator.
Put Sample Size into mixing bowl.
Fold Counter into mixing bowl.
Put Current Sample into mixing bowl.
Loop Counter.
Take Current Sample from refrigerator.
Add Current Sample into mixing bowl.
Endloop Counter until looped.
If Sample Size.
Divide Sample Size into mixing bowl.
Put Counter into 2nd mixing bowl.
Fold Sample Size into 2nd mixing bowl.
Endif until ifed.
Pour contents of mixing bowl into baking dish.
 
Serves 1.

[edit] Clojure

(defn mean [sq]
(let [length (count sq)]
(if (zero? length)
0
(/ (reduce + sq) length)))
)

[edit] Common Lisp

With Reduce

(defun mean (sequence)
(let ((length (length sequence)))
(if (zerop length)
0
(/ (reduce #'+ sequence) length))))

With Loop

(defun mean (sequence)
(loop for i in sequence
with length = (length sequence)
summing i into result
finally
return (/ result length)))

The loop version is buggy if sequence is the empty list.

[edit] D

Using template to make the mean function work for higher-rank array.

module mean ;
import std.stdio ;
 
real mean(T)(T[] a) {
static if(is(T U : U[])) {
// recursively unfold the multi-array
T u ;
foreach(e ; a)
u ~= e ;
return u.mean() ;
 
} else {
// do the math
if(a.length == 0) return 0.0 ;
real sum = 0.0 ;
foreach(e ; a)
sum += e ;
return sum / a.length ;
}
}
void main() {
int[] array = [3,1,4,1,5,9];
real[][][]
multi = [[[1,2,2],[2,3,4],[4,5,7]],
[[4,1,3],[0,3,1],[4,4,6]],
[[1,3,3],[2,7,8],[9,1,5]]] ;
writefln("array : ", array.mean()) ;
writefln("multi : ", multi.mean()) ;
}

[edit] E

Slightly generalized to support any object that allows iteration.

def meanOrZero(numbers) {
var count := 0
var sum := 0
for x in numbers {
sum += x
count += 1
}
return sum / 1.max(count)
}

[edit] Erlang

mean([]) -> 0;
mean(L) -> lists:sum(L)/length(L).

[edit] Factor

USING: math math.statistics ;
 
: arithmetic-mean ( seq -- n )
[ 0 ] [ mean ] if-empty ;

Tests:

( scratchpad ) { 2 3 5 } arithmetic-mean >float
3.333333333333333

[edit] Forth

: fmean ( addr n -- f )
0e
dup 0= if 2drop exit then
tuck floats bounds do
i f@ f+
1 floats +loop
0 d>f f/ ;
 
create test 3e f, 1e f, 4e f, 1e f, 5e f, 9e f,
test 6 fmean f. \ 3.83333333333333

[edit] Fortran

In ISO Fortran 90 or later, use the SUM intrinsic, the SIZE intrinsic and the MAX intrinsic (to avoid divide by zero):

real, target, dimension(100) :: a = (/ (i, i=1, 100) /)
real, dimension(5,20) :: b = reshape( a, (/ 5,20 /) )
real, pointer, dimension(:) :: p => a(2:1) ! pointer to zero-length array
real :: mean, zmean, bmean
real, dimension(20) :: colmeans
real, dimension(5) :: rowmeans
 
mean = sum(a)/size(a) ! SUM of A's elements divided by SIZE of A
mean = sum(a)/max(size(a),1) ! Same result, but safer code
! MAX of SIZE and 1 prevents divide by zero if SIZE == 0 (zero-length array)
 
zmean = sum(p)/max(size(p),1) ! Here the safety check pays off. Since P is a zero-length array,
! expression becomes "0 / MAX( 0, 1 ) -> 0 / 1 -> 0", rather than "0 / 0 -> NaN"
 
bmean = sum(b)/max(size(b),1) ! multidimensional SUM over multidimensional SIZE
 
rowmeans = sum(b,1)/max(size(b,2),1) ! SUM elements in each row (dimension 1)
! dividing by the length of the row, which is the number of columns (SIZE of dimension 2)
colmeans = sum(b,2)/max(size(b,1),1) ! SUM elements in each column (dimension 2)
! dividing by the length of the column, which is the number of rows (SIZE of dimension 1)

[edit] 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 wikipedia about the moving average computation.

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

Checking this:

> 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

We can also make do with the built-in List.average function

List.average [4;1;7;5;8;4;5;2;1;5;2;5]

[edit] Groovy

def avg = { list -> list == [] ? 0 : list.sum() / list.size() }

Test Program:

println avg(0..9)
println avg([2,2,2,4,2])
println avg ([])

Output:

4.5
2.4
0

[edit] Haskell

This function works if the element type is an instance of Fractional:

mean :: (Fractional a) => [a] -> a
mean [] = 0
mean xs = sum xs / Data.List.genericLength xs

But some types, e.g. integers, are not Fractional; the following function works for all Real types:

meanReals :: (Real a, Fractional b) => [a] -> b
meanReals = mean . map realToFrac

If you want to avoid keeping the list in memory and traversing it twice:

{-# 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)

[edit] HicEst

REAL :: vec(100)               ! no zero-length arrays in HicEst
 
vec = $ - 1/2 ! 0.5 ... 99.5
mean = SUM(vec) / LEN(vec) ! 50
END

[edit] Icon and Unicon

[edit] Icon

procedure main(args)
every (s := 0) +:= !args
write((real(s)/(0 ~= *args)) | 0)
end

Sample outputs:

->am 1 2 3 4 5 6 7
4.0
->am
0
->

[edit] Unicon

The Icon solution also works in Unicon.

[edit] IDL

If truly only the mean is wanted, one could use

x = [3,1,4,1,5,9]
print,mean(x)

But mean() is just a thin wrapper returning the zeroth element of moment() :

print,moment(x)
; ==>
3.83333 8.96667 0.580037 -1.25081

which are mean, variance, skewness and kurtosis.

There are no zero-length vectors in IDL. Every variable has at least one value or otherwise it is <Undefined>.

[edit] J

mean=: +/ % #

That is, sum divided by the number of items. The verb also works on higher-ranked arrays. For example:

   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

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.

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

[edit] Java

Works with: Java version 1.5+

Assume the numbers are in a double array called "nums".

...
double sum = 0;
for(double i : nums){
sum += i;
}
System.out.println("The mean is: " + ((nums.length != 0) ? (sum / nums.length) : 0));
...

[edit] 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

Library: Functional

function mean(a) {
return a.length ? Functional.reduce('+', 0, a) / a.length : 0;
}

[edit] 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

[edit] Lua

function mean (numlist)
if type(numlist) ~= 'table' then return numlist end
num = 0
table.foreach(numlist,function(i,v) num=num+v end)
return num / #numlist
end
 
print (mean({3,1,4,1,5,9}))

[edit] Lucid

avg(x)
where
sum = first(x) fby sum + next(x);
n = 1 fby n + 1;
avg = sum / n;
end

[edit] M4

M4 handle only integers, so in order to have a slightly better math for the mean, we must pass to the mean macro integers multiplied by 100. The macro rmean could embed the macro fmean and extractdec directly, but it is a little bit clearer to keep them separated.

define(`extractdec', `ifelse(eval(`$1%100 < 10'),1,`0',`')eval($1%100)')dnl
define(`fmean', `eval(`($2/$1)/100').extractdec(eval(`$2/$1'))')dnl
define(`mean', `rmean(`$#', $@)')dnl
define(`rmean', `ifelse(`$3', `', `fmean($1,$2)',dnl
`rmean($1, eval($2+$3), shift(shift(shift($@))))')')dnl
mean(0,100,200,300,400,500,600,700,800,900,1000)

[edit] Mathematica

Modify the built-in Mean function to give 0 for empty vectors (lists in Mathematica):

Unprotect[Mean];
Mean[{}] := 0

Examples:

Mean[{3,4,5}]
Mean[{3.2,4.5,5.9}]
Mean[{-4, 1.233}]
Mean[{}]
Mean[{1/2,1/3,1/4,1/5}]
Mean[{a,c,Pi,-3,a}]

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):

4
4.53333
-1.3835
0
77/240
1/5 (-3+2 a+c+Pi)

[edit] MATLAB

function meanValue = findmean(setOfValues)
meanValue = mean(setOfValues);
end

[edit] 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))

[edit] Nial

in the standard way, mean is

mean is / [sum, tally] 
 
mean 6 2 4
= 4

but it fails with 0 length vectors. so using a tally with a minimum value 1

dtally is recur [ empty rest, 1 first, 1 first, plus, rest ]
mean is / [sum, dtally]
 
mean []
=0

[edit] Niue

 
[ [ , len 1 - at ! ] len 3 - times swap , ] 'map ; ( a Lisp like map, to sum the stack )
[ len 'n ; [ + ] 0 n swap-at map n / ] 'avg ;
 
1 2 3 4 5 avg .
=> 3
3.4 2.3 .01 2.0 2.1 avg .
=> 1.9619999999999997
 

[edit] Objeck

 
function : native : PrintAverage(values : FloatVector) ~ Nil {
values->Average()->PrintLine();
}
 

[edit] OCaml

These functions return a float:

let mean_floats = function
| [] -> 0.
| xs -> List.fold_left (+.) 0. xs /. float_of_int (List.length xs)
 
let mean_ints xs = mean_floats (List.map float_of_int xs)

the previous code is easier to read and understand, though if you wish the fastest implementation to use in production code notice several points: it is possible to save a call to List.length computing the length through the List.fold_left, and for mean_ints it is possible to save calling float_of_int on every numbers, converting only the result of the addition. (also when using List.map and when the order is not important, you can use List.rev_map instead to save an internal call to List.rev). Also the task asks to return 0 on empty lists, but in OCaml this case would rather be handled by an exception.

let mean_floats xs =
if xs = [] then
invalid_arg "empty list"
else
let total, length =
List.fold_left
(fun (tot,len) x -> (x +. tot), len +. 1.)
(0., 0.) xs
in
(total /. length)
;;
 
 
let mean_ints xs =
if xs = [] then
invalid_arg "empty list"
else
let total, length =
List.fold_left
(fun (tot,len) x -> (x + tot), len +. 1.)
(0, 0.) xs
in
(float total /. length)
;;

[edit] Octave

GNU Octave has a mean function (from statistics package), but it does not handle an empty vector; an implementation that allows that is:

function m = omean(l)
if ( numel(l) == 0 )
m = 0;
else
m = mean(l);
endif
endfunction
 
disp(omean([]));
disp(omean([1,2,3]));

[edit] Oz

A version working on floats:

declare
fun {Mean Xs}
{FoldL Xs Number.'+' 0.0} / {Int.toFloat {Length Xs}}
end
in
{Show {Mean [3. 1. 4. 1. 5. 9.]}}

[edit] Perl

sub avg {
@_ or return 0;
my $sum = 0;
$sum += $_ foreach @_;
return $sum/@_;
}
 
print avg(qw(3 1 4 1 5 9)), "\n";

Library: Data::AverageAverage With module Data::Average. (For zero-length arrays, returns the empty list.)

use Data::Average;
 
my $d = Data::Average->new;
$d->add($_) foreach qw(3 1 4 1 5 9);
print $d->avg, "\n";

[edit] Perl 6

Works with: Rakudo version #21 "Seattle"

sub mean (@a) { ([+] @a) / (@a || 1) }

[edit] PHP

$nums = array(3, 1, 4, 1, 5, 9);
if ($nums)
echo array_sum($nums) / count($nums), "\n";
else
echo "0\n";

[edit] PL/I

 
arithmetic_mean = sum(A)/dimension(A,1);
 

[edit] PicoLisp

(de mean (Lst)
(if (atom Lst)
0
(/ (apply + Lst) (length Lst)) ) )

Output:

: (mean (range 1 1000))
-> 500

[edit] Pop11

define mean(v);
lvars n = length(v), i, s = 0;
if n = 0 then
return(0);
else
for i from 1 to n do
s + v(i) -> s;
endfor;
endif;
return(s/n);
enddefine;

[edit] PostScript

 
/findmean{
/x exch def
/sum 0 def
/i 0 def
x length 0 eq
{}
{
x length{
/sum sum x i get add def
/i i 1 add def
}repeat
/sum sum x length div def
}ifelse
sum ==
}def
 

[edit] PowerShell

The hard way by calculating a sum and dividing:

function mean ($x) {
if ($x.Count -eq 0) {
return 0
} else {
$sum = 0
foreach ($i in $x) {
$sum += $i
}
return $sum / $x.Count
}
}

or, shorter, by using the Measure-Object cmdlet which already knows how to compute an average:

function mean ($x) {
if ($x.Count -eq 0) {
return 0
} else {
return ($x | Measure-Object -Average).Average
}
}

[edit] PureBasic

Procedure.d mean(List number())
Protected sum=0
 
ForEach number()
sum + number()
Next
ProcedureReturn sum / ListSize(number())
; Depends on programm if zero check needed, returns nan on division by zero
EndProcedure

[edit] Python

Works with: Python version 3.0.
Works with: Python version 2.6
Uses fsum which tracks multiple partial sums to avoid losing precision

from math import fsum
def average(x):
return fsum(x)/float(len(x)) if x else 0
print (average([3,1,4,1,5,9]))
print (average([1e20,3,1,4,1,5,9,-1e20]))

Output:

3.83333333333333
2.875


Works with: Python version 2.5

def average(x):
return sum(x)/float(len(x)) if x else 0
print average([3,1,4,1,5,9])
print average([1e20,3,1,4,1,5,9,-1e20])

Output

(Notice how the second call gave the wrong result)

3.83333333333333
0.0


Works with: Python version 2.4

def avg(data):
if len(data)==0:
return 0
else:
return sum(data)/float(len(data))
print avg([3,1,4,1,5,9])

Output:

3.83333333333333

[edit] R

R has its mean function but it does not allow for NULL (void vectors or whatever) as argument: in this case it raises a warning and the result is NA. An implementation that does not suppress the warning could be:

omean <- function(v) {
m <- mean(v)
ifelse(is.na(m), 0, m)
}

[edit] REBOL

rebol [
Title: "Arithmetic Mean (Average)"
Author: oofoe
Date: 2009-12-11
URL: http://rosettacode.org/wiki/Average/Arithmetic_mean
]

 
average: func [v /local sum][
if empty? v [return 0]
 
sum: 0
forall v [sum: sum + v/1]
sum / length? v
]
 
; Note precision loss as spread increased.
 
print [mold x: [] "->" average x]
print [mold x: [3 1 4 1 5 9] "->" average x]
print [mold x: [1000 3 1 4 1 5 9 -1000] "->" average x]
print [mold x: [1e20 3 1 4 1 5 9 -1e20] "->" average x]

Output:

[] -> 0
[3 1 4 1 5 9] -> 3.83333333333333
[1000 3 1 4 1 5 9 -1000] -> 2.875
[1E+20 3 1 4 1 5 9 -1E+20] -> 0.0

[edit] Ruby

nums = [3, 1, 4, 1, 5, 9]
nums.empty? ? 0 : nums.inject(:+) / Float(nums.size)

[edit] Sather

Built to work with VEC, ("geometric" vectors), whose elements must be floats. A 0-dimension vector yields "nan".

class VECOPS is
mean(v:VEC):FLT is
m ::= 0.0;
loop m := m + v.aelt!; end;
return m / v.dim.flt;
end;
end;
 
class MAIN is
main is
v ::= #VEC(|1.0, 5.0, 7.0|);
#OUT + VECOPS::mean(v) + "\n";
end;
end;

[edit] Scala

Using Scala 2.7, this has to be defined for each numeric type:

def mean(s: Seq[Int]) = s.foldLeft(0)(_+_) / s.size

However, Scala 2.8 gives much more flexibility, but you still have to opt between integral types and fractional types. For example:

def mean[T](s: Seq[T])(implicit n: Integral[T]) = {
import n._
s.foldLeft(zero)(_+_) / fromInt(s.size)
}

This can be used with any subclass of Sequence on integral types, up to and including BigInt. One can also create singletons extending Integral for user-defined numeric classes. Likewise, Integral can be replaced by Fractional in the code to support fractional types, such as Float and Double.

Alas, Scala 2.8 also simplifies the task in another way:

def mean[T](s: Seq[T])(implicit n: Fractional[T]) = n.div(s.sum, n.fromInt(s.size))

Here we show a function that supports fractional types. Instead of importing the definitions from n, we are calling them on n itself. And because we did not import them, the implicit definitions that would allow us to use / were not imported as well. Finally, we use sum instead of foldLeft.

[edit] Scheme

(define (mean l)
(if (null? l)
0
(/ (apply + l) (length l))))
> (mean (list 3 1 4 1 5 9))
3 5/6

[edit] Seed7

$ include "seed7_05.s7i";
include "float.s7i";
 
const array float: numVector is [] (1.0, 2.0, 3.0, 4.0, 5.0);
 
const func float: mean (in array float: numbers) is func
result
var float: result is 0.0;
local
var float: total is 0.0;
var float: num is 0.0;
begin
if length(numbers) <> 0 then
for num range numbers do
total +:= num;
end for;
result := total / flt(length(numbers));
end if;
end func;
 
const proc: main is func
begin
writeln(mean(numVector));
end func;

[edit] Slate

[|:list| (list reduce: #+ `er ifEmpty: [0]) / (list isEmpty ifTrue: [1] ifFalse: [list size])] applyWith: #(3 1 4 1 5 9).
[|:list| (list reduce: #+ `er ifEmpty: [0]) / (list isEmpty ifTrue: [1] ifFalse: [list size])] applyWith: {}.

[edit] Smalltalk

| numbers |
numbers := #(1 2 3 4 5 6 7 8).
(numbers isEmpty ifTrue:[0]
ifFalse: [
(numbers inject: 0 into: [:sum :aNumber | sum + aNumber]) / numbers size
]
) displayNl.

[edit] SNOBOL4

Works with: Macro Spitbol Works with: Snobol4+ Works with: CSnobol

        define('avg(a)i,sum') :(avg_end)
avg i = i + 1; sum = sum + a<i> :s(avg)
avg = 1.0 * sum / prototype(a) :(return)
avg_end
 
* # Fill arrays
str = '1 2 3 4 5 6 7 8 9 10'; arr = array(10)
loop i = i + 1; str len(p) span('0123456789') . arr<i> @p :s(loop)
empty = array(1) ;* Null vector
 
* # Test and display
output = '[' str '] -> ' avg(arr)
output = '[ ] -> ' avg(empty)
end

Output:

[1 2 3 4 5 6 7 8 9 10] -> 5.5
[ ] -> 0.

[edit] Standard ML

These functions return a real:

fun mean_reals [] = 0.0
| mean_reals xs = foldl op+ 0.0 xs / real (length xs);
 
val mean_ints = mean_reals o (map real);

the previous code is easier to read and understand, though if you which the fastest implementation to use in production code notice several points: it is possible to save a call to length computing the length through the foldl, and for mean_ints it is possible to save calling real on every numbers, converting only the result of the addition. Also the task asks to return 0 on empty lists, but in Standard ML this case would rather be handled by an exception.

fun mean_reals [] = raise Empty
| mean_reals xs = let
val (total, length) =
foldl
(fn (x, (tot,len)) => (x + tot, len + 1.0))
(0.0, 0.0) xs
in
(total / length)
end;
 
 
fun mean_ints [] = raise Empty
| mean_ints xs = let
val (total, length) =
foldl
(fn (x, (tot,len)) => (x + tot, len + 1.0))
(0, 0.0) xs
in
(real total / length)
end;

[edit] Tcl

package require Tcl 8.5
proc mean args {
if {[set num [llength $args]] == 0} {return 0}
expr {[tcl::mathop::+ {*}$args] / double($num)}
}
mean 3 1 4 1 5 9 ;# ==> 3.8333333333333335


general purpose Tcl
proc mean args {
set num [llength $args]
if {$num == 0} {
set meanargs 0
} else {
set sum [expr [join $args +]]
set meanargs [expr $sum/$num]
}
}
 
=={{header|TI-89 BASIC}}==
 
<lang ti89b>Define rcmean(nums) = when(dim(nums) = 0, 0, mean(nums))

[edit] UnixPipes

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

[edit] Trith

: mean dup empty? [drop 0] [dup [+] foldl1 swap length /] branch ;
 
[3 1 4 1 5 9] mean

[edit] Ursala

There is a library function for means already, although it doesn't cope with empty vectors. A mean function could be defined as shown for this task.

#import nat
#import flo
 
mean = ~&?\0.! div^/plus:-0. float+ length
 
#cast %e
 
example = mean <5.,3.,-2.,6.,-4.>

output:

1.600000e+00


[edit] V

[mean
[sum 0 [+] fold].
dup sum
swap size [[1 <] [1]] when /
].

[edit] Vedit macro language

The numeric data is stored in current edit buffer as ASCII strings, one value per line.

#1 = 0			// Sum
#2 = 0 // Count
BOF
While(!At_EOF) {
#1 += Num_Eval(SIMPLE)
#2++
Line(1, ERRBREAK)
}
if (#2) { #1 /= #2 }
Num_Type(#1)
Personal tools
Support