我正在尝试计算 Ruby 中数组的几何平均值。然而,在数组增长到一定大小后,它开始返回Infinity
计算几何平均值。知道是什么导致了这种情况发生,或者是计算 Ruby 中较大数组的几何平均值的更好方法吗?
ruby 1.9.3p125(2012-02-16 修订版 34643)[x86_64-darwin10.8.0]
例子
def gmean(x)
prod=1.0
x.each {|v| prod *= v}
prod**(1.0/x.size)
end
sample_array = [1, 1, 1, 1200, 1483, 1827, 2114, 2163, 2231, 2313, 2368, 2636, 2736, 2834, 2847, 2985, 3225, 3304, 3317, 3439, 3519, 3586, 3607, 3611, 3722, 3770, 4346, 4383, 4392, 4548, 4639, 4773, 4836, 4929, 4991, 4998, 5075, 5078, 5433, 5549, 5727, 5908, 5911, 5989, 6007, 6031, 6065, 6097, 6141, 6654, 6915, 6969, 6972, 7074, 7257, 7260, 7342, 7526, 7550, 7898, 8032, 8037, 8265, 8567, 8888, 9033, 9169, 9412, 9701, 9962, 10247, 11209, 14069, 14741, 15088, 15511, 18775, 19755, 19937, 24064, 32437, 41372, 59057, 778335]
puts gmean(sample_array)
puts sample_array.inject{|sum,x| sum + x }
puts sample_array.length
#=> 4672.4331716807965
#=> 1429766
#=> 84
sample_array2 = [1, 1, 2, 1200, 1483, 1827, 2114, 2163, 2231, 2313, 2368, 2636, 2736, 2834, 2847, 2985, 3225, 3304, 3317, 3439, 3519, 3586, 3607, 3611, 3722, 3770, 4346, 4383, 4392, 4548, 4639, 4773, 4836, 4929, 4991, 4998, 5075, 5078, 5433, 5549, 5727, 5908, 5911, 5989, 6007, 6031, 6065, 6097, 6141, 6654, 6915, 6969, 6972, 7074, 7257, 7260, 7342, 7526, 7550, 7898, 8032, 8037, 8265, 8567, 8888, 9033, 9169, 9412, 9701, 9962, 10247, 11209, 14069, 14741, 15088, 15511, 18775, 19755, 19937, 24064, 32437, 41372, 59057, 778335]
puts gmean(sample_array2)
puts sample_array2.inject{|sum,x| sum + x }
puts sample_array2.length
#=> Infinity
#=> 1429767
#=> 84