#Mathf.Max for absolute values?
71 messages · Page 1 of 1 (latest)
Use an if statement?
There is no built in method for this but you can trivially write your own
so you want the max function to return the input with higher magnitude?
simply use, Mathf.Max(Mathf.Abs(input1),Mathf.Abs(input2))
That's not what they asked. They want it to return the original number
As in returns the negative number if its absolute value is greater
oh i see
f(-5, 3) should return -5, not 5
we can easily achieve that with some if statements tho
indeed
doubt
what do you want to do if max(-5,5)
yea i just used linq
i was trying to think of if statements like
but id have to convert them all to abs then mathf.max and somehow find the original one in the array
Just write your own Max function it'd be like
2 lines of code
Hell you could one line it
without LINQ
how the hell would you do this in one line
youve got to loop through, check values and set a variable to the iterated one if it's bigger than the variable
well we wanna get the biggest number in an array right?
thats how you figure out the biggest one, you need to check each one
You didn't mention an array in your question
No they don't 
so anyways
But I will just give you a hint
first, are you working exclusively with floats? or do you need a solution that works for ints too
(or any other numeric type)
nvm just did it in one line
public static float SignedMax(float val1, float val2)
{
return Mathf.Abs(val1) > Mathf.Abs(val2) ? val1 : val2;
}
public static float SignedMin(float val1, float val2)
{
return Mathf.Abs(val1) < Mathf.Abs(val2) ? val1 : val2;
}
That won't necessarily give you the correct answer
i imagine it would
It returns a different value based purely on argument order
Which is horrendously bad design 
i think thats just a floating point thing because its literally making both of them absolute
abs 5 is 5
abs -5 is 5
It's not a floating point thing
they're both equal, and its got to return one
The hint: .NET already has this feature. Argument order doesn't matter
if one value is negative and the other is positive, but their abs values are the same, it returns the positive
because the positive is still the max
the only way 5 and 5 arent equal would be floating point stuff
oh neat
This API isn't available in Unity because Unity is stupid and outdated and old
But .NET is open source so you can just
Yoink the code for it
Go nuts
public static float MaxMagnitude(float val1, float val2)
{
float abs1 = Mathf.Abs(val1);
float abs2 = Mathf.Abs(val2);
//1 more than 2, or 1 is so big its not even a number, return 1
if ((abs1 > abs2) || float.IsNaN(abs1))
{
return val1;
}
//If both equal, return the regular biggest one
if (abs1 == abs2)
{
return Mathf.Max(val1, val2);
}
//Otherwise idfk dude we already compared greater and equal cases just fucking return something
//I love using vulgar language in code comments it reminds me of valve employees losing their mind writing TF2 spaghetti
return val2;
}
teehee
So what I'm hearing is that you need to do
public static float SignedMax(float val1, float val2)
{
return Mathf.Abs(val1) == Mathf.Abs(val2) ? Mathf.Max(val1, val2) : ((Mathf.Abs(val1) > Mathf.Abs(val2) || float.IsNan(Mathf.Abs(val1)) ? val1 : val2);
}
Checks out
EW
bro
😭
that is hurting my eyes
I guess that the other options is multiple lines