"Overflow" Error in Intermediate Integer Calculation

ID: Q31785


The information in this article applies to:
  • Microsoft Visual Basic Standard and Professional Editions for MS-DOS, version 1.0
  • Microsoft QuickBASIC for MS-DOS, versions 4.0, 4.0b, 4.5
  • Microsoft BASIC Compiler for MS-DOS and MS OS/2, versions 6.0, 6.0b
  • Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2, versions 7.0, 7.1


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 "overflow" 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 "overflow" error message is displayed instead:

x& = 6 * 6000 PRINT x&
Visual Basic for MS-DOS sees that the "6" 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 "Overflow" 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 "overflow" error because the first multiplication is done as a short integer. The calculation 6 * (6000 * 1&) avoids this "Overflow" error by changing the order of calculation so that a long integer intermediate variable is created before multiplying by the "6".

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, "Data Types," 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 "Microsoft Basic 7.0: Programmer's Guide" manual for Basic PDS for MS-DOS and MS OS/2, versions 7.0 and 7.1.

Additional query words: VBmsdos QuickBas BasicCom

Keywords :
Version : MS-DOS:1.0,4.0,4.0b,4.5; :6.0,6.0b,7.0,7.1
Platform : MS-DOS
Issue type :


Last Reviewed: December 8, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.