From: Pierre Barbier de Reuille Date: 2005-09-13T18:51:33+09:00 Subject: Re: possible bug? Stefan Kaes a �crit : > Hi, > > I have a piece of code which I think should be working, but it doesn't. > > def standard_dev(a, mean) > Math.sqrt((a.inject(0.0){|v,r| r += (v-mean)*(v-mean) })/a.length) > end Well, try this : def standard_dev(a, mean) Math.sqrt((a.inject(0.0){|r,v| r += (v-mean)*(v-mean) })/a.length) end .... The doc says: enum.inject {| memo, obj | block } => obj You just swapped the two arguments ... Pierre > > computes results that don't make sense and differ from the values > computed by this code: > > def standard_dev(a, mean) > r = 0.0 > a.each{|v| r += (v-mean)*(v-mean) } > Math.sqrt(r/a.length) > end > > I think the two should be different. Am I missing something? > > -- stefan > > >