June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -1,21 +1,20 @@
real
mean(list l)
{
integer i;
real sum, x;
sum = 0;
for (i, x in l) {
for (, x in l) {
sum += x;
}
return sum / ~l;
sum / ~l;
}
integer
main(void)
{
o_form("%f\n", mean(l_effect(4.5, 7.25, 5r, 5.75)));
o_form("%f\n", mean(list(4.5, 7.25, 5r, 5.75)));
return 0;
0;
}

View file

@ -1,3 +1,3 @@
mean([1, 2, 3])
mean([1:10])
mean(1..10)
mean([])

View file

@ -18,7 +18,7 @@ extension op
^ aSum / aCount.
]
}
program =
public program =
[
var anArray := (1, 2, 3, 4, 5, 6, 7, 8).
console printLine("Arithmetic mean of {",anArray,"} is ",anArray average); readChar.

View file

@ -0,0 +1,24 @@
implement Command;
include "sys.m";
sys: Sys;
include "draw.m";
include "sh.m";
init(nil: ref Draw->Context, nil: list of string)
{
sys = load Sys Sys->PATH;
a := array[] of {1.0, 2.0, 500.0, 257.0};
sys->print("mean of a: %f\n", getmean(a));
}
getmean(a: array of real): real
{
n: real = 0.0;
for (i := 0; i < len a; i++)
n += a[i];
return n / (real len a);
}

View file

@ -0,0 +1,14 @@
-- v can be (2D) point, (3D) vector or list of integers/floats
on mean (v)
case ilk(v) of
#point: cnt = 2
#vector: cnt = 3
#list: cnt = v.count
otherwise: return
end case
sum = 0
repeat with i = 1 to cnt
sum = sum + v[i]
end repeat
return float(sum)/cnt
end

View file

@ -0,0 +1,6 @@
put mean(point(1, 2.5))
-- 1.7500
put mean(vector(1.2, 4.7, 5.6))
-- 3.8333
put mean([6,12,18,24,30,36,42,48,54,60,66,72,78])
-- 42.0000

View file

@ -1,2 +1,4 @@
[1, 2, 2.718, 3, 3.142] avg println
[ ] avg println
: avg ( x -- avg )
x sum
x size dup ifZero: [ 2drop null ] else: [ >float / ]
;

View file

@ -0,0 +1,10 @@
>>> from statistics import mean
>>> mean([1e20,-1e-20,3,1,4,1,5,9,-1e20,1e-20])
2.3
>>> mean([10**10000, -10**10000, 3, 1, 4, 1, 5, 9, 0, 0])
2.3
>>> mean([10**10000, -10**10000, 3, 1, 4, 1, 5, 9, Fraction(1, 10**10000), Fraction(-1, 10**10000)])
Fraction(23, 10)
>>> big = 10**10000
>>> mean([Decimal(big), Decimal(-big), 3, 1, 4, 1, 5, 9, 1/Decimal(big), -1/Decimal(big)])
Decimal('2.3')

View file

@ -1,5 +1,5 @@
def mean(nums)
nums.inject(0.0, :+) / nums.size
nums.sum(0.0) / nums.size
end
nums = [3, 1, 4, 1, 5, 9]