
Arithmetic Operators
Arithmetic operators combine operands comprising one or more variables, constants, or intrinsic functions. Resulting arithmetic expressions can be combined with other expressions almost indefinitely. The syntax of arithmetic expressions is:
expression operator expression
Table 1 lists the arithmetic operators used in BASIC, in order of evaluation.
Operator | Operation | Sample Expression |
---|---|---|
- | Negation | -X |
^ ** |
Exponentiation | X ^ Y X ** Y |
* / |
Multiplication Division |
X * Y X / Y |
+ - |
Addition Subtraction |
X + Y X - Y |
You can use parentheses to change the order of evaluation. Operations on expressions enclosed in parentheses are performed before those outside parentheses.
The following expression is evaluated as 112 + 6 + 2, or 120:
(14 * 8) + 12 / 2 + 2
On the other hand, the next expression is evaluated as 14 * 20 / 4, or 280 / 4, or 70:
14 * (8 + 12) / (2 + 2)
The result of any arithmetic operation involving the null value is the null value. Since the null value is unknown, the result of combining it with anything must also be unknown. So in the following example, B is the null value:
A = @NULL
B = 3 + A
The values of arithmetic expressions are internally maintained with the full floating-point accuracy of the system.
If a character string variable containing only numeric characters is used in an arithmetic expression, the character string is treated as a numeric variable. That is, the numeric string is converted to its equivalent internal number and then evaluated numerically in the arithmetic expression. For example, the following expression is evaluated as 77:
55 + "22"
If a character string variable containing nonnumeric characters is used in an arithmetic expression, a warning message appears, and the string is treated as zero. For example, the following expression is evaluated as 85, and a message warns that the data is nonnumeric:
"5XYZ" + 85
A BASIC program compiled in an INFORMATION or a PIOPEN flavor account has arithmetic instructions capable of operating on multivalued data. The following statement in a program compiled in an INFORMATION or a PIOPEN flavor account is valid:
C = (23:@VM:46) * REUSE(2)
In a BASIC program compiled in an IDEAL, PICK, PIOPEN, REALITY, or IN2 flavor account, arithmetic can be performed on string data only if the string can be interpreted as a single-valued number. The previous statement successfully compiles in PICK, PIOPEN, IN2, REALITY, and IDEAL flavor accounts but causes a run-time error. The REUSE function converts 2 to a string which then has to be converted back to a number for the arithmetic operation. This is harmless. The multivalued string cannot be converted to a number and causes a nonnumeric data warning.
The IDEAL flavor uses single-valued arithmetic because of the performance penalty incurred by multivalued arithmetic. To perform multivalued arithmetic in IDEAL, PICK, PIOPEN, IN2, and REALITY flavor accounts, use the VEC.MATH option of the $OPTIONS statement.