What is an absolute value?

7

Viewing the MySQL documentation you can find the mathematical function ABS (x) , which returns the absolute value of x .

mysql> SELECT ABS(2);
        -> 2
mysql> SELECT ABS(-32);
        -> 32

What exactly is this absolute value and how does it work?

    
asked by anonymous 05.09.2017 / 19:29

3 answers

11

I'll explain some of the mathematical concept of the ABS () , because I believe it's what you want to understand, and it's what's part of the site's scope.

ABS () - returns the absolute value of a given number

What is an absolute value this small explanation already gives an introduction:

  

The absolute value of any number refers to its magnitude and not to the signal it can have, whether positive or negative. In mathematical terms, the absolute value is an operation that allows any number to become positive. / p>

That's why when you use this ABS function, as you mentioned:

mysql> SELECT ABS(2);
            -> 2
mysql> SELECT ABS(-32);
            -> 32

It will return, respectively, the values 2 and 32 . Basically always returns the positive value, regardless of which number is X.

This function ABS() , is closely related to the module concept ( 1 , 2 ). Actually, because it's the same function but with another name.

Remember, a lot of time when you studied at school, and your teacher placed between "bars" the number to find the module " |x| :

|4|=4 or |-3|=3

Basically, it is this concept of having the nonnegative value of X, regardless of its signal, only applied to the database.

  

Therefore, | x | = x, if x is a positive number and | x | = -x, if x is a negative number ....

Some other examples of outputs using ABS() :

SELECT ABS(3);
      -> 3
SELECT ABS(-7);
      -> 7
SELECT ABS(0);
      -> 0
SELECT ABS(-1);
      -> 1
    
05.09.2017 / 19:31
7

This function is in all standard libraries of programming languages and other technologies that perform mathematical calculations.

It always returns the positive value of the number entered, that is, if it is a positive return itself, if it is the negative it informs the number without signal. It is useful when the signal is not important, but the absolute value.

In general this is a very cheap operation because it is just zeroing a bit of the number (invert the bits in the case of an integer). A naive implementation would check if the value is negative and multiply by -1 to eliminate the signal, but this is costly and unnecessary.

Documentation .

    
05.09.2017 / 19:32
7

In mathematics, absolute is the distance at which a number is from 0:

R = {...-3,-2,-1,0,+1,+2,+3...}
         |_______|________|
                 3

The -3 is "3 houses" from scratch, just like +3. So the absolute value of both is 3.

The absolute value always returns a positive number, this is because the negative sign, if any, is ignored.

In programming, when there is a possibility of a negative number coming but you just want to know its absolute value, you then use its function or method to do so.

    
05.09.2017 / 20:12