Title basically says it. I made this C# script where I need to use Mathf.Min and there is a scenario where some of the values I need to get the min of are the same. How is Mathf.Min going to choice which value it outputs?
Basically, in code.
int i = 10;
int j = 10;
does Mathf.Min (i , j) return consistently i, j, or a combination of both?
Technically, it doesn’t return either i or j. It returns the smallest value. In your case, 10. It’s not going to return a variable, it just returns the smallest value out of 2 or more values.
Of course, you could always just test it out!
Also, I apologize if I don’t quite understand what you are asking. If you simply want to know how it determines what the lowest value is, I am not sure on that.
I’m not sure that really makes sense (or at least you’re phrasing it a little oddly, imo). Like @Brathnann said, Mathf.Min returns the smallest value of two parameters you supply. It does not return a variable reference, it returns the value in that variable, however.
So some examples:
int i = 10;
int j = 10;
Debug.Log(Mathf.Min(i, j));
// Displays 10
i = 10;
j = 11;
Debug.Log(Mathf.Min(i, j));
// Displays 10
i = 11;
j = 10;
Debug.Log(Mathf.Min(i, j));
// Displays 10
Debug.Log(Mathf.Min(3, 1));
// Displays 1
Note, in these examples, the number is displayed because that is returned… it does not return “j” or “i”.
So, your right. It returns the smaller value. But in my specific circumstance, a lot of my code revolved around whatever number mathf.min returned. If it returned just 10 in my example above and did not pick between i or j, my code would have crashed. Basically, mathf.min, or something else, had to be choosing which 10 to output somehow.
I would post my code, but the code is for work and I don’t want to get in trouble. It doesn’t really matter anymore because I figured it out.
This is the code for Math.Min. Unity’s Mathf.Min most likely either looks just like this, or calls Math.Min and typecasts the result to float.
public static double Min(double val1, double val2)
{
if (val1 < val2)
{
return val1;
}
if (double.IsNaN(val1))
{
return val1;
}
return val2;
}
As everyone else has said, it doesn’t matter what variables you pass in or in what order. This function knows nothing about your variables, it only accepts values. It compares those values and returns the lower one (or NaN as a special case), or an arbitrary one if they are the same. The arbitrary one in this case is the second one, but it could just as easily return the first and the caller wouldn’t know or care.
hahaha, oops, good catch… obviously I know the difference, but I was on auto-pilot for a moment not thinking. I will update my post. Sorry to the OP if my post caused confusion because of that brain fart.
This whole thread completely ignored the important part of the question and instead got hung up on the fact that OP asked if it would “return i or j” when the OP meant “the value of i or the value of j.”
The question was what value will be returned if both values passed into an operator (like Min or Max) are equal.
The question was ignored because the question is nonsensical, the entire thread is discussing why it’s nonsensical.
The two values are equal. This is literally asking, “Which is greater, 4 or 4?” The function responds with “4”. There is no scenario where there could be a distinction between “which 4” it returned.
There might be a different function where the question might be meaningful. If you had a function like LongestWord(i, j), and you pass in “Sam” and “Max”, it would be meaningful to know which of them it returned, because they’re the same length but are different values. But with int and float, that simply isn’t the case. It’s fundamentally impossible for two floats to have different values without one being greater than the other.
But the question isn’t asking which of the two values are greater, it’s asking how the operator works/what will be returned. The entire premise of the question is that they are equal, there is no desire to find out which is larger or smaller.
Given that i and j are equal, this suggests(?) Mathf.Min(i, j) would return the value of j, as it uses < not <=. Entirely separate from the context that this is useless code.
If the values are the same, it will return the value. Obviously, internally, the code returns one of its inputs or the other, but it’s literally impossible for there to be a distinction when looking in from the outside of the function. There is no connection between the returned value and either of the parameters, and the only way to distinguish between them is to look at the source code.
I don’t know what else you want. The question was meaningless in 2017 and it’s meaningless now.
And honestly if you are doing anything that depends on this answer then I guarantee you that you are misunderstanding what you are trying to do.
Actually, if i = 4, and j = 4. That’s two variables stored in 2 different locations in memory. Math.Min(i,j) will return k, a third value, which just happens to be 4. i and j are both value types and Min returns a value type, and Min doesn’t pass by reference.
So even if the function literally did nothing but “return i”, it’ll still be a 3rd place in memory that will hold the return value when brought back into the calling scope, cause its value types you are messing with.
That’s why its non-sensical. Min doesn’t “return” either.
All that matters is what you assign the result to, if you ever assign it to a variable or argument at all.
More than that, even. There are the two variables in memory to start. Once they are in the method, there are two more local variables in memory. The return value then becomes a fifth variable in memory once it’s outside the method.