title: Geometric mean in Ruby returning Infinity for large arrays
body: <p>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 <code>Infinity</code> 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?</p>
  
  <p>ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-darwin10.8.0]  </p>
  
  <p><strong>Example</strong></p>
  
  <pre><code>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
  
  #=&gt; 4672.4331716807965
  #=&gt; 1429766
  #=&gt; 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
  
  #=&gt; Infinity
  #=&gt; 1429767
  #=&gt; 84
  </code></pre>