integer max, min?

Paolo Invernizzi arathorn at NO_SPAMfastwebnet.it
Tue Oct 2 08:40:16 PDT 2007


God! It's an overkill!

There's not in Phobos something like fmin fmax but for integers? Or must 
I always convert the result back from real?

Cheers, Paolo

downs wrote:
> BLS wrote:
>> Paolo Invernizzi schrieb:
>>> Silly question...
>>>
>>> Where are in Phobos the max and min for integers?
>>>
>>> Thanks, Paolo
>> Have a look at :
>>
>> http://www.digitalmars.com/d/property.html
>>
>> HTH Bjoern
> Or if you mean the other max/min ...
> 
> template supertype(T...) {
>   static assert (T.length);
>   static if (T.length==1)
>     alias T[0] supertype;
>   else static if (T.length==2)
>     alias typeof(T[0].init+T[1].init) supertype;
>   else
>     alias supertype!(
>       typeof(T[0].init+T[1].init),
>       T[2..$]
>     ) supertype;
> }
> 
> supertype!(T) max(T...)(T t) {
>   static assert(T.length);
>   supertype!(T) res=t[0];
>   foreach (v; t[1..$]) if (v>res) res=v;
>   return res;
> }
> 
> supertype!(T) min(T...)(T t) {
>   static assert(T.length);
>   supertype!(T) res=t[0];
>   foreach (v; t[1..$]) if (v<res) res=v;
>   return res;
> }
> 
> That should work. Have fun!
>  --downs


More information about the Digitalmars-d-learn mailing list