Arithmetic Operators
Parentheses change the order in which arithmetic operations are performed.
Operations within parentheses are performed first. Inside parentheses, the
usual order of operation is maintained.
Generally, two consecutive operators must be separated by parentheses.
Exceptions to this rule are * -, * +, ^ -, and ^ +.
Integer Division
Integer division is denoted by the backslash (\) instead of the forward
slash (/), which indicates floating-point division. Before integer division
is performed, operands are rounded to integers or long integers, and so
must be greater than -2,147,483,648.5 and less than +2,147,483,647.5. The
quotient of an integer division is truncated to an integer.
Example
PRINT 10 \ 4, 10 / 4, -32768.499 \ 10, -32768.499 / 10
Output
2 2.5 -3276 -3276.8499
Modulo Arithmetic
Modulo arithmetic is denoted by the modulus operator MOD. Modulo arithmetic
provides the remainder, rather than the quotient, of an integer division.
Example
X% = 10.4 \ 4
REMAINDER% = INT(10.4) - 4*X% '10\4 = 2, with remainder 2
PRINT REMAINDER%, 10.4 MOD 4
Output
2 2
Overflow and Division by Zero
Dividing by zero, raising zero to a negative power, or arithmetic overflow
produce run-time errors. These errors can be trapped by an error-trapping
routine. The following BASIC statements are useful for error trapping:
- allows user to define error codes
- error handling
- continue execution after error-trapping
- error status