Product SiteDocumentation Site

Chapter 10. Numbers and Arithmetic

10.1. Precision
10.2. Arithmetic Operators
10.2.1. Power
10.2.2. Integer Division
10.2.3. Remainder
10.2.4. Operator Examples
10.3. Exponential Notation
10.4. Numeric Comparisons
10.5. Limits and Errors when Rexx Uses Numbers Directly
This chapter gives an overview of the arithmetic facilities of the Rexx language.
Numbers can be expressed flexibly. Leading and trailing whitespace characters are permitted, and exponential notation can be used. Valid numbers are, for example:

Example 10.1. Numbers

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                */

Ignoring exponential notation (Section 10.3, “Exponential Notation”), a number in Rexx is defined as follows:

>>-+------------+--+----------------------+--+-digits--------+---------->
   +-whitespace-+  +-sign--+------------+-+  +-digits.digits-+
                           +-whitespace-+    +-.digits-------+
                                             +-digits.-------+

>--+------------+--------------------------------------------------><
   +-whitespace-+

whitespace
are one or more blanks or horizontal tab characters.
sign
is either + or -.
digits
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 Section 10.2.4, “Operator Examples”.
The result of an arithmetic operation is formatted as a character string according to specific rules. The most important rules are:

10.1. Precision

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. The default may be overridden on a source-file basis using the ::OPTIONS directive (Section 3.5, “::OPTIONS”).
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.