abs#
Return the absolute value of a number.
abs takes a single scalar, forces it to numeric context, and returns
its magnitude — the value with any sign stripped. Non-numeric strings
are converted using the same rules as arithmetic (leading whitespace
and an optional sign, followed by digits; the rest is discarded, with
a warning under use warnings). If VALUE is omitted, abs operates
on $_.
Synopsis#
abs VALUE
abs
abs($x)
What you get back#
A non-negative number. The result is an integer if the input was an
integer within the platform’s integer range, otherwise a floating-point
number. abs never changes its argument — the result is a fresh
scalar, so abs $x leaves $x alone.
Global state it touches#
With no argument, abs reads $_. It neither writes nor reads any
other special variable. Under use warnings, a non-numeric argument
triggers the standard Argument "…" isn't numeric warning via the
normal numeric-conversion path.
Examples#
Strip the sign from an integer:
print abs(-7); # 7
print abs(7); # 7
Floating-point magnitude — the result keeps the full precision of the input:
print abs(-3.14159); # 3.14159
print abs(-1e-10); # 1e-10
Use the default argument form inside a loop over $_:
for (-3, -1, 0, 2, 4) {
print abs, "\n"; # 3, 1, 0, 2, 4 on separate lines
}
Distance between two points on a number line:
sub distance { abs($_[0] - $_[1]) }
print distance(10, 3); # 7
print distance(3, 10); # 7
String that looks like a negative number — abs parses the leading
numeric prefix and strips the sign:
print abs("-42"); # 42
print abs(" -3.5xyz"); # 3.5 (warning under use warnings)
Edge cases#
abs(undef)returns0. Underuse warningsit emitsUse of uninitialized value in abs.Non-numeric string:
abs("hello")returns0and warns underuse warningsthat the argument isn’t numeric.Integer overflow boundary:
absof the most-negative platform integer (PERL_INT_MIN, e.g.-2**63on 64-bit builds) promotes to a floating-point result, because the positive counterpart does not fit in a signed integer:use Config; my $min = -(2 ** ($Config{ivsize} * 8 - 1)); print abs($min); # 9.22337203685478e+18 on 64-bit
IEEE 754 special values:
abs(-0.0)returns0(a positive zero).abs("-Inf")returnsInf.abs("NaN")returnsNaN—NaNhas no sign thatabscan meaningfully remove.Complex numbers and big integers are not handled by the built-in.
use Math::Complexoverloadsabsto compute the modulus;Math::BigIntext andMath::BigFloatext provide their own sign-stripping on blessed objects via operator overloading.No in-place form:
absalways returns a new scalar. To replace a variable’s value with its magnitude, assign back:$x = abs $x;
Parser note: the unary form
abs $xbinds tighter than,but looser than most arithmetic operators, soabs $x + 1parses asabs($x) + 1, notabs($x + 1). Parenthesise when in doubt:print abs $x + 1; # abs($x) + 1 print abs($x + 1); # abs of the sum
Differences from upstream#
Fully compatible with upstream Perl 5.42.
See also#
sqrt— square root; likeabsit is a pure numeric unary with a default-to-$_formint— truncate toward zero; pairs withabswhen you want the integer magnitude of a floatsprintf— formatted numeric output; use%dor%gtogether withabsto render unsigned magnitudes$_— the default argument whenabsis called without one