Fileaze banner

 

VBScript Scripting Techniques > Data > Integers

Integers

& Integer Divides

There are 3 distinct methods for Integer Divides in VBScript: \ y, Int( x / y ) and CInt( x / y )
The best way to explain the differences between the 3 available Integer Divide methods may be by showing some examples.
The following table lists the outcomes of the 3 methods for a range of values:

Demonstration of 3 integer divide methods
Abs(x\y) <=
Abs(x/y)
  Int(x/y) <= x/y   CInt(x/y) = x/y rounded
to nearest integer
-7 \ 3 = -2          Int( -7 / 3 ) = -3          CInt( -7 / 3 ) = -2
-6 \ 3 = -2          Int( -6 / 3 ) = -2          CInt( -6 / 3 ) = -2
-5 \ 3 = -1          Int( -5 / 3 ) = -2          CInt( -5 / 3 ) = -2
-4 \ 3 = -1          Int( -4 / 3 ) = -2          CInt( -4 / 3 ) = -1
-3 \ 3 = -1          Int( -3 / 3 ) = -1          CInt( -3 / 3 ) = -1
-2 \ 3 =  0          Int( -2 / 3 ) = -1          CInt( -2 / 3 ) = -1
-1 \ 3 =  0          Int( -1 / 3 ) = -1          CInt( -1 / 3 ) =  0
 0 \ 3 =  0          Int(  0 / 3 ) =  0          CInt(  0 / 3 ) =  0
 1 \ 3 =  0          Int(  1 / 3 ) =  0          CInt(  1 / 3 ) =  0
 2 \ 3 =  0          Int(  2 / 3 ) =  0          CInt(  2 / 3 ) =  1
 3 \ 3 =  1          Int(  3 / 3 ) =  1          CInt(  3 / 3 ) =  1
 4 \ 3 =  1          Int(  4 / 3 ) =  1          CInt(  4 / 3 ) =  1
 5 \ 3 =  1          Int(  5 / 3 ) =  1          CInt(  5 / 3 ) =  2
 6 \ 3 =  2          Int(  6 / 3 ) =  2          CInt(  6 / 3 ) =  2
 7 \ 3 =  2          Int(  7 / 3 ) =  2          CInt(  7 / 3 ) =  2

 

VBScript code to generate the table above:

strHTML = "<TABLE cellspacing=""15"" STYLE=""border-style: solid; " _
        & "border-width: 3px; border-color: gray;"">" & vbCrLf _
        & "<THEAD>" & vbCrLf _
        & "<TR>" & vbCrLf _
        & "    <TH COLSPAN=""5"">Demonstration of 3 integer " _
        & "divide methods</TH>" & vbCrLf _
        & "</TR>" & vbCrLf _
        & "<TR>" & vbCrLf _
        & "    <TH>Abs(x\y) &lt;=<BR>Abs(x/y)</TH>" & vbCrLf _
        & "    <TH>&nbsp;</TH>" & vbCrLf _
        & "    <TH>Int(x/y) &lt;= x/y</TH>" & vbCrLf _
        & "    <TH>&nbsp;</TH>" & vbCrLf _
        & "    <TH>CInt(x/y) = x/y rounded<BR>" _
        & "to <EM>nearest</EM> integer</TH>" & vbCrLf _
        & "</TR>" & vbCrLf _
        & "</THEAD>" & vbCrLf _
        & "<TBODY>" & vbCrLf

strText = "Demonstration of 3 integer divide methods:" & vbCrLf _
        & "==========================================" & vbCrLf & vbCrLf
       
For i = -7 To 7
    strText = strText & LeftPad( RightPad( i, 2, " " ) & " \ 3 = " _
                      & RightPad( ( i \ 3 ), 2, " " ), 18, " " ) _
                      & LeftPad( "Int( " & RightPad( i, 2, " " ) & " / 3 ) = " _
                      & RightPad( Int( i / 3 ), 2, " " ), 25, " " ) _
                      & "CInt( " & RightPad( i, 2, " " ) & " / 3 ) = " _
                      & RightPad( CInt( i / 3 ), 2, " " ) & vbCrLf

    strHTML = strHTML & "<TR>" & vbCrLf _
                      & "    <TD align=""right""><FONT FACE=""Courier"">" _
                      & RightPad( i, 2, "@" ) & " \ 3 = " _
                      & RightPad( ( i \ 3 ), 2, "@" ) _
                      & "</FONT></TD>" & vbCrLf _
                      & "    <TD>&nbsp;&nbsp;&nbsp;&nbsp;" _
                      & "&nbsp;&nbsp;&nbsp;&nbsp;</TD>" & vbCrLf _
                      & "    <TD align=""right""><FONT FACE=""Courier"">Int( " _
                      & RightPad( i, 2, "@" ) & " / 3 ) = " _
                      & RightPad( Int( i / 3 ), 2, "@" ) _
                      & "</FONT></TD>" & vbCrLf _
                      & "    <TD>&nbsp;&nbsp;&nbsp;&nbsp;" _
                      & "&nbsp;&nbsp;&nbsp;&nbsp;</TD>" & vbCrLf _
                      & "    <TD align=""right""><FONT FACE=""Courier"">CInt( " _
                      & RightPad( i, 2, "@" ) & " / 3 ) = " _
                      & RightPad( CInt( i / 3 ), 2, "@" ) _
                      & "</FONT></TD>" & vbCrLf _
                      & "</TR>" & vbCrLf
Next

strHTML = Replace( strHTML, "@", "&nbsp;" ) & "</TBODY>" & vbCrLf _
        & "</TABLE>" & vbCrLf

' Uncomment the line you want to display
' WScript.Echo strHTML
WScript.Echo strText

Function LeftPad( strText, intLen, chrPad )
    'LeftPad( "1234", 7, "x" ) = "1234xxx"
    'LeftPad( "1234", 3, "x" ) = "123"
    LeftPad = Left( strText & String( intLen, chrPad ), intLen )
End Function

Function RightPad( strText, intLen, chrPad )
    'RightPad( "1234", 7, "x" ) = "xxx1234"
    'RightPad( "1234", 3, "x" ) = "234"
    RightPad = Right( String( intLen, chrPad ) & strText, intLen )
End Function

 

 

 


page last uploaded: 4 March 2011, 12:54