You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/visual-basic/language-reference/operators/mod-operator.md
+35-16Lines changed: 35 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,7 @@
1
1
---
2
2
title: "Mod Operator (Visual Basic)"
3
-
ms.date: 07/20/2015
3
+
ms.date: 04/24/2018
4
4
ms.prod: .net
5
-
ms.reviewer: ""
6
-
ms.suite: ""
7
5
ms.technology:
8
6
- "devlang-visual-basic"
9
7
ms.topic: "article"
@@ -18,11 +16,10 @@ helpviewer_keywords:
18
16
- "arithmetic operators [Visual Basic], Mod"
19
17
- "math operators [Visual Basic]"
20
18
ms.assetid: 6ff7e40e-cec8-4c77-bff6-8ddd2791c25b
21
-
caps.latest.revision: 22
22
-
author: dotnet-bot
23
-
ms.author: dotnetcontent
19
+
author: rpetrusha
20
+
ms.author: ronpet
24
21
---
25
-
# Mod Operator (Visual Basic)
22
+
# Mod operator (Visual Basic)
26
23
Divides two numbers and returns only the remainder.
27
24
28
25
## Syntax
@@ -38,12 +35,34 @@ number1 Mod number2
38
35
`number2`
39
36
Required. Any numeric expression.
40
37
41
-
## Supported Types
38
+
## Supported types
42
39
All numeric types. This includes the unsigned and floating-point types and `Decimal`.
43
40
44
-
## Result
45
-
The result is the remainder after `number1` is divided by `number2`. For example, the expression `14 Mod 4` evaluates to 2.
46
-
41
+
## Result
42
+
43
+
The result is the remainder after `number1` is divided by `number2`. For example, the expression `14 Mod 4` evaluates to 2.
44
+
45
+
> [!NOTE]
46
+
> 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.
47
+
48
+
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:
49
+
50
+
```vb
51
+
PublicModuleExample
52
+
PublicSubMain()
53
+
Console.WriteLine($" 8 Mod 3 = {8 Mod 3}")
54
+
Console.WriteLine($"-8 Mod 3 = {-8 Mod 3}")
55
+
Console.WriteLine($" 8 Mod -3 = {8 Mod -3}")
56
+
Console.WriteLine($"-8 Mod -3 = {-8 Mod -3}")
57
+
EndSub
58
+
EndModule
59
+
' The example displays the following output:
60
+
' 8 Mod 3 = 2
61
+
' -8 Mod 3 = -2
62
+
' 8 Mod -3 = 2
63
+
' -8 Mod -3 = -2
64
+
```
65
+
47
66
## Remarks
48
67
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`.
49
68
@@ -55,18 +74,18 @@ number1 Mod number2
55
74
56
75
- 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.
57
76
58
-
## Attempted Division by Zero
77
+
## Attempted division by zero
59
78
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>.
60
79
61
-
## Equivalent Formula
80
+
## Equivalent formula
62
81
The expression `a Mod b` is equivalent to either of the following formulas:
63
82
64
83
`a - (b * (a \ b))`
65
84
66
85
`a - (b * Fix(a / b))`
67
86
68
-
## Floating-Point Imprecision
69
-
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).
87
+
## Floating-point imprecision
88
+
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).
70
89
71
90
## Overloading
72
91
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).
0 commit comments