handling T.min the right way
Frits van Bommel
fvbommel at REMwOVExCAPSs.nl
Tue Mar 20 18:24:18 PDT 2007
Daniel Keep wrote:
>
> Bill Baxter wrote:
>> Also when you're say trying to find the maximum of a set of numbers it
>> can be handy to initialize the 'current_max' to the smallest number
>> possible.
>>
>> float max_val = float.min; // want the new meaning here
>> int max_idx = -1;
>> foreach(i,x; bunch_o_floats) {
>> if (x<max_val) {
>> max_val=x;
>> max_idx=i;
>> }
>> }
>>
>> --bb
>
> I usually cheat and use nan, then change the comparison so that it will
> succeed if 'x' is any real number :P
>
> That way, if I give it an empty list, I get nan back instead of
> float.min, which could be misleading.
I'd use -float.infinity as the initial value. Empty quantifications
should return the identity value[1]. At least, that's what I was taught
in "Logic and Set Theory" (not sure if I translated that course name
right) as well as several other courses I can't remember the names of
right now.
You'd still need to make sure your comparison does the right thing for
NaNs, if that's important. (I'm pretty sure they didn't cover NaNs in
any course :) )
[1]: The identity value of an operator <op> is the value e such that
(for all x) e <op> x == x <op> e == x holds. (i.e. for '+' it's 0, for
'*' it's 1, and for 'max' it's -infinity)
More information about the Digitalmars-d
mailing list