#!/usr/bin/env ruby require 'trollop' cfg = 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 } stddev = Math.sqrt(var) if cfg[:round] >= 0 puts stddev.round cfg[:round] else puts stddev end