Microsoft KB Archive/57710

From BetaArchive Wiki
Knowledge Base

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

Article ID: 57710

Article Last Modified on 11/21/2006

This article was previously published under Q57710


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.


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 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&
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.

Additional query words: MQuickB

Keywords: kbbug KB57710