From: paulNO@... (Paul J. Sanchez) Date: 2003-08-20T05:45:15+09:00 Subject: Re: NaN and Infinity >>>>> "MS" == Mike Stok writes: MS> In article <86r83r5v8l.fsf@daemon.argelfraster.org>, MS> Paul J. Sanchez wrote: >> >> [Code Snipped] >> >> Note that by initializing min and max to +/- Infinity, >> respectively, the update stage for them doesn't need a special >> case for the first time. If only the same were true of the >> mean and variance updates... >> >> If anyone has any suggestions for tightening this up, by the >> way, they'd be appreciated! MS> Does it make sense that MS> s = SimpleStats.new s.max => Infinity Actually, it yields -Infinity. s.min would be Infinity. MS> nil seems more reasonable for a "no value" result. Since infinity is a bound, not a value, it can be interpreted as saying "the smallest thing I've seen so far is unbounded above, the biggest thing is unbounded below." To tell you the truth, though, it's a translation from Java of an assignment I give the intro programming students, and I switch the initialization between nil and infinity in alternate quarters. It makes a great filter to catch those who are copying solutions from the previous class. MS> If you let @variance, @mean, @max and @min start out life as MS> nil then MS> def newObs(x) @max = [@max, x].max @min = [@min, x].min @n += MS> 1 MS> unless @mean # if @mean.nil? @mean, @variance = x, 0.0 else MS> x -= @mean @mean += x / @n @variance = (@n - 2.0) / (@n - 1.0) MS> * @variance + x * x / @n end end MS> might be worth trying; Enumerable#max and Enumerable#min do MS> the "right thing" with nil. MS> Hope this helps Thanks for the input! --paul