General information
The datatype of an expression is important when it is the argument for a function or event. The expression's datatype must be compatible with the argument's definition. If a function is overloaded, the datatype of the argument determines which version of the function to call.
There are three types: numeric, string, and char datatypes.
General information
All numeric datatypes are compatible with each other.
What PowerBuilder does
PowerBuilder converts datatypes as needed to perform calculations and make assignments. When PowerBuilder evaluates a numeric expression, it converts the datatypes of operands to datatypes of higher precedence according to the operators and the datatypes of other values in the expression.
Order of precedence
The PowerBuilder numeric datatypes are listed here in order of highest to lowest precedence (the order is based on the range of values for each datatype):
Double |
Real |
Decimal |
LongLong |
UnsignedLong |
Long |
UnsignedInteger |
Integer |
Byte |
Rules for type promotion
Datatypes of operands
If operands in an expression have different datatypes, the value whose type has lower precedence is converted to the datatype with higher precedence.
Unsigned versus signed
Unsigned has precedence over signed, so if one operand is signed and the other is unsigned, both are promoted to the unsigned version of the higher type. For example, if one operator is a long and another UnsignedInteger, both are promoted to UnsignedLong.
Operators
The effects of operators on an expression's datatype are:
-
+, -, *
The minimum precision for addition, subtraction, and multiplication calculations is long. Integer types are promoted to long types before doing the calculation and the expression's resulting datatype is, at a minimum, long. When operands have datatypes of higher precedence, other operands are promoted to match based on the Datatypes of operands rule above.
-
/ and ^
The minimum precision for division and exponentiation is double. All types are promoted to double before doing the calculation, and the expression's resulting datatype is double.
-
Relational
Relational operators do not cause promotion of numeric types.
Datatypes of literals
When a literal is an operand in an expression, its datatype is determined by the literal's value. The datatype of a literal affects the type promotion of the literal and other operands in an expression.
Literal |
Datatype |
---|---|
Integer literals (no decimal point or exponent) within the range of Long |
Long |
Integer literals beyond the range of Long and within the range of UnsignedLong |
UnsignedLong |
Integer literals beyond the range of UnsignedLong and within the range of LongLong |
LongLong |
Numeric literals with a decimal point (but no exponent) |
Decimal |
Numeric literals with a decimal point and explicit exponent |
Double |
Out of range
Integer literals beyond the range of LongLong cause compiler errors.
General information
Assignment is not part of expression evaluation. In an assignment statement, the value of an expression is converted to the datatype of the left-hand variable. In the expression
c = a + b
the datatype of a+b is determined by the datatypes of a and b. Then, the result is converted to the datatype of c.
Overflow on assignment
Even when PowerBuilder performs a calculation at high enough precision to handle the results, assignment to a lower precision variable can cause overflow, producing the wrong result.
Example 1
Consider this code:
integer a = 32000, b = 1000 long d d = a + b
The final value of d is 33000. The calculation proceeds like this:
Convert integer a to long
Convert integer b to long
Add the longs a and b
Assign the result to the long d
Because the variable d is a long, the value 33000 does not cause overflow.
Example 2
In contrast, consider this code with an assignment to an integer variable:
integer a = 32000, b = 1000, c long e c = a + b e = c
The resulting value of c and e is -32536. The calculation proceeds like this:
Add the integers a and b
Assign the result to c
Convert integer c to long and assign the result to e
The assignment to the integer variable c causes the long result of the addition to be truncated, causing overflow and wrapping. Assigning c to e cannot restore the lost information.
General information
There is no explicit char literal type.
String literals convert to type char using the following rules:
-
When a string literal is assigned to a char variable, the first character of the string literal is assigned to the variable. For example:
char c = "xyz"
results in the character x being assigned to the char variable c.
-
Special characters (such as newline, formfeed, octal, hex, and so on) can be assigned to char variables using string conversion, such as:
char c = "~n"
String variables assigned to char variables also convert using these rules. A char variable assigned to a string variable results in a one-character string.
Assigning strings to char arrays
As with other datatypes, you can use arrays of chars. Assigning strings to char arrays follows these rules:
-
If the char array is unbounded (defined as a variable-size array), the contents of the string are copied directly into the char array.
-
If the char array is bounded and its length is less than or equal to the length of the string, the string is truncated in the array.
-
If the char array is bounded and its length is greater than the length of the string, the entire string is copied into the array along with its zero terminator. Remaining characters in the array are undetermined.
Assigning char arrays to strings
When a char array is assigned to a string variable, the contents of the array are copied into the string up to a zero terminator, if found, in the char array.
Using both strings and chars in an expression
Expressions using both strings and char arrays promote the chars to strings before evaluation. For example, the following promotes the contents of c to a string before comparison with the string "x":
char c . . . if (c = "x") then
Using chars in PowerScript functions
All PowerScript functions that take strings also take chars and char arrays, subject to the conversion rules described above.