Anonymous user
Averages/Pythagorean means: Difference between revisions
add Ruby
(added perl) |
(add Ruby) |
||
Line 294:
<pre>5.5 4.52872868812 3.41417152147</pre>
=={{header|Ruby}}==
{{works with|Ruby|1.9+}}
<lang ruby>class Array
def arithmetic_mean
inject(:+).to_f / length
end
def geometric_mean
inject(:*) ** (1.0 / length)
end
def harmonic_mean
length.to_f / inject(0) {|s, m| s += 1.0/m}
end
end
class Range
def method_missing(m, *args)
case m
when /_mean$/ then to_a.send(m)
else super
end
end
end
p a = (1..10).arithmetic_mean
p g = (1..10).geometric_mean
p h = (1..10).harmonic_mean
# is h < g < a ??
p g.between?(h, a)</lang>
outputs
<pre>5.5
4.52872868811677
3.41417152147406
true</pre>
=={{header|Tcl}}==
|