13.1 Built-in Numeric Operators and Functions

13.1  Built-in Numeric Operators and Functions



Perl provides the standard arithmetic operators: +, -, *, / and ** for addition, subtraction, multiplication, division, exponentiation, respectively. It also provides the modulo division operator %. a % b gives the remainder when a is divided by b. In addition, Perl provides two operators, ++ and --, for auto-incrementing and auto-decrementing. $a++ and ++$a both increment the value of the numeric scalar $a.
However, $a++ performs the incrementation before the value of $a is used, if $a happens to appear in any expression or statement. This operation should be carefully used. Otherwise, an expression or statement may become difficult to understand. The auto-decrement operator -- also works similarly.

For purposes of creating Boolean expressions with numbers or numeric expression, the available operators are

==    !=   <  >  <= >=



for equality, inequality, less than, greater than, less than or equal to, and greater than or equal to, respectively. Additionally, there is the “ship” operator <=>.

$a <=> $b



returns -1, 0, or 1, depending on whether $a is less than, equal to, or greater than $b, respectively.

Perl also has a few built-in functions that deal with mathematical calculations. It has functions for the basic trigonometric calculations: sin, cos, tan and atan2. The last function atan2 takes two arguments Y and X, and returns the arc tangent of Y/X. There also a few other built-in functions such as abs, int, sqrt, exp, rand for performing obvious tasks such as obtaining the absolute value of an operand, the integer part of a number, the square root of a positive number, the value of e raised to the power of an exponent, and to generate random numbers.