This chapter gives an overview of the arithmetic facilities of the Rexx language.
Numbers can be expressed flexibly. Leading and trailing blanks are permitted, and exponential notation can be used. Valid numbers are, for example:
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 */
A number in Rexx is defined as follows:
>>-+--------+--+------------------+--+-digits--------+----------> +-blanks-+ +-sign--+--------+-+ +-digits.digits-+ +-blanks-+ +-.digits-------+ +-digits.-------+ >--+--------+-------------------------------------------------->< +-blanks-+
are one or more spaces.
is either + or -.
are one or more of the decimal digits 0-9.
Note that a single period alone is not a valid number.
The arithmetic operators include addition (+), subtraction (-), multiplication (*), power (**), division (/), prefix plus (+), and prefix minus (-). In addition, it includes integer divide (%), which divides and returns the integer part, and remainder (//), which divides and returns the remainder. For examples of the arithmetic operators, see Operator Examples.
The result of an arithmetic operation is formatted as a character string according to specific rules. The most important rules are:
Results are calculated up to a maximum number of significant digits. The default is 9, but you can alter it with the NUMERIC DIGITS instruction. Thus, if a result requires more than 9 digits, it is rounded to 9 digits. For example, the division of 2 by 3 results in 0.666666667.
Except for division and power, trailing zeros are preserved. For example:
2.40 + 2 -> 4.40 2.40 - 2 -> 0.40 2.40 * 2 -> 4.80 2.40 / 2 -> 1.2If necessary, you can remove trailing zeros with the STRIP method (see STRIP), the STRIP function (see STRIP), or by division by 1.
A zero result is always expressed as the single digit 0.
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 */
Precision is the maximum number of significant digits that can result from an operation. This is controlled by the instruction:
>>-NUMERIC DIGITS--+------------+--;--------------------------->< +-expression-+
The expression is evaluated and must result in a positive whole number. This defines the precision (number of significant digits) of a calculation. Results are rounded to that precision, if necessary.
If you do not specify expression in this instruction, or if no NUMERIC DIGITS instruction has been processed since the start of a program, the default precision is used. The Rexx standard for the default precision is 9.
NUMERIC DIGITS can set values smaller than nine. However, use small values with care because the loss of precision and rounding affects all Rexx computations, including, for example, the computation of new values for the control variable in DO loops.