Skip to content

Commit 4ee8fc3

Browse files
author
Ron Petrusha
authored
Added modulus/remainder note (#5008)
* Added modulus/remainder note * Addressed feedback * Addressed feedback
1 parent c4178da commit 4ee8fc3

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

docs/visual-basic/language-reference/operators/mod-operator.md

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
---
22
title: "Mod Operator (Visual Basic)"
3-
ms.date: 07/20/2015
3+
ms.date: 04/24/2018
44
ms.prod: .net
5-
ms.reviewer: ""
6-
ms.suite: ""
75
ms.technology:
86
- "devlang-visual-basic"
97
ms.topic: "article"
@@ -18,11 +16,10 @@ helpviewer_keywords:
1816
- "arithmetic operators [Visual Basic], Mod"
1917
- "math operators [Visual Basic]"
2018
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
2421
---
25-
# Mod Operator (Visual Basic)
22+
# Mod operator (Visual Basic)
2623
Divides two numbers and returns only the remainder.
2724

2825
## Syntax
@@ -38,12 +35,34 @@ number1 Mod number2
3835
`number2`
3936
Required. Any numeric expression.
4037

41-
## Supported Types
38+
## Supported types
4239
All numeric types. This includes the unsigned and floating-point types and `Decimal`.
4340

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+
Public Module Example
52+
Public Sub Main()
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+
End Sub
58+
End Module
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+
4766
## Remarks
4867
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`.
4968

@@ -55,18 +74,18 @@ number1 Mod number2
5574

5675
- 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.
5776

58-
## Attempted Division by Zero
77+
## Attempted division by zero
5978
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>.
6079

61-
## Equivalent Formula
80+
## Equivalent formula
6281
The expression `a Mod b` is equivalent to either of the following formulas:
6382

6483
`a - (b * (a \ b))`
6584

6685
`a - (b * Fix(a / b))`
6786

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).
7089

7190
## Overloading
7291
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).
@@ -81,7 +100,7 @@ number1 Mod number2
81100

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

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

0 commit comments

Comments
 (0)