Thread overview
integer max, min?
Oct 02, 2007
Paolo Invernizzi
Oct 02, 2007
BLS
Oct 02, 2007
downs
Oct 02, 2007
Paolo Invernizzi
Oct 02, 2007
Nathan Reed
Oct 02, 2007
downs
Oct 02, 2007
Paolo Invernizzi
Oct 02, 2007
Bill Baxter
Oct 02, 2007
Paolo Invernizzi
October 02, 2007
Silly question...

Where are in Phobos the max and min for integers?

Thanks, Paolo
October 02, 2007
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
October 02, 2007
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
October 02, 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
October 02, 2007
"Paolo Invernizzi" <arathorn@NO_SPAMfastwebnet.it> wrote in message news:fdt65v$1306$1@digitalmars.com...
> Silly question...
>
> Where are in Phobos the max and min for integers?
>
> Thanks, Paolo

try x < y ? x : y for min(x, y)
and x > y ? x : y for max(x, y)

-Steve


October 02, 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
October 02, 2007
Nathan Reed wrote:
> (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.

Actually, it only works with numbers. Most of the complicated-looking
code just determines the type that can hold all possible results (that's
what the supertype stuff is for). That's also the reason it won't work
with strings and arrays - they don't define "+". Sorry. ^^
But yeah, it is a bit overkill. Gets handy when you want to min/max more
than two values of different types.
 --downs
October 02, 2007
Thanks Steven,
I know the ternary operator, and that's good for simple variable comparison, but if you use it in complex expressions, say also more than one times, sometimes it's not so readable as plain old min/max.

Paolo.

Steven Schveighoffer wrote:
> "Paolo Invernizzi" <arathorn@NO_SPAMfastwebnet.it> wrote in message news:fdt65v$1306$1@digitalmars.com...
>> Silly question...
>>
>> Where are in Phobos the max and min for integers?
>>
>> Thanks, Paolo
> 
> try x < y ? x : y for min(x, y)
> and x > y ? x : y for max(x, y)
> 
> -Steve 
> 
> 
October 02, 2007
Natan,
I'm talking about the (b) case, sorry for the confusion.
I know I can use a template, I'm just wondering if THAT template was in some Phobos module, as I cannot find it!
I'm already including std.math, so I was only wondering why...

Cheers, Paolo

Nathan Reed wrote:
> 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
October 02, 2007
Paolo Invernizzi wrote:
> Natan,
> I'm talking about the (b) case, sorry for the confusion.
> I know I can use a template, I'm just wondering if THAT template was in some Phobos module, as I cannot find it!
> I'm already including std.math, so I was only wondering why...
> 

Nope, crazy as it may seem it's not there.  There's also no standard swap() function like:

void swap(T)(ref T a, ref T b) {
    T tmp = a;
    a = b;
    b = tmp;
}

--bb