blob: 6a29e74ccd83ec5d10e64dcfae19917dce99f8c2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
#!/usr/bin/env ruby
require 'trollop'
conf = Trollop::options do
banner "stddev [-r <d>] < <one number per line>"
opt :round, "Number of digits after decimal point.", :type => :int, :default => -1
opt :corrected, "corrected stddev", :type => :bool, :default => false
end
sum = 0.0
i = 0
cached = []
while line=STDIN.gets
v = line.to_f
sum += v
cached << v
i +=1
end
avg = sum/i.to_f
var = 0
cached.each { |v|
var += (avg - v)**2
}
stddev = 0
if conf[:corrected]
stddev = Math.sqrt(var/(i.to_f-1))
else
stddev = Math.sqrt(var/i.to_f)
end
if conf[:round] >= 0
puts stddev.round conf[:round]
else
puts stddev
end
|