﻿ ﻿
﻿

"Mod" - Arithmetic Modulus Operation

This section provides some detail rules and a tutorial example on how arithmetic modulus operation works in VBScript.

﻿

Here are some detail rules about the arithmetic operation - modulus:

• Arithmetic modulus operation uses the modulus operator: "Mod"
• Arithmetic modulus operation requires that at least one of the operands is a numeric subtype: Byte, Integer, Long, Single or Double.
• If an operand is not a numeric subtype, it will be converted into a numeric subtype.
• If an operand is not an integer, it will be rounded into an integer. If the rounding process fails, the whole operation will fail.
• The Boolean subtype is considered as a numeric subtype in a modulus operation. "True" represents a numeric value of -1. "False" represents a numeric value of 0.
• The Empty subtype is considered as a numeric subtype in a modulus operation. "Empty" represents a numeric value of 0.
• When a String value is used in a modulus operation, it will be parsed into a Long value. If the parsing process fails, the modulus operation will raise the "Type mismatch" runtime error.
• The operation will fail, if the second operand is a 0. Dividing by 0 is mathematically impossible.
• The returning value is the remainder of the first operand divided by the second operand.
• The subtype of the returning value is Integer or Long determined by the returning value range and subtypes of operands.

Here is a tutorial example showing you how arithmetic modulus operation rules work:

```<html>
<body>
<!-- modulus_division_operation.html
-->
<pre>
<script language="vbscript">
document.writeln("21 Mod 7=" & (21 Mod 7) & " : " _
& TypeName(21 Mod 7))
document.writeln("20.6 Mod 7=" & (20.6 Mod 7) & " : " _
& TypeName(20.6 Mod 7))
document.writeln("24 Mod 7=" & (24 Mod 7) & " : " _
& TypeName(24 Mod 7))
document.writeln("24 Mod 6.6=" & (24 Mod 6.6) & " : " _
& TypeName(24 Mod 6.6))
document.writeln("24 Mod 6.4=" & (24 Mod 6.4) & " : " _
& TypeName(24 Mod 6.4))
document.writeln("3 Mod 777777=" & (3 Mod 777777) & " : " _
& TypeName(3 Mod 777777))
document.writeln("3 Mod 9.99=" & (3 Mod 9.99) & " : " _
& TypeName(3 Mod 9.99))
document.writeln("3.33 Mod 9.99=" & (3.33 Mod 9.99) & " : " _
& TypeName(3.33 Mod 9.99))
'   document.writeln("3 Mod 333.333e200=" & (3 Mod 333.333e200) _
'      & " : " & TypeName(3 Mod 333.333e200))
document.writeln("3 Mod ""7""=" & (3 Mod "7") & " : " _
& TypeName(3 Mod "7"))
'  document.writeln("3 Mod ""7Hello""=" & (3 Mod "7Hello") & " : " _
'     & TypeName(3 Mod "7Hello"))
document.writeln("3 Mod True=" & (3 Mod True) & " : " _
& TypeName(3 Mod True))
'   document.writeln("3 Mod False=" & (3 Mod False) & " : " _
'      & TypeName(3 Mod False))
document.writeln("False Mod True=" & (False Mod True) & " : " _
& TypeName(False Mod True))
document.writeln("Empty Mod 3=" & (Empty Mod 3) & " : " _
& TypeName(Empty Mod 3))
</script>
</pre>
</body>
</html>
```

Here is the output of this VBScript example script:

```21 Mod 7=0 : Integer
20.6 Mod 7=0 : Long
24 Mod 7=3 : Integer
24 Mod 6.6=3 : Long
24 Mod 6.4=0 : Long
3 Mod 777777=3 : Long
3 Mod 9.99=3 : Long
3.33 Mod 9.99=3 : Long
3 Mod "7"=3 : Long
3 Mod True=0 : Integer
False Mod True=0 : Integer
Empty Mod 3=0 : Integer
```

The output confirms some of arithmetic modulus operation rules:

• Return values from all tests are Integer or Double values .
• "20.6 Mod 7=0 : Long" shows that the first operand is rounded up from 20.6 to 21.
• "24 Mod 6.6=3 : Long" shows that the second operand is rounded up from 6.6 to 7.
• "24 Mod 6.4=0 : Long" shows that the first operand is rounded down from 6.4 to 6.
• The statement on (3 Mod 333.333e200) is commented out, because the second operand could not be rounded to an integer - out of the Double value range.
• The statement on (3 Mod False) is commented out, because dividing by 0 is not allowed.

﻿

﻿ ﻿