BUG: Incorrect Result, No "Overflow" Error, in Compiled App (71239)






This article was previously published under Q71239

SYMPTOMS

The following code fails to give an "Overflow" error when compiled and run as an application, and prints an incorrect value of -32768:
   x% = 32767 + 1
   print x%
   while mouse(0)<> 1:wend
				
An "Overflow" error correctly occurs when the code is run in the interpreter.

This problem occurs in Microsoft QuickBasic versions 1.00, 1.00a, and 1.00b for the Macintosh System. Microsoft is researching this problem and will post new information here when it becomes available.

MORE INFORMATION

The happens because of "constant folding" (also known as "constant arithmetic"). Constant expressions are evaluated at compile time and replaced with the result. Thus, an expression such as x% = 5 + 5 is folded by the compiler into a single assignment statement of the form x% = 10.

Thus x% = 32767 + 1 is compiled as x% = -32768. In this case, the maximum value for an integer is reached and the value gets rolled over (the sign bit gets set and a negative number is received). The expression x% = 32767 + 2 is translated to x% = -32767; the expression x% = 32767 + 3 is translated to x% = -32766; and so on.

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