Microsoft KB Archive/205053

= PRB: "Overflow" with Integer Division and MOD Operator =

Article ID: 205053

Article Last Modified on 6/28/2004

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 4.0 Standard Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 16-bit Enterprise Edition
 * Microsoft Visual Basic 4.0 32-Bit Enterprise Edition

-



This article was previously published under Q205053



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.

Keywords: kberrmsg kbprb KB205053

-

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

© Microsoft Corporation. All rights reserved.