From db6a6ecfa350cae29739c59df1210d8f76a479c9 Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Thu, 5 Dec 2013 07:56:38 +0100 Subject: init --- stddev | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100755 stddev (limited to 'stddev') diff --git a/stddev b/stddev new file mode 100755 index 0000000..3bf0270 --- /dev/null +++ b/stddev @@ -0,0 +1,41 @@ +#!/usr/bin/env ruby + +require 'trollop' + + +def usage + STDERR.write "./stddev [-r ] < \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 +} + +stddev = Math.sqrt(var) + +if opts[:round] >= 0 + puts stddev.round opts[:round] +else + puts stddev +end + -- cgit v1.2.3