diasks2 - 3 months ago 7
Ruby Question

# Geometric mean in Ruby returning Infinity for large arrays

I am trying to calculate the geometric mean of an array in Ruby. However, after the array grows to a certain size it starts to return

`Infinity`
for the calculation of the geometric mean. Any idea of what is causing this to happen, or a better way to calculate the geometric mean of larger arrays in Ruby?

ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-darwin10.8.0]

Example

``````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
``````

Your value is overflowing what a `Float` can hold. Instead consider using `BigDecimal`:

``````require 'bigdecimal'

def gmean(x)
prod = BigDecimal.new 1
x.each { |v| prod *= BigDecimal.new(v) }
prod ** (1.0 / x.size)
end

gmean(sample_array2).to_f  #=> 4711.148446895203
``````

Note that your method can be simplified to a more functional style:

``````def gmean(xs)
one = BigDecimal.new 1
xs.map { |x| BigDecimal.new x }.inject(one, :*) ** (one / xs.size)
end
``````