Numeric operators

Provides a reference for numeric operators in OPL.

Note:

For C/C++ programmers: The numeric operators are the same as in C and C++.

Table 1. Numeric operators
Syntax Effect

x+y

x-y

x*y

x/y

The usual arithmetic operations.

Examples:

3 + 4.2 –> 7.2

100 - 120 –> -20

4 * 7.1 –> 28.4

6 / 5 –> 1.2

-x

Negation.

Examples:

- 142

-142

x%y

Returns the floating-point remainder of dividing x by y.

Examples:

12 % 5 –> 2

12.5 % 5 –> 2.5

x==y

x!=y

The operator == returns true if x and y are equal, and false otherwise. The operator != is the converse of ==.

Examples:

12 == 12 –> true

12 == 12.1 –> false

12 != 12.1 –> true

x<y

x<=y

x>y

x>=y

The operator < returns true if x is less than y, and false otherwise. The operator <= returns true if x is less than or equal to y, and false otherwise; and so on.

Examples:

-1 < 0 –> true

1 < 1 –> false

1 <= 1 –> true

x&y

x|y

x^y

The bitwise operations & (AND), | (inclusive OR), and ^ (exclusive OR), where x and y must be integers in the range -2**31+1 to 2**31-1 (-2147483647 to 2147483647.)

Examples:

14 & 9 –> 8 (because 1110 & 1001 –> 1000)

14 | 9 –> 15 (because 1110 | 1001 –> 1111)

14 ^ 9 –> 7 (because 1110 ^ 1001 –> 111)

~ x

The bitwise NOT operation, where x must be an integer in the range -2**31+1 to 2**31-1 (-2147483647 to 2147483647.)

Examples:

~ 14 –> 1 (because ~ 1110 –> 0001)

x<<y

x>>y

x>>>y

Binary shift operations, where x and y must be integers in the range -2**31+1 to 2**31-1 (-2147483647 to 2147483647.) The operator << shifts to the left, >> shifts to the right (maintaining the sign bit). The left operand specifies the value to be shifted. The right operand specifies the number of positions that the bits in the value are to be shifted.

>>> shifts to the right, shifting in zeros from the left.

Examples:

9 << 2 –> 36 (because 1001 << 2 –> 100100)

9 >> 2 –> 2 (because 1001 >> 2 –> 10)

-9 >> 2 –> -2 (because 1..11001 >> 2 –> 1..11110)

-9 >>> 2 –> 1073741821 (because 1..11001 >>> 2 –> 01..11110)