Absolute value

In mathematics, the absolute value (or modulus which is Latin for a small measure) of a real number is its numerical value without regard to its sign. So, for example, 3 is the absolute value of both 3 and −3. In computer programming, the mathematical function used to perform this calculation is usually given the name abs.

Generalizations of the absolute value for real numbers occur in a wide variety of mathematical settings. For example an absolute value is also defined for the complex numbers, the quaternions, ordered rings, fields and vector spaces. The absolute value is closely related to the notions of magnitude, distance, and norm in various mathematical and physical contexts.



Real numbers
For any real number a the absolute value or modulus of a is denoted by |&thinsp;a&thinsp;| and is defined as


 * $$|a| = \begin{cases} a, & \mbox{if } a \ge 0  \\ -a,  & \mbox{if } a < 0. \end{cases} $$

As can be seen from the above definition, the absolute value of a is always either positive or zero, but never negative.

From a geometric point of view, the absolute value of a real number is the distance along the real number line of that number from zero, and more generally the absolute value of the difference of two real numbers is the distance between them. Indeed the notion of an abstract distance function in mathematics can be seen to be a generalization of  the absolute value of the difference (see "Distance" below).

The following proposition, gives an identity which is sometimes used as an alternative (and equivalent) definition of the absolute value:

Proposition 1:
 * $$|a| = \sqrt{a^2}$$

The absolute value has the following four fundamental properties:

Proposition 2:


 * style="width: 250px" |$$|a| \ge 0 $$
 * Non-negativity
 * $$|a| = 0 \iff a = 0 $$
 * Positive-definiteness
 * $$|ab| = |a||b|\,$$
 * Multiplicativeness
 * $$|a+b| \le |a| + |b|  $$
 * Subadditivity
 * }
 * $$|a+b| \le |a| + |b|  $$
 * Subadditivity
 * }
 * }

Other important properties of the absolute value include:

Proposition 3:


 * style="width:250px" |$$|-a| = |a|\,$$
 * Symmetry
 * $$|a - b| = 0 \iff a = b $$
 * Identity of indiscernibles (equivalent to positive-definiteness)
 * $$|a - b| \le |a - c| +|c - b|  $$
 * Triangle inequality (equivalent to subadditivity)
 * $$|a/b| = |a| / |b| \mbox{ (if } b \ne 0) \,$$
 * Preservation of division (equivalent to multiplicativeness)
 * $$|a-b| \ge ||a| - |b|| $$
 * (equivalent to subadditivity)
 * }
 * Preservation of division (equivalent to multiplicativeness)
 * $$|a-b| \ge ||a| - |b|| $$
 * (equivalent to subadditivity)
 * }
 * }

Two other useful inequalities are:
 * $$|a| \le b \iff -b \le a \le b $$
 * $$|a| \ge b \iff a \le -b \mbox{ or } b \le a $$

The above are often used in solving inequalities; for example:




 * $$|x-3| \le 9 $$
 * $$\iff -9 \le x-3 \le 9 $$
 * $$\iff -6 \le x \le 12 $$
 * }
 * $$\iff -6 \le x \le 12 $$
 * }
 * }

Complex numbers


Since the complex numbers are not ordered, the definition given above for the real absolute value cannot be directly generalized for a complex number. However the identity given in Proposition 1:
 * $$|a| = \sqrt{a^2}$$

can be seen as motivating the following definition.

For any complex number


 * $$z = x + iy,\,$$

where x and y are real numbers, the absolute value or modulus of z is denoted |&thinsp;z&thinsp;| and is defined as


 * $$|z| = \sqrt{x^2 + y^2}.$$

It follows that the absolute value of a real number x is equal to its absolute value considered as a complex number since:


 * $$ |x + i0| = \sqrt{x^2 + 0^2} = \sqrt{x^2} = |x|.$$

Similar to the geometric interpretation of the absolute value for real numbers, it follows from the Pythagorean theorem that the absolute value of a complex number is the distance in the complex plane of that complex number from the origin, and more generally, that the absolute value of the difference of two complex numbers is equal to the distance between those two complex numbers.

The complex absolute value shares all the properties of the real absolute value given in Propositions 2 and 3 above. In addition, If


 * $$ z = x + i y = r (\cos \phi + i \sin \phi ) \,$$

and


 * $$\overline{z} = x - iy$$

is the complex conjugate of z, then it is easily seen that


 * $$\begin{align} |z| & = r, \\ |z| & = |\overline{z}|\end{align}$$

and


 * $$|z| = \sqrt{z\overline{z}},$$

with the last formula being the complex analogue of Proposition 1 mentioned above in the real case.

Since the positive reals form a subgroup of the complex numbers under multiplication, we may think of absolute value as an endomorphism of the multiplicative group of the complex numbers.

Absolute value functions
The real absolute value function is continuous everywhere. It is differentiable everywhere except for x = 0. It is monotonically decreasing on the interval (&minus;∞, 0] and monotonically increasing on the interval [0, ∞). Since a real number and its negative have the same absolute value, it is an even function, and is hence not invertible.

The complex absolute value function is continuous everywhere but (complex) differentiable nowhere (One way to see this is to show that it does not obey the Cauchy-Riemann equations).

Both the real and complex functions are idempotent.

It is a nonlinear function.

Ordered rings
The definition of absolute value given for real numbers above can easily be extended to any ordered ring. That is, if a is an element of an ordered ring R, then the absolute value of a, denoted by |&thinsp;a&thinsp;|, is defined to be:


 * $$|a| = \begin{cases} a, & \mbox{if } a \ge 0  \\ -a,  & \mbox{if } a < 0, \end{cases} $$

where &minus;a is the additive inverse of a, and 0 is the additive identity element.

Distance
The absolute value is closely related to the idea of distance. As noted above, the absolute value of a real or complex number is the distance from that number to the origin, along the real number line, for real numbers, or in the complex plane, for complex numbers, and more generally, the absolute value of the difference of two real or complex numbers is the distance between them.

The standard Euclidean distance between two points


 * $$a = (a_1, a_2, \cdots, a_n) $$

and


 * $$b = (b_1, b_2, \cdots, b_n) $$

in Euclidean n-space is defined as:
 * $$\sqrt{(a_1-b_1)^2 + (a_2-b_2)^2 + \cdots + (a_n-b_n)^2}. $$

This can be seen to be a generalization of |&thinsp;a &minus; b&thinsp;|, since if a and b are real, then by Proposition 1,
 * $$|a - b| = \sqrt{(a - b)^2}$$

while if


 * $$ a = a_1 + i a_2 \,$$

and


 * $$ b = b_1 + i b_2 \,$$

are complex numbers, then




 * $$|a - b| \,$$
 * $$ = |(a_1 + i a_2) - (b_1 + i b_2)|\,$$
 * $$ = |(a_1 - b_1) + i(a_2 - b_2)|\,$$
 * $$ = \sqrt{(a_1 - b_1)^2 + (a_2 - b_2)^2}$$
 * }
 * $$ = |(a_1 - b_1) + i(a_2 - b_2)|\,$$
 * $$ = \sqrt{(a_1 - b_1)^2 + (a_2 - b_2)^2}$$
 * }
 * $$ = \sqrt{(a_1 - b_1)^2 + (a_2 - b_2)^2}$$
 * }

The above shows that the "absolute value" distance for the real numbers or the complex numbers, agrees with the standard Euclidean distance they inherit as a result of considering them as the one and two-dimensional Euclidean spaces respectively.

The properties of the absolute value of the difference of two real or complex numbers: non-negativity, identity of indiscernibles, symmetry and the triangle inequality given in Propositions 2 and 3 above, can be seen to motivate the more general notion of a distance function as follows:

A real valued function d on a set X &times; X is called a distance function (or a metric) on X, if it satisfies the following four axioms:




 * style="width:250px" | $$d(a, b) \ge 0 $$
 * Non-negativity
 * $$d(a, b) = 0 \iff a = b $$
 * Identity of indiscernibles
 * $$d(a, b) = d(b, a) \,$$
 * Symmetry
 * $$d(a, b) \le d(a, c) + d(c, b)  $$
 * Triangle inequality
 * }
 * $$d(a, b) \le d(a, c) + d(c, b)  $$
 * Triangle inequality
 * }
 * }

Derivatives
The derivative of the real absolute value function is the signum function, sgn(x), which is defined as


 * $$\sgn (x) = \frac{x}{|x|},$$

for x ≠ 0. The absolute value function is not differentiable at x = 0. Where the absolute value function of a real number returns a value without respect to its sign, the signum function returns a number's sign without respect to its value. Therefore x = sgn(x)abs(x). The signum function is a form of the Heaviside step function used in signal processing, defined as:


 * $$ u(x) =

\begin{cases} 0,          & x < 0 \\ \frac{1}{2}, & x = 0 \\ 1,          & x > 0, \end{cases} $$

where the value of the Heaviside function at zero is conventional. So for all nonzero points on the real number line,


 * $$u(x) = \frac{\sgn(x) +1}{2}.\,$$

The absolute value function has no concavity at any point, the sign function is constant at all points. Therefore the second derivative of |x| with respect to x is zero everywhere except zero, where it is undefined.

The absolute value function is also integrable. Its antiderivative is


 * $$\int|x|dx=\frac{x|x|}{2}+C.$$

Fields
The fundamental properties of the absolute value for real numbers given in Proposition 2 above, can be used to generalize the notion of absolute value to an arbitrary field, as follows.

A real-valued function v on a field F is called an absolute value (also a modulus, magnitude, value, or valuation) if it satisfies the following four axioms:


 * {| cellpadding=10


 * $$v(a) \ge 0 $$
 * Non-negativity
 * $$v(a) = 0 \iff a = \mathbf{0} $$
 * Positive-definiteness
 * $$v(ab) = v(a) v(b) \,$$
 * Multiplicativeness
 * $$v(a+b) \le v(a) + v(b)  $$
 * Subadditivity or the triangle inequality
 * }
 * $$v(a+b) \le v(a) + v(b)  $$
 * Subadditivity or the triangle inequality
 * }
 * }

Where 0 denotes the additive identity element of F. It follows from positive-definiteness and multiplicativeness that v(1) = 1, where 1 denotes the multiplicative identity element of F. The real and complex absolute values defined above are examples of absolute values for an arbitrary field.

If v is an absolute value on F, then the function d on F &times; F, defined by d(a, b) = v(a &minus; b), is a metric and the following are equivalent:


 * d satisfies the ultrametric inequality d(x, y) &lt; max{d(x, z), d(y, z)}.


 * $$ \big\{ v\Big({\textstyle \sum_{k=1}^n } \mathbf{1}\Big) : n \in \mathbb{N} \big\} $$ is bounded in R.


 * $$ v\Big({\textstyle \sum_{k=1}^n } \mathbf{1}\Big) \le 1 \text{ for every } n \in \mathbb{N}.$$


 * $$ v(a) \le 1 \Rightarrow v(1+a) \le 1 \text{ for all } a \in F.$$


 * $$ v(a + b) \le \mathrm{max}\{v(a), v(b)\} \text{ for all } a, b \in F.$$

An absolute value which satisfies any (hence all) of the above conditions is said to be non-Archimedean, otherwise it is said to be Archimedean.

Vector spaces
Again the fundamental properties of the absolute value for real numbers can be used, with a slight modification, to generalize the notion to an arbitrary vector space.

A real valued function ||&thinsp;·&thinsp;|| on a vector space V over a field F, is called an absolute value (or more usually a norm) if it satisfies the following axioms:

For all a in F, and v, u in V,


 * {| cellpadding=10


 * $$\|\mathbf{v}\| \ge 0 $$
 * Non-negativity
 * $$\|\mathbf{v}\| = 0 \iff \mathbf{v} = 0$$
 * Positive-definiteness
 * $$\|a \mathbf{v}\| = |a| \|\mathbf{v}\| $$
 * Positive homogeneity or positive scalability
 * $$\|\mathbf{v} + \mathbf{u}\| \le \|\mathbf{v}\| + \|\mathbf{u}\| $$
 * Subadditivity or triangle inequality
 * }
 * $$\|\mathbf{v} + \mathbf{u}\| \le \|\mathbf{v}\| + \|\mathbf{u}\| $$
 * Subadditivity or triangle inequality
 * }
 * }

The norm of a vector is also called its length or magnitude.

In the case of Euclidean space Rn, the function defined by


 * $$\|(x_1, x_2, \cdots, x_n) \| = \sqrt{\sum_{i=1}^{n}(x_i)^2}$$

is a norm called the Euclidean norm. When the real numbers R are considered as the one-dimensional vector space R1, the absolute value is a norm, and is the p-norm for any p. In fact the absolute value is the "only" norm on R1, in the sense that, for every norm ||&thinsp;·&thinsp;|| on R1, ||&thinsp;x&thinsp;|| = ||&thinsp;1&thinsp;||&thinsp;·&thinsp;|&thinsp;x&thinsp;|. The complex absolute value is a special case of the norm in an inner product space. It is identical to the Euclidean norm, if the complex plane is identified with the Euclidean plane R2.

Algorithms
In the C programming language, the,  ,   (in C99),  ,  , and   functions compute the absolute value of an operand. Coding the integer version of the function is trivial, ignoring the boundary case where the largest negative integer is input:

int abs (int i) { if (i < 0) return -i; else return i; }

The floating-point versions are trickier, as they have to contend with special codes for infinities and not-a-number.

The function for absolute value in Fortran, Matlab, and GNU Octave is. It handles integer, real as well as complex numbers.

Using assembly language, it is possible to take the absolute value of a register in just three instructions (example shown for a 32-bit register on an x86 architecture, Intel syntax):

cdq xor eax, edx sub eax, edx

extends the sign bit of  into. If  is nonnegative, then   becomes zero, and the latter two instructions have no effect, leaving   unchanged. If  is negative, then   becomes 0xFFFFFFFF, or -1. The next two instructions then become a two's complement inversion, giving the absolute value of the negative value in.