Microsoft KB Archive/31785

= &quot;Overflow&quot; Error in Intermediate Integer Calculation =

Article ID: 31785

Article Last Modified on 8/16/2005

-

APPLIES TO


 * Microsoft Visual Basic for MS-DOS
 * Microsoft QuickBasic 4.0
 * Microsoft QuickBASIC 4.0b
 * Microsoft QuickBasic 4.5 for MS-DOS
 * Microsoft BASIC Compiler 6.0
 * Microsoft BASIC Compiler 6.0b
 * Microsoft BASIC Professional Development System 7.0
 * Microsoft BASIC Professional Development System 7.1

-



This article was previously published under Q31785



SUMMARY
Microsoft Visual Basic for MS-DOS makes certain compile-time assumptions about the numerical types of intermediate results in mathematical expressions. These assumptions can sometimes lead to an &quot;overflow&quot; error at run time.



MORE INFORMATION
As an example of this behavior, if you run the following program in Visual Basic for MS-DOS, you might expect the output to be 36,000; however, an &quot;overflow&quot; error message is displayed instead:

x& = 6 * 6000 PRINT x&

Visual Basic for MS-DOS sees that the &quot;6&quot; and the 6000 are both short integers (that is, integers between -32,768 and +32,767), so it assumes that the intermediate multiplication result should also be a short integer. However, an &quot;Overflow&quot; error message results because 36,000 is not in the range of short integers.

To force the intermediate expression to be calculated as a long integer, make the constants into long integers in the first calculations performed in that expression. For example, the following two statements work correctly: x& = 1& * 6 * 6000  ' 1. & suffix makes a constant a long integer. x& = CLNG(6) * 6000 ' 2. CLNG forces type to be long integer. Using 1& in the multiplication of 1& * 6 creates an intermediate long integer variable that forces the subsequent multiplication (* 6000) to be done using a long integer.

Note that 6 * 6000 * 1& fails with an &quot;overflow&quot; error because the first multiplication is done as a short integer. The calculation 6 * (6000 * 1&) avoids this &quot;Overflow&quot; error by changing the order of calculation so that a long integer intermediate variable is created before multiplying by the &quot;6&quot;.

Integer Data Type Notation Standards for Basic
Note that the number 6 is the same as 6%, and 6000 is the same as 6000% in Basic's short integer notation. Appending the percent (%) symbol to a constant makes it explicitly a short integer. Appending the ampersand symbol (&) to a constant makes it explicitly a long integer. Constants whose types are not explicitly declared will default as shown in Chapter 2, &quot;Data Types,&quot; of the Basic language reference manual for QuickBasic for MS-DOS, versions 4.0 and 4.0b; and Microsoft Basic Compiler for MS-DOS and MS OS/2, version 6.0 and 6.0b. This information is also found in Appendix B of the &quot;Microsoft Basic 7.0: Programmer's Guide&quot; manual for Basic PDS for MS-DOS and MS OS/2, versions 7.0 and 7.1.

Additional query words: VBmsdos QuickBas BasicCom

Keywords: KB31785

-

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

© Microsoft Corporation. All rights reserved.