Hi,
I found Math.Min/Max, but they seems to be different. Is there an equivalent of them in C#
Thanks,
aw
Hi,
I found Math.Min/Max, but they seems to be different. Is there an equivalent of them in C#
Thanks,
aw
Try the static Math.Floor and Math.Ceiling methods.
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
Thanks Peter. But Math.Min/Max only works with 2 values
Thanks again,
aw
Right, so do standard C/C++ MIN and MAX.
Azurewrath wrote:
Thanks Peter. But Math.Min/Max only works with 2 values
Thanks again,
aw
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.
Azurewrath wrote:
Thanks guys. But in the above code in the link, he uses 3 arguments
min = MIN( r, g, b );
Thanks again,
aw
For Example of a method that does this:
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; } |
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
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)
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!
awUnfortunately, 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)
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