Absolute Value of a Number


In this artiĀ­cle we are going to see the absolute valĀ­ue funcĀ­tion of a numĀ­ber, what are its propĀ­erĀ­ties, some graphĀ­ics and as an extra we are going to see how to calĀ­cuĀ­late the absolute valĀ­ue of an expresĀ­sion in the UniĀ­ty graphĀ­ic engine, in the field of proĀ­gramĀ­ming and game development.


Definition of the Absolute Value of a Number

The absolute valĀ­ue of a numĀ­ber is the disĀ­tance between that numĀ­ber and the 0 of the meaĀ­sureĀ­ment system. 

This results in two posĀ­siĀ­ble outĀ­comes dependĀ­ing on the numĀ­ber in question.

If the numĀ­ber is posĀ­iĀ­tive, the result of takĀ­ing the absolute valĀ­ue is the same numĀ­ber. If on the othĀ­er hand the numĀ­ber is negĀ­aĀ­tive, when takĀ­ing the absolute valĀ­ue the result is the same numĀ­ber but in posĀ­iĀ­tive value.


    \[|a| = \left\{\begin{array}{rl}  a & \text{si } a \geq 0\\   -a & \text{si } a > 0 \end{array} \right.\]

Because of this defĀ­iĀ­nĀ­iĀ­tion we can affirm that the absolute valĀ­ue of a numĀ­ber is always a posĀ­iĀ­tive number.

Graph of the Absolute Value of X

Below are some graphs of the absolute valĀ­ue funcĀ­tion to underĀ­stand how it behaves.

Observe in each graph the ranges of the variĀ­ables in the axes to see how the graph is affectĀ­ed by the disĀ­placeĀ­ments in X in Y and by the coefĀ­fiĀ­cients that mulĀ­tiĀ­ply the expression.

(1)   \begin{equation*}    y = |x|\end{equation*}

Rendered by QuickLaTeX.com

(2)   \begin{equation*}    y = -2|x|\end{equation*}

Rendered by QuickLaTeX.com

(3)   \begin{equation*}    y = |x - 2|\end{equation*}

Rendered by QuickLaTeX.com

(4)   \begin{equation*}    y = -|x| + 3\end{equation*}

Rendered by QuickLaTeX.com


When we are workĀ­ing with difĀ­ferĀ­ent points in a coorĀ­diĀ­nate sysĀ­tem, we are often interĀ­estĀ­ed in workĀ­ing with the disĀ­tances between them, which are posĀ­iĀ­tive values. 

The absolute valĀ­ue allows workĀ­ing with these disĀ­tances in mathĀ­eĀ­matĀ­iĀ­cal expressions.

How to calculate the Absolute Value in Unity

To calĀ­cuĀ­late the absolute valĀ­ue of an expresĀ­sion in UniĀ­ty, we must make use of the staĀ­tĀ­ic method "Abs" of the class "Mathf".

Let "num" be a numerĀ­iĀ­cal variĀ­able defined in the proĀ­gram in quesĀ­tion, to take its absolute valĀ­ue we do:


The result of the exeĀ­cuĀ­tion of this method can be stored in anothĀ­er variĀ­able or in the same one, in the folĀ­lowĀ­ing way:

num = Mathf.Abs(num);

Scroll to Top
Secured By miniOrange