Numbers and arithmetic operations
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 to numbers
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.
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.
- 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). - 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:
This behavior is desirable for most calculations (especially financial calculations).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 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 */
How to determine numeric settings
To determine the current settings of the NUMERIC options, use the built-in functions DIGITS, FORM, and FUZZ. These functions return the current settings of NUMERIC DIGITS, NUMERIC FORM, and NUMERIC FUZZ, respectively.
Errors that can occur during arithmetic operations
- Overflow or underflow
This error occurs if the exponential part of a result would exceed the range that the language processor can handle, when the result is formatted according to the current settings of NUMERIC DIGITS and NUMERIC FORM. The language defines a minimum capability for the exponential part, namely the largest number that can be expressed as an exact integer in default precision. Because the default precision is
9, REXX for CICS® TS supports exponents in the range-999999999through999999999.Because this allows for (very) large exponents, overflow or underflow is treated as a syntax error.
- Insufficient storage
Storage is needed for calculations and intermediate results, and on occasion an arithmetic operation may fail because of lack of storage. This is considered a terminating error as usual, rather than an arithmetic error.
Definition of arithmetic facilities
A precise definition of the arithmetic facilities of the REXX language is given in the following topics.