Arithmetic Operators
Arithmetic operators work on valid numeric constants or
on variables that represent valid numeric constants.
- Types of Numeric Constants
- 12
- A
whole number
has no decimal point or commas. Results of arithmetic operations
with
whole numbers can contain a maximum of nine digits unless you override
this default by using the
NUMERIC DIGITS instruction. For information about the NUMERIC DIGITS
instruction, see
NUMERIC
. Examples of whole numbers are:
123456789 0 91221 999 - 12.5
- A
decimal number
includes a decimal point.
Results of arithmetic operations with decimal numbers are limited
to a total maximum of nine digits (NUMERIC DIGITS default) before
and
after
the decimal. Examples of decimal numbers are:
123456.789 0.888888888 - 1.25E2
- A
floating point number
in exponential notation, is
said to
be in scientific notation. The number after the "E" represents the
number of places the decimal
point moves. Thus 1.25E2 (also written as 1.25E+2) moves the decimal
point to the right two places
and results in 125. When an "E" is followed by a minus (-), the
decimal point moves to the left. For
example, 1.25E-2 is .0125.
You can use floating point numbers to represent very large or very small numbers. For more information about scientific notation (floating point numbers), see Exponential Notation.
- -12
- A signed number with a minus (-) next to the number represents a negative value. A signed number with a plus (+) next to the number represents a positive value. When a number has no sign, it is processed as if it has a positive value.
The arithmetic operators you can use are:
- Operator
- Meaning
- +
- Add
- -
- Subtract
- *
- Multiply
- /
- Divide
- %
- Divide and return a whole number without a remainder
- //
- Divide and return the remainder only
- **
- Raise a number to a whole number power
- - number
- (Prefix -) Same as the subtraction 0 - number
- + number
- (Prefix +) Same as the addition 0 + number
Using numeric constants and arithmetic operators,
you can write arithmetic expressions such as:
7 + 2 /* result is 9 */
7 - 2 /* result is 5 */
7 * 2 /* result is 14 */
7 ** 2 /* result is 49 */
7 ** 2.5 /* result is an error */
Division
Notice that three operators represent
division. Each operator computes the result of a division expression
in a different way.
- /
- Divide and express the answer possibly as a decimal number. For
example:
7 / 2 /* result is 3.5 */ 6 / 2 /* result is 3 */ - %
- Divide and express the answer as a whole number. The remainder
is ignored. For example:
7 % 2 /* result is 3 */ - //
- Divide and express the answer as the remainder only. For example:
7 // 2 /* result is 1 */
Order of evaluation
When you have more than
one operator in an arithmetic expression, the order of numbers and
operators can be critical. For example, in the following expression,
which operation does the language processor perform first?
7 + 2 * (9 / 3) - 1
Proceeding
from left to right, the language processor evaluates the expression
as follows:
- First it evaluates expressions within parentheses.
- Then it evaluates expressions with operators of higher priority before expressions with operators of lower priority.
Arithmetic operator priority is as follows, with the highest first:
| Operator symbol | Operator description |
|---|---|
| - + | Prefix operators |
| ** | Power (exponential) |
| * / % // | Multiplication and division |
| + - | Addition and subtraction |
Thus, the preceding example would be evaluated in the following
order:
- Expression in parentheses
7 + 2 * (9 / 3) - 1 \___/ 3 - Multiplication
7 + 2 * 3 - 1 \___/ 6 - Addition and subtraction from left to right
7 + 6 - 1 = 12
Using arithmetic expressions
You can use
arithmetic expressions in
a program
many
different ways. The following example uses several arithmetic operators
to round and remove extra decimal places from a dollar and cents
value.
Figure 1. Example Using Arithmetic Expressions
/****************************** REXX
*********************************/
/* This
program
computes the total price of an item including sales */
/* tax, rounded to two decimal places. The cost and percent of the
*/
/* tax (expressed as a decimal number) are passed to the
program
*/
/* when you run it. */
/*********************************************************************/
PARSE ARG cost percent_tax
total = cost + (cost * percent_tax) /* Add tax to cost. */
price = ((total * 100 + .5) % 1) / 100 /* Round and remove extra */
/* decimal places. */
SAY 'Your total cost is £'price'.'
Exercises: calculating arithmetic expressions
What line of output does the following program produce?/****************************** REXX
******************************/
pa = 1
ma = 1
kids = 3
SAY "There are" pa + ma + kids "people in this family."
- 6 - 4 + 1
- 6 - (4 + 1)
- 6 * 4 + 2
- 6 * (4 + 2)
- 24 % 5 / 2
ANSWERS
- There are 5 people in this family.
- The values are as follows:
- 3
- 1
- 26
- 36
- 2