แก้ไข

แชร์ผ่าน


Mod operator (Visual Basic)

Divides two numbers and returns only the remainder.

Syntax

result = number1 Mod number2

Parts

result
Required. Any numeric variable or property.

number1
Required. Any numeric expression.

number2
Required. Any numeric expression.

Supported types

All numeric types. This includes the unsigned and floating-point types and Decimal.

Result

The result is the remainder after number1 is divided by number2. For example, the expression 14 Mod 4 evaluates to 2.

Note

There is a difference between remainder and modulus in mathematics, with different results for negative numbers. The Mod operator in Visual Basic, the .NET Framework op_Modulus operator, and the underlying rem IL instruction all perform a remainder operation.

The result of a Mod operation retains the sign of the dividend, number1, and so it may be positive or negative. The result is always in the range (-number2, number2), exclusive. For example:

Public Module Example
   Public Sub Main()
      Console.WriteLine($" 8 Mod  3 = {8 Mod 3}")
      Console.WriteLine($"-8 Mod  3 = {-8 Mod 3}")
      Console.WriteLine($" 8 Mod -3 = {8 Mod -3}")
      Console.WriteLine($"-8 Mod -3 = {-8 Mod -3}")
   End Sub
End Module
' The example displays the following output:
'       8 Mod  3 = 2
'      -8 Mod  3 = -2
'       8 Mod -3 = 2
'      -8 Mod -3 = -2

Remarks

If either number1 or number2 is a floating-point value, the floating-point remainder of the division is returned. The data type of the result is the smallest data type that can hold all possible values that result from division with the data types of number1 and number2.

If number1 or number2 evaluates to Nothing, it is treated as zero.

Related operators include the following:

  • The \ Operator (Visual Basic) returns the integer quotient of a division. For example, the expression 14 \ 4 evaluates to 3.

  • The / Operator (Visual Basic) returns the full quotient, including the remainder, as a floating-point number. For example, the expression 14 / 4 evaluates to 3.5.

Attempted division by zero

If number2 evaluates to zero, the behavior of the Mod operator depends on the data type of the operands:

  • An integral division throws a DivideByZeroException exception if number2 cannot be determined in compile-time and generates a compile-time error BC30542 Division by zero occurred while evaluating this expression if number2 is evaluated to zero at compile-time.
  • A floating-point division returns Double.NaN.

Equivalent formula

The expression a Mod b is equivalent to either of the following formulas:

a - (b * (a \ b))

a - (b * Fix(a / b))

Floating-point imprecision

When you work with floating-point numbers, remember that they do not always have a precise decimal representation in memory. This can lead to unexpected results from certain operations, such as value comparison and the Mod operator. For more information, see Troubleshooting Data Types.

Overloading

The Mod operator can be overloaded, which means that a class or structure can redefine its behavior. If your code applies Mod to an instance of a class or structure that includes such an overload, be sure you understand its redefined behavior. For more information, see Operator Procedures.

Example 1

The following example uses the Mod operator to divide two numbers and return only the remainder. If either number is a floating-point number, the result is a floating-point number that represents the remainder.

Debug.WriteLine(10 Mod 5)
' Output: 0
Debug.WriteLine(10 Mod 3)
' Output: 1
Debug.WriteLine(-10 Mod 3)
' Output: -1
Debug.WriteLine(12 Mod 4.3)
' Output: 3.4
Debug.WriteLine(12.6 Mod 5)
' Output: 2.6
Debug.WriteLine(47.9 Mod 9.35)
' Output: 1.15

Example 2

The following example demonstrates the potential imprecision of floating-point operands. In the first statement, the operands are Double, and 0.2 is an infinitely repeating binary fraction with a stored value of 0.20000000000000001. In the second statement, the literal type character D forces both operands to Decimal, and 0.2 has a precise representation.

firstResult = 2.0 Mod 0.2
' Double operation returns 0.2, not 0.
secondResult = 2D Mod 0.2D
' Decimal operation returns 0.

See also