Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 35 additions & 16 deletions docs/visual-basic/language-reference/operators/mod-operator.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
---
title: "Mod Operator (Visual Basic)"
ms.date: 07/20/2015
ms.date: 04/24/2018
ms.prod: .net
ms.reviewer: ""
ms.suite: ""
ms.technology:
- "devlang-visual-basic"
ms.topic: "article"
Expand All @@ -18,11 +16,10 @@ helpviewer_keywords:
- "arithmetic operators [Visual Basic], Mod"
- "math operators [Visual Basic]"
ms.assetid: 6ff7e40e-cec8-4c77-bff6-8ddd2791c25b
caps.latest.revision: 22
author: dotnet-bot
ms.author: dotnetcontent
author: rpetrusha
ms.author: ronpet
---
# Mod Operator (Visual Basic)
# Mod operator (Visual Basic)
Divides two numbers and returns only the remainder.

## Syntax
Expand All @@ -38,12 +35,34 @@ number1 Mod number2
`number2`
Required. Any numeric expression.

## Supported Types
## 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.

## 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]<xref:System.Reflection.Emit.OpCodes.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:

```vb
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`.

Expand All @@ -55,18 +74,18 @@ number1 Mod number2

- The [/ Operator (Visual Basic)](../../../visual-basic/language-reference/operators/floating-point-division-operator.md) 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
## 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 <xref:System.DivideByZeroException> exception. A floating-point division returns <xref:System.Double.NaN>.

## Equivalent Formula
## 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 representation in memory. This could lead to unexpected results from certain operations, such as value comparison and the `Mod` operator. For more information, see [Troubleshooting Data Types](../../../visual-basic/programming-guide/language-features/data-types/troubleshooting-data-types.md).
## 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](../../../visual-basic/programming-guide/language-features/data-types/troubleshooting-data-types.md).

## 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](../../../visual-basic/programming-guide/language-features/procedures/operator-procedures.md).
Expand All @@ -81,7 +100,7 @@ number1 Mod number2

[!code-vb[VbVbalrOperators#32](../../../visual-basic/language-reference/operators/codesnippet/VisualBasic/mod-operator_2.vb)]

## See Also
## See also
<xref:Microsoft.VisualBasic.Conversion.Int%2A>
<xref:Microsoft.VisualBasic.Conversion.Fix%2A>
[Arithmetic Operators](../../../visual-basic/language-reference/operators/arithmetic-operators.md)
Expand Down