IBM SC34-5764-01 Server User Manual


 
Chapter 16. Numbers and Arithmetic
REXX defines the usual arithmetic operations (addition, subtraction, multiplication, and division) in as
natural a way as possible. What this really means is that the rules followed are those that are
conventionally taught in schools and colleges.
During the design of these facilities, however, it was found that unfortunately the rules vary considerably
(indeed much more than generally appreciated) from person to person and from application to application
and in ways that are not always predictable. The arithmetic described here is, therefore, a compromise
that (although not the simplest) should provide acceptable results in most applications.
Introduction
Numbers (that is, character strings used as input to REXX arithmetic operations and built-in functions) can
be expressed very flexibly. Leading and trailing blanks are permitted, and exponential notation can be
used. Some valid numbers are:
12 /* a whole number */
'-76' /* a signed whole number */
12.76 /* decimal places */
' + 0.003 ' /* blanks around the sign and so forth */
17. /* same as "17" */
.5 /* same as "0.5" */
4E9 /* exponential notation */
0.73e-7 /* exponential notation */
In exponential notation, a number includes an exponent, a power of ten by which the number is multiplied
before use. The exponent indicates how the decimal point is shifted. Thus, in the preceding examples, 4E9
is simply a short way of writing 4000000000, and 0.73e-7 is short for 0.000000073.
The arithmetic operators include addition (+), subtraction (-), multiplication (*), power (**), division (/),
prefix plus (+), and prefix minus (-). In addition, there are two further division operators: integer divide (%)
divides and returns the integer part; remainder (//) divides and returns the remainder.
The result of an arithmetic operation is formatted as a character string according to definite rules. The
most important of these rules are as follows (see the “Definition” section for full details):
v Results are calculated up to some maximum number of significant digits (the default is 9, but you can
alter this with the NUMERIC DIGITS instruction to give whatever accuracy you need). Thus, if a result
requires more than 9 digits, it would usually be rounded to 9 digits. For example, the division of 2 by 3
would result in 0.666666667 (it would require an infinite number of digits for perfect accuracy).
v Except for division and power, trailing zeros are preserved (this is in contrast to most popular
calculators, which remove all trailing zeros in the decimal part of results). So, for example:
2.40 + 2 -> 4.40
2.40 - 2 -> 0.40
2.40 * 2 -> 4.80
2.40 / 2 -> 1.2
This behavior is desirable for most calculations (especially financial calculations).
If necessary, you can remove trailing zeros with the STRIP function (see page 192), or by division by 1.
v A zero result is always expressed as the single digit 0.
v Exponential form is used for a result depending on its value and the setting of NUMERIC DIGITS (the
default is 9). If the number of places needed before the decimal point exceeds the NUMERIC DIGITS
setting, or the number of places after the point exceeds twice the NUMERIC DIGITS setting, the
number is expressed in exponential notation:
1e6 * 1e6 -> 1E+12 /* not 1000000000000 */
1 / 3E10 -> 3.33333333E-11 /* not 0.0000000000333333333 */
© Copyright IBM Corp. 1992, 2009 217