What is an absolute value?

Asked

Viewed 3,452 times

8

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

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

What exactly is this absolute value and how it works?

3 answers

12


Let me explain a little bit about the mathematical concept of function ABS(), because I believe that is what you want to understand, and is what is part of the scope of the site.


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

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

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

That’s why when using this ABS function ( ABSolute value, in English), as you quoted:

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

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


That function ABS(), is closely related to the concept of module (1,2). Actually, because it’s the same function only with another name.

Remember, a long time ago when you went to school, and your teacher put it between "bars" the number to "figure out the module" |x|:

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

Basically, it’s this concept of having the non-negative value of X, regardless of its signal, only applied in a 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

8

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

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

It’s usually a very cheap operation because it’s just zero a bit of the number. 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.

  • 1

    For a floating point number is to zero a bit. Integers are usually represented in complement of 2, the negation operation is slightly different.

  • 1

    Do you have any reference to that?

  • Complement de 2 na Wikipedia. With 4 bits, +3 is represented as 0011 and -3 as 1101

  • About x86: http://download.intel.com/products/processor/manual/325462.pdf this link opens a page with a built-in pdf, finally. Chapter 4.2.1.2, Signed Integers: All Operations on Signed integers assume a two’s Complement representation

  • In this case, invert and increment. + 6 -> 0110, -6 -> 1010; ~+6 => 1001; inc(~+6) => inc(1001) => 1010. In terms of depth of logical gates, it’s very little that changes.

8

In mathematics, absolute value 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, as well as the +3. Therefore, the absolute value of both is 3.

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.