handling T.min the right way
Bill Baxter
dnewsgroup at billbaxter.com
Tue Mar 20 21:47:25 PDT 2007
Frits van Bommel wrote:
> 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.
Yeh, I guess that's better. I've just gotten used to working with
languages where there's no portable way to get at nan and infinity
constants. But I guess D doesn't have that problem. Yay D!
--bb
More information about the Digitalmars-d
mailing list