Arithmetic Operators

Operators are not the most thrilling of Perl topics to read about, but you need them to build expressions. Perl has a fairly robust set of operators for building expressions with scalars. You'll learn about some of these operators today, and most of the remainder of them tomorrow in Day 3, “More Scalar Data and Operators.”

We'll start with the arithmetic operators, which perform arithmetic operations on numeric data. Strings are converted to numbers as needed. Perl includes the operators shown in Table 2.2 for basic arithmetic operations, with the operands usually appearing on either side of the operator, as you'd expect.

Table 2.2. Arithmetic Operators
Operator What it Does For Example Results In
+ Addition 3 + 4 7
- Subtraction (2 operands) Negation (1 operand) 4 - 2 -5 2 -5
* Multiplication 5 * 5 25
/ Floating-point division 15 / 4 3.75
** Exponent 4**5 1024
% Modulus (remainder) 15 % 4 3

Little of this should be a surprise to you, although the exponent operator might be new. For exponents, the left-side operand is the base, and the right side is the exponent, so 10**3 is the same as 103 and evaluates to 1000.

Operator precedence—the order in which operators are calculated, if there are more than one in a single expression, is for arithmetic as you learned it in Ninth grade: multiplication, division, and modulus are performed first, then addition and subtraction. However, negation (unary -, as it's sometimes called) has a higher precedence than multiplication, and the exponent operator has an even higher precedence than that (higher precedence means that those expressions are evaluated first). You'll learn more about operator precedence tomorrow on Day 3, “More Scalar Data and Operators.”

Arithmetic and Decimal Precision

All arithmetic in Perl is done using floating-point numbers. Although this is convenient for doing simple math (no worrying about converting between integers and floats), there are a number of gotchas surrounding floating-point math that you might need to watch out for.

First is that the division operator always uses floating-point division. The expression 15 / 4 results in 3.75, not 3 as it would be in integer division (and what you would expect if you're coming from C). If you really want an integer result, you can use the int function to remove the decimal part, like this:

$result_as_int = int 15 / 4;  # result will be 3

Another side effect of floating-point division is that sometimes you end up with way more precision than you want. For example, take the simple expression:

print 10 / 3;

This expression results in the number 3.33333333333333. This is fine if you want the number 3.33333333333333, but not if all you want is, say, 3.33 or just 3.3.

Perl has no built-in mathematical rounding functions, but you can use its printing functions to accomplish the same thing. The printf and sprintf functions, borrowed from C, are used to format a numerical value inside a string. The printf function, like print, prints the value to the screen, whereas sprintf just returns a string that you can assign to a variable or use inside some other expression. Because Perl converts happily between numbers and strings, you can use either of these functions to control rounding. For example, to print 3.33333333333 to the screen as a value with only two decimal places, use this expression:

printf("%.2f", 10/3);

The %.2f part of this expression is the important part; it says print a floating-point value (f) with 2 decimal places after the decimal point (.2).

To convert a value to a rounded-off equivalent inside your Perl script, without printing anything, use sprintf instead of printf:

$value = sprintf("%.2f", $value); # round $value to 2 decimals

You'll learn a little more about printf and sprintf tomorrow.

The final gotcha to note about floating-point arithmetic is in what's called a rounding-off error. Because of the way floating-point numbers are stored, sometimes very simple floating-point arithmetic might result in values that are extremely close to, but not quite, what you'd expect. For example, a simple operation such as 4.5 + 5.7 might actually result in the number 10.199999999999999, rather than 10.2 as you might expect. Most of the time this isn't a problem, as Perl can keep track of the numbers internally, and print will cover up very small inaccuracies like this when you actually print the numbers. One particular place it will show up is if you attempt to compare the value of an expression like this to a constant—a test to see if the expression 4.5 + 5.7 is equal to 10.2 might return false. Keep this rounding-off error in mind as you work with Perl—and particularly watch out for it if you start getting results you don't expect.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset