PRB: "Overflow" with Integer Division and MOD Operator

ID: Q205053


The information in this article applies to:
  • Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, versions 5.0, 6.0
  • Microsoft Visual Basic Standard, Professional, and Enterprise Editions, 16-bit and 32-bit, for Windows, version 4.0


SYMPTOMS

When using a number larger than 2,147,483,647 (or smaller than -2,147,483,648) with the Mod operator or the integer division operator (\), you receive the following error message:

Run Time Error '6':
Overflow


CAUSE

The Visual Basic Help topic for the Mod operator and the integer division operator (\) explains that if floating point numbers are used in the expression, they are converted to Longs first. Thus, if the floating point number is greater than the maximum value of a Long (2,147,483,647), or less than the minimum value for a long (-2,147,483,648), an overflow error will occur.


RESOLUTION

The following code demonstrates how to perform integer division and modulo arithmetic when the size of an operand is sufficiently large to cause overflow:


Dim dblX as Double
Dim dblY as Double
dblX = 2147483648                ' numerator
dblY = 123                       ' denominator

' round off the numerator and denominator (ensure number is .0)
dblX = INT(dblX + .5)         
dblY = INT(dblY + .5)      

' Emulate integer division
MsgBox FIX(dblX / dblY)             
' Emulate modulo arithmetic
MsgBox dblX - ( dblY * FIX(dblX / dblY) ) 


STATUS

This behavior is by design.

Additional query words:

Keywords : kberrmsg kbVBp kbVBp400 kbVBp500 kbVBp600 kbGrpVB
Version : WINDOWS:4.0,5.0,6.0
Platform : WINDOWS
Issue type : kbprb


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