integer max, min?

Nathan Reed nathaniel.reed at gmail.com
Tue Oct 2 09:29:03 PDT 2007


Paolo Invernizzi wrote:
> 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?
> 

(a) If you're talking about the maximum and minimum values an int can 
take on, they are named int.max and int.min.

(b) If you're talking about functions max(a,b) and min(a,b) that return 
the max and min of their arguments, see Downs' post, which is a bit 
overkill if you just want it on numeric types, but will (I think) do 
lexicographic comparison for string and array types.  If you just want 
to use it with numeric types (or types that have opCmp), this will do:

T min(T) (T a, T b)
{
     return (a < b) ? a : b;
}

T max(T) (T a, T b)
{
     return (a > b) ? a : b;
}

Thanks,
Nathan


More information about the Digitalmars-d-learn mailing list