Skip to main content

Arithmetic Operators

Arithmetic Operators

The arithmetic operators interpret their operands as numeric values and produce numeric results. When operating on a string, an arithmetic operators treats the string as its numeric value, according to the rules described in String-to-Number Conversion. ObjectScript provides the following arithmetic operators:

Unary Positive (+)

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:

 WRITE + "32 dollars and 64 cents"        // 32

For details, see the Unary Positive (+) reference page.

Unary Negative (-)

The unary negative operator (-) reverses the sign of a numerically interpreted operand. For example:

 SET x = -60
 WRITE " x: ", x,! // -60
 WRITE "-x: ",-x,! // 60

ObjectScript gives the unary negative operator precedence over the binary (two-operand) arithmetic operators.

For details, see the Unary Negative (-) reference page.

To return the absolute value of a numeric expression, use the $ZABS function.

Addition (+)

The addition operator adds two numeric values. For example:

 WRITE 2936.22 + 301.45 //  3237.67

For details, see the Addition (+) reference page.

Subtraction (-)

The subtraction operator subtracts one numeric value from another. For example:

 WRITE 2936.22 - 301.45 // 2634.77

For details, see the Subtraction (-) reference page.

Multiplication (*)

The multiplication operator multiplies two numeric values. For example:

 WRITE 9 * 5.5 // 49.5

For details, see the Multiplication (*) reference page.

Division (/)

The division operator divides one numeric value with another. For example:

 WRITE 9 / 5.5 // 1.636363636363636364

For details, see the Division ( / ) reference page.

Integer Division ( \ )

The integer operator divides one numeric value with another and discards any fractional value. For example:

 WRITE "355 \ 113 = ", 355 \ 113 // 3

For details, see the Integer Division ( \ ) reference page.

Modulo (#)

When the two operands are positive, then the modulo operation is the remainder of the left operand integer divided by the right operand. For example:

 WRITE "37 # 10 = ",37 # 10,! // 7
 WRITE "12.5 # 3.2 = ",12.5 # 3.2,! // 2.9

For details, see the Modulo (#) reference page.

Exponentiation (**)

The exponentiation operator raises one numeric value to the power of the other numeric value. For example:

 WRITE "9 ** 2 = ",9 ** 2,! // 81

For details, see the Exponentiation (**) reference page. Exponentiation can also be performed using the $ZPOWER function.

Note:

InterSystems IRIS supports two representations of numbers: ObjectScript decimal floating-point (referred to as decimal format) and IEEE double-precision binary floating-point (referred to as $DOUBLE, generally used for special purposes). ObjectScript automatically converts a decimal value to the corresponding $DOUBLE value in the following situations:

  • If an arithmetic operation involves a $DOUBLE value, ObjectScript converts all numbers in the operation to $DOUBLE.

  • If an operation results in a number that is too large to be represented in decimal format, ObjectScript automatically converts this number to $DOUBLE, rather than issuing a <MAXNUMBER> error.

For details on these formats, see Numeric Computing in InterSystems Applications.

FeedbackOpens in a new tab