Numeric functions

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) returns 0. Under use warnings it emits Use of uninitialized value in abs.

  • Non-numeric string: abs("hello") returns 0 and warns under use warnings that the argument isn’t numeric.

  • Integer overflow boundary: abs of the most-negative platform integer (PERL_INT_MIN, e.g. -2**63 on 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) returns 0 (a positive zero). abs("-Inf") returns Inf. abs("NaN") returns NaNNaN has no sign that abs can meaningfully remove.

  • Complex numbers and big integers are not handled by the built-in. use Math::Complex overloads abs to compute the modulus; Math::BigIntext and Math::BigFloatext provide their own sign-stripping on blessed objects via operator overloading.

  • No in-place form: abs always returns a new scalar. To replace a variable’s value with its magnitude, assign back:

    $x = abs $x;
    
  • Parser note: the unary form abs $x binds tighter than , but looser than most arithmetic operators, so abs $x + 1 parses as abs($x) + 1, not abs($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; like abs it is a pure numeric unary with a default-to-$_ form

  • int — truncate toward zero; pairs with abs when you want the integer magnitude of a float

  • sprintf — formatted numeric output; use %d or %g together with abs to render unsigned magnitudes

  • $_ — the default argument when abs is called without one