Arithmetic
Arithmetic
The arithmetic operators interpret their operands as numeric values and produce numeric results. When operating on a string, an arithmetic operator treats the string as its numeric value, according to the rules in String-to-Number Conversion. The arithmetic operators are as follows:
The unary positive operator gives its single operand a numeric interpretation. It does this by sequentially parsing the characters of the string as a number, until it encounters a character that cannot be interpreted as a number. It then returns whatever leading portion of the string was a well-formed numeric (or it returns 0 if no such interpretation was possible). For example:
USER>WRITE + "32 dollars and 64 cents"
32
The unary negative operator reverses the sign of a numerically interpreted operand. For example:
USER>SET x = -60
USER>WRITE x
-60
USER>WRITE -x
60
ObjectScript gives the unary negative operator precedence over the binary (two-operand) arithmetic operators.
To return the absolute value of a numeric expression, use the $ZABS function.
The addition operator adds two numeric values. For example:
USER>WRITE 2936.22 + 301.45
3237.67
The subtraction operator subtracts one numeric value from another. For example:
USER>WRITE 2936.22 - 301.45
2634.77
The multiplication operator multiplies two numeric values. For example:
USER>WRITE 9 * 5.5
49.5
The division operator divides one numeric value with another. For example:
USER>WRITE 355 / 113
3.141592920353982301
The integer division operator divides one numeric value with another and discards any fractional value. For example:
USER>WRITE 355 \ 113
3
When the two operands are positive, then the modulo operator returns the remainder of the left operand integer divided by the right operand. For example:
USER>WRITE 37 # 10
7
USER>WRITE 12.5 # 3.2
2.9
The exponentiation operator raises one numeric value to the power of the other numeric value. For example:
USER>WRITE 9 ** 2
81
Exponentiation can also be performed using the $ZPOWER function.