Arithmetic operators in PowerBuilder
Description
The following table lists the arithmetic operators used in
PowerBuilder.
Operator |
Meaning |
Example |
---|---|---|
+ |
Addition |
Total=SubTotal+Tax |
– |
Subtraction |
Price=Price-Discount Unless you have |
* |
Multiplication |
Total=Quantity*Price |
/ |
Division |
Factor=Discount/Price |
^ |
Exponentiation |
Rank=Rating^2.5 |
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 2 3 4 5 6 7 8 |
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:
1 2 3 4 5 6 7 8 |
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:
1 2 3 |
integer i i = 32767 i = i + 1 // i is now -32768 |