Negative Math/Overflow in Compiled Program; OK in Interpreter (34290)



The information in this article applies to:

  • Microsoft QuickBASIC 1.0
  • Microsoft BASIC Compiler
  • Microsoft BASIC Interpreter for Apple Macintosh 2.0
  • Microsoft BASIC Interpreter for Apple Macintosh 2.1
  • Microsoft BASIC Interpreter for Apple Macintosh 3.0

This article was previously published under Q34290

SUMMARY

This information applies to Microsoft QuickBASIC Version 1.00, Microsoft BASIC Compiler Version 1.00, and Microsoft BASIC Interpreter Versions 2.00, 2.10, and 3.00 for the Apple Macintosh.

The compiler is more limited than the interpreter in handling numeric overflow and will, in certain circumstances, return negative or wrong values when intermediate calculations overflow. The compiler must decide at compile time which precision is appropriate for a given operation. The precision cannot be changed if it proves inadequate at run time. This difference is documented on Page 90 of the "Microsoft BASIC Compiler for Apple Macintosh" manual and Page 70 of the "Microsoft QuickBASIC for Apple Macintosh: User's Guide."

MORE INFORMATION

The following example demonstrates the difference:
   M% = 30000 : N% = 30000
   X! = M% + N%
   PRINT X!
				
Within the interpreter, this code produces the value 60000 for X!. The integer sum of the integer values M% and N% is automatically converted to the floating-point value X!.

The compiler is not able to make the same conversion when it turns your program into an application. Since the expression in the last statement contains only integer variables, the compiler generates object code to perform integer addition. However, the result is too large (greater than 32,767) to be handled as an integer, so an incorrect value (-5536) is printed.

Modification Type:MinorLast Reviewed:8/16/2005
Keywords:KB34290