NaN and Inifinity

In article 86r83r5v8l.fsf@daemon.argelfraster.org,

[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!

> Does it make sense that

>   s = SimpleStats.new s.max => Infinity

Actually, it yields -Infinity. s.min would be Infinity.

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

> If you let @variance, @mean, @max and @min start out life as
> nil then

> def newObs(x) @max = [@max, x].max @min = [@min, x].min @n +=
> 1

>   unless @mean # if @mean.nil?  @mean, @variance = x, 0.0 else
> x -= @mean @mean += x / @n @variance = (@n - 2.0) / (@n - 1.0)
> * @variance + x * x / @n end end

> might be worth trying; Enumerable#max and Enumerable#min do
> the "right thing" with nil.

> Hope this helps

Thanks for the input!

–paul

···

Paul J. Sanchez paulNO@SPAMargelfraster.org wrote: