Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

help explaining Mathf.Abs?

Discussion in 'Scripting' started by Harryliu, Jun 9, 2016.

Thread Status:
Not open for further replies.
  1. Harryliu

    Harryliu

    Joined:
    Apr 17, 2014
    Posts:
    3
    What is an absolute value? and what do we use it for and why?

    Sorry but I am just confused.
     
  2. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    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:
    Code (CSharp):
    1. int maxUseRange = 5;
    2.  
    3. int a = 4;
    4. int b = 10;
    5. int absDiff = Mathf.Abs(a - b);//a - b = -6, Mathf.Abs removes the sign resulting in 6
    6. if (absDiff <= maxUseRange) useObject();//doesn't execute useObject()
     
    Last edited: Jun 9, 2016
  3. vikankraft

    vikankraft

    Joined:
    Feb 25, 2016
    Posts:
    88
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
    Thorlar likes this.
  5. shaderop

    shaderop

    Joined:
    Nov 24, 2010
    Posts:
    942
    Then wait for the mind f**k that is Quaternions.
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I actually think vectors will kill him first.
     
  7. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
     
    Thorlar and shaderop like this.
  8. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,043
    Mathf.Approximately() may deliver a glancing blow too ;)
     
  9. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,342
    Mathf.Approximately: so small delta that you might as well use ==
     
  10. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    Just wait till he gets to floating point precision, and Mathf.Epsilon
     
  11. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    Simple use case
    Code (CSharp):
    1. float A = 5.5f;
    2. float B = 2.4f;
    3. float difference = Mathf.Abs(A - B); // is 3.1
    4. // Or it's same as:
    5. float difference = Mathf.Abs(B - A); // is 3.1 too
     
    Thorlar likes this.
  12. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,560
    Was this really a question?

    And it really got multiple answers?

    This is one of those times this image is required:
     
  13. SPEAKERBUG

    SPEAKERBUG

    Joined:
    Aug 2, 2017
    Posts:
    1
    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.
     
  14. MassimoFrancesco

    MassimoFrancesco

    Joined:
    Jul 6, 2019
    Posts:
    44
    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.
     
  15. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    Thanks for resurrecting this 3 year old thread, though. Round of applause.
     
    matkoniecz likes this.
  16. Necrohunter

    Necrohunter

    Joined:
    May 18, 2017
    Posts:
    9
    "just read this" ... yeah, but no. in the time i finish reading AND understanding this, i could publish 2 games
     
  17. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,560
    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".
     
    Joe-Censored likes this.
  18. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    They really aren't that complicated. Here's a simplified function which returns an absolute value of an int without using Mathf.
    Code (csharp):
    1. public int GetAbsoluteValue(int initialValue)
    2. {
    3.     int returnValue;
    4.     if (initialValue >= 0)
    5.     {
    6.         //initialValue is already not negative, so return itself
    7.         returnValue = initialValue;
    8.     }
    9.     else
    10.     {
    11.         //initialValue is negative, so subtract 2 times itself from itself
    12.         //  Subtracting 1x itself from itself brings a negative number up to 0
    13.         //  Subtracting 2x itself then is the same thing as removing the negative sign
    14.         //  Example: -10 - (-10 * 2) =
    15.         //           -10 - (-20) =
    16.         //            10
    17.         returnValue = initialValue - (initialValue * 2);
    18.     }
    19.  
    20.     return returnValue;
    21. }
     
  19. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,560
    No need for all the arithmetic. Just negate (or multiply by -1).
    Code (csharp):
    1. public int GetAbsoluteValue(int value)
    2. {
    3.     return value >= 0 ? value : -value;
    4. }
    Which is technically all Math.Abs does... though it does a safety check for int.MinValue since technically negating it is an overflow.
    https://github.com/microsoft/referencesource/blob/master/mscorlib/system/math.cs#L278

    Subtracting 2*value is also an overflow, and would occur at -(2^30) rather than at -(2^31).
     
    Last edited: Apr 28, 2020
Thread Status:
Not open for further replies.