#!/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 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 } var /= i.to_f if conf[:round] >= 0 puts var.round conf[:round] else puts var end