Azurewrath

Hi,

I found Math.Min/Max, but they seems to be different. Is there an equivalent of them in C#

Thanks,

aw



Re: Visual C# Language What's the equivalent of MIN/MAX in C++(std), for C#

michhes

Try the static Math.Floor and Math.Ceiling methods.






Re: Visual C# Language What's the equivalent of MIN/MAX in C++(std), for C#

Azurewrath

Thanks man. I think it's different. Here is the code I found:

http://www.cs.rit.edu/~ncs/color/t_convert.html

MIN and MAX. I believe they return the smallest value, as in (4,7,1) returns 7 for MAX, and 1 for MIN.

Thanks,

aw





Re: Visual C# Language What's the equivalent of MIN/MAX in C++(std), for C#

Peter Ritchie

You can Math.Min and Math.Max




Re: Visual C# Language What's the equivalent of MIN/MAX in C++(std), for C#

Azurewrath

Thanks Peter. But Math.Min/Max only works with 2 values

Thanks again,

aw





Re: Visual C# Language What's the equivalent of MIN/MAX in C++(std), for C#

michhes

Might be a bit clunky but if everything's in an array, you could sort it and then pull out the lower/upper bound using Array.GetLowerBound/GetUpperBound.




Re: Visual C# Language What's the equivalent of MIN/MAX in C++(std), for C#

Peter Ritchie

Azurewrath wrote:

Thanks Peter. But Math.Min/Max only works with 2 values

Thanks again,

aw

Right, so do standard C/C++ MIN and MAX.




Re: Visual C# Language What's the equivalent of MIN/MAX in C++(std), for C#

Azurewrath

Thanks guys. But in the above code in the link, he uses 3 arguments

min = MIN( r, g, b );

Thanks again,

aw





Re: Visual C# Language What's the equivalent of MIN/MAX in C++(std), for C#

Peter Ritchie

Azurewrath wrote:

Thanks guys. But in the above code in the link, he uses 3 arguments

min = MIN( r, g, b );

Thanks again,

aw

That's a custom MIN define, if he doesn't provide source for it, you'll have to implement your own. The .NET framework does not have a Min or Max that takes three parameters.

For Example of a method that does this:

Code Snippet
public static Int32 Min(Int32 r, Int32 g, Int32 b)
{
return Math.Min(Math.Min(r, g), b);
}







Re: Visual C# Language What's the equivalent of MIN/MAX in C++(std), for C#

James Curran

First of all, there is not std::min function in C++ which does what you ask. There is a std::min, but it's functionally the same as Math.Min (two parameters). There is also an std::min_element, which is close to what you want, but with a different interface.

Anyway, this should do what you want:


int Min(int first, params int[] rest)
{
int min = first;
foreach(int next in rest)
{
min = Math.Min(min, next);
}
return min;
}






Re: Visual C# Language What's the equivalent of MIN/MAX in C++(std), for C#

Azurewrath

Thanks guys. Brilliant replies!

Thanks James for the code! I have a question about it though.

You use int and params, is it possible to get away with only using params Or are you trying to make sure that they caller provides at least 2 arguments

Thanks again guys!

aw





Re: Visual C# Language What's the equivalent of MIN/MAX in C++(std), for C#

James Curran

a) You only need to provide 1 argument (rest[] can be empty)

b) It's done that way to make the implementation easier.

c) It was actually done that way to make an different/eariler implementation possible. (In the original version, the method was recursive)






Re: Visual C# Language What's the equivalent of MIN/MAX in C++(std), for C#

Azurewrath

Thanks James for replying.

a) Do you think it's better to force the user to provide at least 2 values Or does it makse sense to use/allow it with 1 argument

c) Different/eariler implementation possible. Which one are these Also what's the original version

Lastly if it would be an array, would it be slower

Thanks alot again!

aw




Re: Visual C# Language What's the equivalent of MIN/MAX in C++(std), for C#

James Curran

Unfortunately, I didn't keep a copy of my first attempt at it, but it went something like:

if (rest.Length == 1)

return Math.Min(first, rest[0]);

else
return Math.Min(first, Min(rest[0], rest[1...end]);

There is, unfortunately, no simple way of expressing rest[1...end], so I had to create a new array and copy part of rest[] into it. Then I realize the foreach() would work better.

I'm not sure what you are asking in you second question. I think you want to know,

if the method was defined as :

Min(params int[] args) {....}

Which way would be faster:

int x = Min(1,2,3,4,5);

or

int[] nums = new int[]{1,2,3,4,5};

Min(nums);

The answer is that it's a complete wash, because the former is just a shorthand way of expressing the latter. The compile will hide some of it for you, but the two methods do exactly the same work. (Note, that given that method definition, both calling methods are allowed)






Re: Visual C# Language What's the equivalent of MIN/MAX in C++(std), for C#

Azurewrath

Thanks James. Very helpful. I was wondering how faster/slower it would be if you would allow support for arrays for the method, because I didn't know you could also pass an array to it.

My other question was, can I make the user to provide at least 2 arguments As in not have a method that accepts one argument

Your method is perfect!

Thanks alot again!

aw