BUG: Math w/ Long-Integer Constants Gives Bad Answer Compiled (57710)






This article was previously published under Q57710

SYMPTOMS

If an expression contains an addition, subtraction, or multiplication of two long-integer constants so that the computed result exceeds 32,767 (the maximum short integer), the compiled application gives wrong numeric results. (Division works correctly.)

Microsoft has confirmed this to be a bug with applications compiled with Microsoft QuickBasic Version 1.00 for Macintosh systems. We are researching this problem and will post new information as it becomes available. This problem does not occur in the interpreter in QuickBasic Version 1.00.

To work around this problem in compiled programs, store one or more of the literals in a variable and use the variable in the computational expression.

MORE INFORMATION

Note that long-integer constants (or literals) are numeric digits with an & (ampersand) suffix, and can range from -2147483648& to +2147483648&.

Code Example

The following code example illustrates the problem described above:
a& = 256& * 256&    ' Multiplication results greater than 32,767 fail.
PRINT a&
PRINT 65536& + 1&   ' Addition results greater than 32,767 fail.
PRINT 65536& - 1&   ' Subtraction results greater than 32,767 fail.
PRINT 131072& / 2&  '**** Division works correctly.
				
The results of the above program are as follows:
   Interpreted:   |  Compiled:
   ---------------+------------
   65536          |      0
   65537          |      1
   65535          |     -1
   65536          |  65536
				
[Note: The resultant value from the compiled application is actually just the low word of the completed computation. The compiled program is failing to keep the high word of the long-integer calculation. For example, if the resultant is equal to a power of 2 larger than 2^15, which means all bits in the low word are zero, then the compiled program reports zero.]

The following program works around the problem:
tmp& = 256&
a& = tmp& * 256&
PRINT a&
tmp& = 65536&
PRINT tmp& + 1&
PRINT tmp& - 1&
PRINT 131072& / 2&          '**** no change for division
				
The results for this altered program will match the correct interpreter results listed above.

Modification Type: Minor Last Reviewed: 1/9/2003
Keywords: kbbug KB57710