Arithmetic operators#
The numeric four-function-calculator set, plus modulus and exponentiation, plus the unary sign operators.
Operator | Operation | Arity | Associativity |
|---|---|---|---|
| exponentiation | binary | right |
| multiplication | binary | left |
| division | binary | left |
| modulus | binary | left |
| addition | binary | left |
| subtraction | binary | left |
| unary plus | unary | right |
| unary negation | unary | right |
All arithmetic operators evaluate their operands in numeric context
strings are coerced via the rule “leading numeric prefix wins, the rest is ignored (with an
Argument "..." isn't numericwarning underuse warnings)”.
my $x = 5 + 3; # 8
my $y = 10 / 3; # 3.3333333333333…
my $z = 2 ** 10; # 1024
my $rem = 17 % 5; # 2
my $neg = -$x; # -8
Precedence and associativity#
** binds tightest and is right-associative. *, /, % form one group; +, - form another. Unary + and - bind between ** and * (high precedence).
2 ** 3 ** 2 # 2 ** 9 = 512 (right-assoc: top-down)
-2 ** 2 # -(2 ** 2) = -4 (** binds tighter than unary -)
(-2) ** 2 # 4 (parens override)
6 / 2 / 3 # (6 / 2) / 3 = 1 (left-assoc on /)
The -2 ** 2 case bites everyone exactly once. Parenthesise the sign when the base might be negative.
Division#
/ always returns a floating-point result, even for integer operands that divide exactly:
10 / 5 # 2 -- numerically equal, stored as int or float
# depending on the runtime; treat as numeric
10 / 4 # 2.5
10 / 3 # 3.3333333333333335
For integer division, use int:
int(10 / 4) # 2
int(-7 / 2) # -3 (truncation toward zero, not floor)
Division by zero is a fatal error (not a warning, not an infinity-producing operation):
my $bad = 1 / 0; # dies: Illegal division by zero
Modulus#
% returns the integer remainder. Both operands are converted to integers first; the sign of the result follows the right operand (Perl-flavoured “sign of divisor”, same as Python, unlike C).
17 % 5 # 2
-17 % 5 # 3 -- result follows sign of $b ( 5)
17 % -5 # -3 -- result follows sign of $b (-5)
-17 % -5 # -2
For floating-point remainder, use POSIX::fmod from POSIX.
Exponentiation#
** accepts any pair of numbers including non-integer exponents:
2 ** 10 # 1024
2 ** 0.5 # 1.4142135623730951 -- square root
$x ** -1 # 1/$x -- reciprocal
For sqrt, the dedicated sqrt function is clearer (and named-unary, so it parses with one argument greedily - see precedence).
Unary + and unary -#
Unary - negates. Unary + is a no-op on numbers; its only practical use is to force numeric context, or to disambiguate a syntactic edge case where Perl would otherwise treat the next token as a function name:
sub foo { 42 }
print foo (1+2)*3; # prints 42*3 ... wait, no:
# parses as: print( foo(1+2) ) * 3
# because foo(...) sticks to the parens.
print foo +(1+2)*3; # parses as: print( foo + ((1+2)*3) )
# the unary + signals "not a function call"
You will rarely need this. It is documented because it appears in older code.
Compound assignment#
Every binary arithmetic operator has a compound assignment form (+=, -=, *=, /=, %=, **=). See assignment.
See also#
String operators -
.andx, the string equivalents of+and*.Numeric comparison -
< <= == > >= != <=>.Precedence - full table.