Averages/Arithmetic mean: Difference between revisions

Content added Content deleted
m (→‎[[Mean#ALGOL 68]]: clarify what ELLA ALGOL 68 is missing.)
No edit summary
Line 79: Line 79:
This implementation uses a plain old static array of <tt>double</tt>s for the numeric vector.
This implementation uses a plain old static array of <tt>double</tt>s for the numeric vector.


<c>#include <stdio.h>
<lang c>#include <stdio.h>


double mean(double *p, unsigned qty)
double mean(double *p, unsigned qty)
Line 91: Line 91:
{double test[6] = {1.0, 2.0, 5.0, -5.0, 9.5, 3.14159};
{double test[6] = {1.0, 2.0, 5.0, -5.0, 9.5, 3.14159};
printf("%lg\n", mean(test, 6));
printf("%lg\n", mean(test, 6));
return 0;}</c>
return 0;}</lang>


=={{header|C++}}==
=={{header|C++}}==
Line 322: Line 322:
These functions return a float:
These functions return a float:


<ocaml>let mean_floats xs =
<lang ocaml>let mean_floats xs =
if xs = [] then
if xs = [] then
0.
0.
Line 328: Line 328:
List.fold_left (+.) 0. xs /. float_of_int (List.length xs)
List.fold_left (+.) 0. xs /. float_of_int (List.length xs)


let mean_ints xs = mean_floats (List.map float_of_int xs)</ocaml>
let mean_ints xs = mean_floats (List.map float_of_int xs)</lang>


the previous code is easier to read and understand, though if you which
the previous code is easier to read and understand, though if you which
Line 340: Line 340:
would rather be handled by an exception.
would rather be handled by an exception.


<ocaml>let mean_floats xs =
<lang ocaml>let mean_floats xs =
if xs = [] then
if xs = [] then
invalid_arg "empty list"
invalid_arg "empty list"
Line 363: Line 363:
in
in
(float total /. length)
(float total /. length)
;;</ocaml>
;;</lang>


=={{header|Perl}}==
=={{header|Perl}}==
<perl>sub avg {
<lang perl>sub avg {
@_ or return 0;
@_ or return 0;
my $sum = 0;
my $sum = 0;
Line 373: Line 373:
}
}
print avg(qw(3 1 4 1 5 9)), "\n";</perl>
print avg(qw(3 1 4 1 5 9)), "\n";</lang>
{{libheader|Data::Average}}
{{libheader|Data::Average}}
With module Data::Average.
With module Data::Average.
(For zero-length arrays, returns the empty list.)
(For zero-length arrays, returns the empty list.)
<perl>use Data::Average;
<lang perl>use Data::Average;


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


=={{header|PHP}}==
=={{header|PHP}}==
<php>$nums = array(3, 1, 4, 1, 5, 9);
<lang php>$nums = array(3, 1, 4, 1, 5, 9);
if ($nums)
if ($nums)
echo array_sum($nums) / count($nums), "\n";
echo array_sum($nums) / count($nums), "\n";
else
else
echo "0\n";</php>
echo "0\n";</lang>


=={{header|Pop11}}==
=={{header|Pop11}}==
Line 408: Line 408:
=={{header|Python}}==
=={{header|Python}}==
{{works with|Python|2.5}}
{{works with|Python|2.5}}
<python>def average(x):
<lang python>def average(x):
return sum(x)/float(len(x)) if x else 0
return sum(x)/float(len(x)) if x else 0
print average([3,1,4,1,5,9])</python>
print average([3,1,4,1,5,9])</lang>


Output:
Output:
<python>3.83333333333333</python>
<lang python>3.83333333333333</lang>




{{works with|Python|2.4}}
{{works with|Python|2.4}}
<python>def avg(data):
<lang python>def avg(data):
if len(data)==0:
if len(data)==0:
return 0
return 0
else:
else:
return sum(data)/float(len(data))
return sum(data)/float(len(data))
print avg([3,1,4,1,5,9])</python>
print avg([3,1,4,1,5,9])</lang>


Output:
Output:
<python>3.83333333333333</python>
<lang python>3.83333333333333</lang>


=={{header|Ruby}}==
=={{header|Ruby}}==
<ruby>nums = [3, 1, 4, 1, 5, 9]
<lang ruby>nums = [3, 1, 4, 1, 5, 9]
nums.empty? ? 0 : nums.inject(:+) / Float(nums.size)</ruby>
nums.empty? ? 0 : nums.inject(:+) / Float(nums.size)</lang>


=={{header|Scheme}}==
=={{header|Scheme}}==
<scheme>(define (mean l)
<lang scheme>(define (mean l)
(if (null? l)
(if (null? l)
0
0
(/ (apply + l) (length l))))</scheme>
(/ (apply + l) (length l))))</lang>


> (mean (list 3 1 4 1 5 9))
> (mean (list 3 1 4 1 5 9))