Integer Expressions

The following mathematical rules apply to integer variables:

  • On the View menu, select Split. To move the split bar, either click and drag it with the mouse or use the right and left arrow keys. Press Enter to accept the results.

  • Integer variables are 32-bit signed values.

  • No overflow checks are performed during addition, subtraction, multiplication, and division.

  • Integer division will discard the remainder.

When evaluating expressions that contain a mix of integer variables, regular numeric variables, and constants, it is important to understand how the calculation will be carried out. When in doubt, it is best to break a complicated, mixed expression into smaller expressions as follows.

PRO/5 decides how to evaluate a mixed expression:

  • The mode of an expression may be considered "real", "integer", or "neutral." When an expression is in "neutral" or "real" mode, all calculations are performed in the usual manner. While an expression is in "integer" mode, all adds, subtracts, multiplies, and divides are performed as pure integers with no overflow detection (with remainder discarded on division).

  • All expressions begin as "neutral." During left-to-right evaluation of an expression, the mode of the expression may be promoted to "integer" then "real." The mode will never be demoted (i.e., from "real" to "integer").

  • Subexpressions (within parentheses), subscripts, and function arguments are considered complete expressions. In other words, they begin evaluation as "neutral" regardless of the context in which they appear.

  • Upon encountering an integer variable, the expression mode becomes "integer" if it is not already set to "real."

  • An expression becomes "real" upon encountering a regular numeric variable, a non-integer constant, any function (including INT()), or an exponentiation operation.

  • Integer constants are, themselves, "neutral." However, addition, subtraction, multiplication, and division between two integer constants may affect the mode of the expression. If the expression mode is "integer", then integer constants will follow the same rules as integer variables. If the mode is "neutral" or "real", then integer constants will follow the usual rules. In this case, if the result of the operation between two integer constants is not a 32-bit integer, then the expression is forced to "real".

Here are some examples (assume A%=1, C%=3):

Expression

Answer

Notes

C%/2

1

C% forces the mode to be integer and 3/2=1 if the remainder is ignored

C%*.5

1.5

The constant (.5) forces the expression to be "real". The fact that an integer variable was involved no longer matters.

3/2

1.5

No integer variable exists to force the mode to be "integer".

3/2+A%

2.5

A% is not encountered until after the division is performed. The "neutral" division resulted in a non-integer and forced the expression to be "real."

A%+3/2

2

A% forces the integer constant division to be performed as an integer division. The integer constants themselves are not enough to override the "integer" mode once it had been established.