25.01.2015 Views

Using Caché ObjectScript - InterSystems Documentation

Using Caché ObjectScript - InterSystems Documentation

Using Caché ObjectScript - InterSystems Documentation

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Arithmetic Operators<br />

5.2.2 Unary Negative Operator (-)<br />

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

Set x = -60<br />

Write " x: ", x,! // -60<br />

Write "-x: ",-x,! // 60<br />

If its operand has a string value, the unary negative operator interprets it as a numeric value before reversing its sign. This<br />

numeric interpretation is exactly the same as that performed by the unary positive operator, described above. For example:<br />

Set x = -23<br />

Write -"32 dollars and 64 cents" // -32<br />

<strong>Caché</strong> <strong>ObjectScript</strong> gives the unary negative operator precedence over the binary arithmetic operators. <strong>Caché</strong> <strong>ObjectScript</strong><br />

first scans a numeric expression and performs any unary negative operations. Then, <strong>Caché</strong> <strong>ObjectScript</strong> evaluates the<br />

expression and produces a result.<br />

In the following example, <strong>Caché</strong> <strong>ObjectScript</strong> scans the string and encounters the numeric value of 2 and stops there. It<br />

then applies the unary negative operator to the value and uses the concatenate operator (_) to concatenate the value “Rats”<br />

from the second string to the numeric value.<br />

Write -"2Cats"_"Rats" // -2Rats<br />

To return the absolute value of a numeric expression, use the $ZABS function.<br />

5.2.3 Addition Operator (+)<br />

The addition operator produces the sum of two numerically interpreted operands. It uses any leading valid numeric characters<br />

as the numeric values of the operands and produces a value that is the sum of the numeric value of the operands.<br />

The following example performs addition on two numeric literals:<br />

Write 2936.22 + 301.45 // 3237.67<br />

The following example performs addition on two locally defined variables:<br />

Set x = 4<br />

Set y = 5<br />

Write "x + y = ",x + y // 9<br />

The following example performs string arithmetic on two operands that have leading digits, adding the resulting numerics:<br />

Write "4 Motorcycles" + "5 bicycles" // 9<br />

The following example illustrates that leading zeros on a numerically evaluated operand do not affect the results the operator<br />

produces:<br />

Write "007" + 10 // 17<br />

5.2.4 Subtraction Operator (-)<br />

The subtraction operator produces the difference between two numerically interpreted operands. It interprets any leading,<br />

valid numeric characters as the numeric values of the operand and produces a value that is the remainder after subtraction.<br />

The following example performs subtraction on two numeric literals:<br />

Write 2936.22 - 301.45 // 2634.77<br />

The following example performs subtraction on two locally defined variables:<br />

<strong>Using</strong> <strong>Caché</strong> <strong>ObjectScript</strong> 37

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!