Over a million developers have joined DZone.

Standard Deviation

·

#!/usr/bin/ruby -w

include Math

# werte, für die die standardabweichung berechnet werden soll
arrValues = [ 4.58, 4.53, 4.1, 4.05 ]

# mittelwert der werte
fMedian = 0
arrValues.each do |fValue|
fMedian += fValue
end
fMedian /= arrValues.size.to_f
puts "Mittelwert = " + fMedian.to_s

# summieren
fStandardDeviation = 0
arrValues.each do |fValue|
fStandardDeviation += (fValue - fMedian)**2
end
puts "Zwischensumme = " + fStandardDeviation.to_s

# durch anzahl teilen
fStandardDeviation /= arrValues.size.to_f
puts fStandardDeviation

# wurzel ziehen
fStandardDeviation = Math.sqrt(fStandardDeviation)

# auf 3 stellen runden
fStandardDeviation = "%.3f" % fStandardDeviation

puts "rating = " + fStandardDeviation.to_s

Resources: blog.cadego.de
Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}