help explaining Mathf.Abs?

What is an absolute value? and what do we use it for and why?

Sorry but I am just confused.

An absolute value is the value without the sign. It basically just makes a negative number positive. I personally use it for quite a few things but mainly checking distances for an OnUseObject function I have.

Example:

int maxUseRange = 5;

int a = 4;
int b = 10;
int absDiff = Mathf.Abs(a - b);//a - b = -6, Mathf.Abs removes the sign resulting in 6
if (absDiff <= maxUseRange) useObject();//doesn't execute useObject()
6 Likes

Well thats a very shallow description, abs have MANY interesting properties not only for numbers… just read this:

It’s useful for a few things. The most obvious is distance checking. It’s also useful if you need to get the square root of a number. Or to do normalisation.

1 Like

Then wait for the mind f**k that is Quaternions.

12 Likes

I actually think vectors will kill him first.

4 Likes

2 Likes

Mathf.Approximately() may deliver a glancing blow too :wink:

Mathf.Approximately: so small delta that you might as well use ==

Just wait till he gets to floating point precision, and Mathf.Epsilon

Simple use case

float A = 5.5f;
float B = 2.4f;
float difference = Mathf.Abs(A - B); // is 3.1
// Or it's same as:
float difference = Mathf.Abs(B - A); // is 3.1 too
1 Like

Was this really a question?

And it really got multiple answers?

This is one of those times this image is required:

3 Likes

haha but can you really be bothered lifting up your shirt and showing us that when its easier just to answer? haha

cheers fellas. I found this useful as I was specifically looking to make negative vector values positive and then put them through an if statement to do something if a number is within a particular range. just shows you how little people know when they are noobs. now I know how to get rid of those horrible negative values without making my positive values negative which would defeat the purpose of trying to make my negative values positive.

Great, this is actually one of the top results if you google for “Unity Mathf.abs” and the thread is full of geniuses who go basically “Just google it dumbass” and “if you find THAT already confusing you should stop programming”.

Great help. Round of applause.

But I guess jimroberts explained it good enough, for newcommers to get a handle on this for now. Thanks.

Thanks for resurrecting this 3 year old thread, though. Round of applause.

1 Like

“just read this” … yeah, but no. in the time i finish reading AND understanding this, i could publish 2 games

If it’s going to take you that long to read and understand an article about “Absolute Value”… I doubt you’re going to complete and publish 2 games in that time. Considering that the complexity of creating a publish a game far surpasses that of “Absolute Value”.

1 Like

They really aren’t that complicated. Here’s a simplified function which returns an absolute value of an int without using Mathf.

public int GetAbsoluteValue(int initialValue)
{
    int returnValue;
    if (initialValue >= 0)
    {
        //initialValue is already not negative, so return itself
        returnValue = initialValue;
    }
    else
    {
        //initialValue is negative, so subtract 2 times itself from itself
        //  Subtracting 1x itself from itself brings a negative number up to 0
        //  Subtracting 2x itself then is the same thing as removing the negative sign
        //  Example: -10 - (-10 * 2) =
        //           -10 - (-20) =
        //            10
        returnValue = initialValue - (initialValue * 2);
    }

    return returnValue;
}

No need for all the arithmetic. Just negate (or multiply by -1).

public int GetAbsoluteValue(int value)
{
    return value >= 0 ? value : -value;
}

Which is technically all Math.Abs does… though it does a safety check for int.MinValue since technically negating it is an overflow.

Subtracting 2*value is also an overflow, and would occur at -(2^30) rather than at -(2^31).