Microsoft KB Archive/21844

= Integer Overflow Handling in Compiler Differs from Interpreter =

Article ID: 21844

Article Last Modified on 11/21/2006

-

APPLIES TO


 * Microsoft QuickBasic 1.0
 * Microsoft QuickBASIC 1.0b
 * Microsoft QuickBASIC 1.0b
 * Microsoft QuickBasic 1.01
 * Microsoft QuickBasic 1.02

-



This article was previously published under Q21844



SUMMARY
Question:

Why does the compiler give negative numbers or "Overflow" errors in intermediate calculations involving two integers, when the same program works correctly in the IBM BasicA and GW-Basic Interpreters?

The following program is an example of the problem:

10 FOR I%=240 TO 300 20 Z! = INT( 130 * I% ) 30 PRINT I%,Z! 40 NEXT The above program will give incorrect negative results in QuickBasic Version 1.x when I% exceeds 252 and Z! exceeds 32K (32,767).

In QuickBasic Versions 2.x and greater, you will get an "Overflow" error.

The same program will run in GW-Basic and IBM BasicA completely through to I%=300 and print the correct values for Z!.

Response:

The above program will work properly if you make the integer constant "130" into a single-precision constant ("130!" or "130.") or double-precision constant "130#" as shown in the following example:

20 Z! = INT( 130! * I% ) QuickBasic Version 1.x should not give negative results when an integer overflow occurs. This problem was corrected in Version 2.x and later, in which you properly get an "Overflow" message.

The QuickBasic compiler handles integer overflow differently than the GW-Basic and IBM BasicA Interpreters handle it.

When the compiler compiles a mathematical expression, it has to decide at compile time how to most efficiently optimize the expression into machine language. When the compiler sees the intermediate calculation 130*I% in the above program, it decides to restrict it to integer limits at compile time because 130*I% is the product of two integers. Changing the intermediate expression to the product of an integer and a higher-precision constant or variable will prevent the overflow problem.

The interpreters are able to dynamically make the decision at run time to convert 130*I% to a single-precision constant to avoid integer overflow. The disadvantage of dynamic handling is the slower speed of the interpreters when compared with the compiler.

Additional query words: QuickBas

Keywords: KB21844

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.