summaryrefslogtreecommitdiff
path: root/var
diff options
context:
space:
mode:
authorPatrick Simianer <p@simianer.de>2013-12-05 07:56:38 +0100
committerPatrick Simianer <p@simianer.de>2013-12-05 07:56:38 +0100
commitdb6a6ecfa350cae29739c59df1210d8f76a479c9 (patch)
treef137a001f57f170455c28ce97b5abb2726006cf6 /var
init
Diffstat (limited to 'var')
-rwxr-xr-xvar39
1 files changed, 39 insertions, 0 deletions
diff --git a/var b/var
new file mode 100755
index 0000000..08b75b6
--- /dev/null
+++ b/var
@@ -0,0 +1,39 @@
+#!/usr/bin/env ruby
+
+require 'trollop'
+
+
+def usage
+ STDERR.write "./stddev [-r <d>] < <one number per line>\n"
+ exit 1
+end
+usage if not [0,2].include? ARGV.size
+
+opts = Trollop::options do
+ opt :round, "Number of digits after decimal point.", :type => :int, :default => -1
+end
+
+
+sum = 0.0
+i = 0
+cached = []
+while line=STDIN.gets
+ v = line.strip.to_f
+ sum += v
+ cached << v
+ i +=1
+end
+
+avg = sum/i.to_f
+
+var = 0
+cached.each { |v|
+ var += (avg - v)**2
+}
+
+if opts[:round] >= 0
+ puts var.round opts[:round]
+else
+ puts var
+end
+