The following table lists the arithmetic operators used in PocketBuilder.
Operator |
Meaning |
Example |
---|---|---|
+ |
Addition |
|
- |
Subtraction |
Unless you have prohibited the use of dashes in identifier names, you must surround the minus sign with spaces. |
* |
Multiplication |
|
/ |
Division |
|
^ |
Exponentiation |
|
Operator shortcuts for assignments For information about shortcuts that combine arithmetic operators with assignments (such as ++ and +=), see Assignment.
Subtraction If the option Allow Dashes in Identifiers is checked on the Script tab in the Options dialog box, you must always surround the subtraction operator and the -- operator with spaces. Otherwise, PocketBuilder interprets the expression as an identifier.
For information about dashes in identifiers, see “Identifier names”.
Multiplication and division Multiplication and division are carried out to full precision (16–18 digits). Decimal numbers are rounded (not truncated) on assignment.
Calculation with NULL When you form an arithmetic expression that contains a NULL value, the expression’s value is null. Thinking of null as undefined makes this easier to understand.
For more information about null values, see “NULL values”.
Errors and overflows The following problems can occur when using arithmetic operators:
Division by zero, exponentiation of negative values, and so on cause errors during execution
Overflow of real, double, and decimal values causes errors during execution.
Overflow of signed or unsigned integers and longs causes results to wrap. However, because integers are promoted to longs in calculations, wrapping does not occur until the result is explicitly assigned to an integer variable.
For more information about type promotion, see “Datatype of PocketBuilder expressions”.
Subtraction This statement always means subtract B from A:
A - B
If DashesInIdentifiers is set to 1, the following statement means a variable named A-B, but if DashesInIdentifiers is set to 0, it means subtract B from A:
A-B
Precision for division These examples show the values that result from various operations on decimal values:
decimal {4} a,b,d,e,f
decimal {3} c
a = 20.0/3 // a contains 6.6667
b = 3 * a // b contains 20.0001
c = 3 * a // c contains 20.000
d = 3 * (20.0/3) // d contains 20.0000
e = Truncate(20.0/3, 4) // e contains 6.6666
f = Truncate(20.0/3, 5) // f contains 6.6667
Calculations with null When the value of variable c is null, the following assignment statements all set the variable a to null:
integer a, b=100, c
SetNULL(c)
a = b+c // all statements set a to NULL
a = b - c
a = b*c
a = b/c
Overflow This example illustrates the value of the variable i after overflow occurs:
integer i
i = 32767
i = i + 1 // i is now -32768