Arithmetic operators in PowerBuilder
Description
The following table lists the arithmetic operators used in
PowerBuilder.
|
Operator |
Meaning |
Example |
|---|---|---|
|
+ |
Addition |
|
|
– |
Subtraction |
Unless you have prohibited the use of dashes in identifier |
|
* |
Multiplication |
|
|
/ |
Division |
|
|
^ |
Exponentiation |
|
Usage
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, PowerBuilder
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–28 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 at runtime. -
Overflow of real, double, and decimal values causes
errors at runtime. -
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 PowerBuilder
expressions”.
Examples
Subtraction
This statement always means subtract B from A:
|
1 |
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:
|
1 |
A-B |
Precision for division
These examples show the values that result from various operations
on decimal values:
|
1 |
decimal {4} a,b,d,e,f |
|
1 |
decimal {3} c |
|
1 |
a = 20.0/3           // a contains 6.6667 |
|
1 |
b = 3 * a            // b contains 20.0001 |
|
1 |
c = 3 * a            // c contains 20.000 |
|
1 |
d = 3 * (20.0/3)       // d contains 20.0000 |
|
1 |
e = Truncate(20.0/3, 4)   // e contains 6.6666 |
|
1 |
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:
|
1 |
integer a, b=100, c |
|
1 |
|
1 |
SetNULL(c) |
|
1 |
|
1 |
a = b+c  // all statements set a to NULL |
|
1 |
a = b - c |
|
1 |
a = b*c |
|
1 |
a = b/c |
Overflow
This example illustrates the value of the variable i after
overflow occurs:
|
1 |
integer i |
|
1 |
i = 32767 |
|
1 |
i = i + 1 // i is now -32768 |