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.
Even when PocketBuilder performs a calculation at high enough precision to handle the results, assignment to a lower precision variable can cause overflow, producing the wrong result.
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:
Convert integer a to long
Convert integer b to long
Add the longs a and b
Convert the result from long to integer and assign the result to c
Convert integer c to long and assign the result to e
The assignment to c causes the long result of the addition to be truncated, causing overflow and wrapping. Assigning c to e cannot restore the lost information.