#Mathf.Max for absolute values?

71 messages · Page 1 of 1 (latest)

lone tapir
#

Hi! sort of a short question, but how can you do Mathf.Max that checks the absolute values?

i.e max(-5,3) returns -5, max(6,9) returns 9, max(-2, 1) returns -2

lavish lion
#

Use an if statement?

carmine stone
#

There is no built in method for this but you can trivially write your own

granite sage
#

so you want the max function to return the input with higher magnitude?
simply use, Mathf.Max(Mathf.Abs(input1),Mathf.Abs(input2))

carmine stone
#

As in returns the negative number if its absolute value is greater

granite sage
#

oh i see

carmine stone
#

f(-5, 3) should return -5, not 5

granite sage
#

we can easily achieve that with some if statements tho

carmine stone
#

indeed

granite sage
#

doubt
what do you want to do if max(-5,5)

lone tapir
#

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

carmine stone
#

That's horrible

lone tapir
#

yea

#

should rewrite it tbh

carmine stone
#

Just write your own Max function it'd be like

#

2 lines of code

#

Hell you could one line it

#

without LINQ

lone tapir
#

youve got to loop through, check values and set a variable to the iterated one if it's bigger than the variable

carmine stone
#

what

#

Why would you need a loop

lone tapir
#

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

carmine stone
#

You didn't mention an array in your question

lone tapir
#

i thought itd be a given

#

since max functions usually take arrays

carmine stone
#

No they don't lolwutvelma

lone tapir
#

so anyways

carmine stone
#

They take just two arguments

#

That's Unity being stupid

lone tapir
#

neat

carmine stone
#

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)

lone tapir
#

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;
}
carmine stone
#

That won't necessarily give you the correct answer

lone tapir
#

i imagine it would

carmine stone
#

It returns a different value based purely on argument order

#

Which is horrendously bad design var

lone tapir
#

i think thats just a floating point thing because its literally making both of them absolute

#

abs 5 is 5

#

abs -5 is 5

carmine stone
#

It's not a floating point thing

lone tapir
#

they're both equal, and its got to return one

carmine stone
#

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

lone tapir
#

the only way 5 and 5 arent equal would be floating point stuff

carmine stone
#

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

lone tapir
#
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

lavish lion
# carmine stone

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);
}
carmine stone
#

Checks out

mellow pike
#

😭

#

that is hurting my eyes

#

I guess that the other options is multiple lines