#!/usr/bin/env ruby require 'trollop' conf = Trollop::options do banner "stddev [-r ] < " 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