The information in this article applies to:
SUMMARYMicrosoft 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 INFORMATIONAs 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: 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:
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 BasicNote 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 : |
Last Reviewed: December 8, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |