我試圖獲得每個成員與平均值(例如,給定平均值= 4.5)的差異以及這兩個使用每個循環的結果的平方根。按照這個link的步驟,我做的是。該total = square root of (array[0] - average) + square root of (array[1] - average) + ...
平方根和Ruby上的標準偏差循環
array = [some values here]
average = 4.5 #as example to make the code shorter
squaredifference = 0
#Loop through the array, accumulate the total of the
#difference of num and average and of the square root of that result
array.each { |num| squaredifference += Math::sqrt(num - average) }
puts squaredifference
我的錯誤是
Math::DomainError: NumericalNumerical argument is out of domain - "sqrt"
from (irb):5:in `sqrt'
from (irb):5:in `block in irb_binding'
from (irb):5:in `each'
from (irb):5
from /Users/username/.rvm/rubies/ruby-1.9.3-p551/bin/irb:12:in `<main>'
任何幫助將是巨大的。謝謝。