-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Added modulus/remainder note #5008
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| > [!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 is always in the range (`number2`, -`number2`), exclusive, and retains the sign of the dividend, (`number1`). For example: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we say this more simply as (assuming we use the terms dividend and divisor frequently in the docs).
The result of a Mod operation retains the sign of the dividend, (number1), so may be positive or negative. If the dividend is positive, the result is always between zero and the divisor (number 2). If the dividend is negative, the result is always between zero and the negative of the divisor (-number 2). For example:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The range (number2, -number2) reads unusual for negative numbers, e.g., (3, -3). I like the version suggested by @KathleenDollard but it's doesn't work when number2 is negative. What about the following edition (I might be wrong with the Abs syntax):
The result of a Mod operation retains the sign of the dividend, (number1), so may be positive or negative. If the dividend is positive, the result is always between zero and the absolute value of the divisor (Abs(number2)). If the dividend is negative, the result is always between the negative of the absolute value of the divisor (-Abs(number2)) and zero. For example:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OR:
The result of a Mod operation retains the sign of the dividend, number1, so may be positive or negative. The absolute value of the result is less than the absolute value of the divisor, Abs(number2)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another way is:
The result of a Mod operation retains the sign of the dividend, number1, so may be positive or negative. The result is always between 0 and number2.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider number1 = -8, number2 = 3. Then result is -2 and not between 0 and 3. I like that @rpetrusha shows all possible sign combinations in the example. Maybe it's better than all the words :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like your correction to mine, but agree that the point is to support the great examples. I wonder if calling out ignoring the divisor makes sense.
The result of a Mod operation retains the sign of the dividend, (number1), so may be positive or negative. The sign of the divisor is ignored and the absolute value is used, (Abs(number2)). If the dividend is positive, the result is always between zero and the absolute value of the divisor (Abs(number2)). If the dividend is negative, the result is always between the negative of the absolute value of the divisor (-Abs(number2)) and zero. For example:
I'm fine with any of them, but found I had to do mental gymnastics to figure out the range explanation, even if it is more concise. If we can be verbose here, we could include both as they will speak to different people.
| 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we format op_Modulus operator as code: op_Modulus?
| > [!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 is always in the range (`number2`, -`number2`), exclusive, and retains the sign of the dividend, (`number1`). For example: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The range (number2, -number2) reads unusual for negative numbers, e.g., (3, -3). I like the version suggested by @KathleenDollard but it's doesn't work when number2 is negative. What about the following edition (I might be wrong with the Abs syntax):
The result of a Mod operation retains the sign of the dividend, (number1), so may be positive or negative. If the dividend is positive, the result is always between zero and the absolute value of the divisor (Abs(number2)). If the dividend is negative, the result is always between the negative of the absolute value of the divisor (-Abs(number2)) and zero. For example:
| > [!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 is always in the range (`number2`, -`number2`), exclusive, and retains the sign of the dividend, (`number1`). For example: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OR:
The result of a Mod operation retains the sign of the dividend, number1, so may be positive or negative. The absolute value of the result is less than the absolute value of the divisor, Abs(number2)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This LGTM @rpetrusha You can merge once you update per other comments.
| > [!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 is always in the range (`number2`, -`number2`), exclusive, and retains the sign of the dividend, (`number1`). For example: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another way is:
The result of a Mod operation retains the sign of the dividend, number1, so may be positive or negative. The result is always between 0 and number2.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some minor problems, otherwise great!
| 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}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The expression is not the same as what's declared in the string part in the last line. $"-8 Mod -3 = {-8 Mod 3}"
|
|
||
| ## Floating-Point Imprecision | ||
| ## 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). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
they do not always have a precise representation in memory.
While this is pre-existing text... technically they do have a perfectly-precise-to-certain-significant-digits binary representation in memory. May be worth changing this text to "they do not always have a precise decimal representation in memory." Ex: 0.1 (decimal) is 0.0001100110011... in binary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, technically it is precise but not accurate
| End Sub | ||
| End Module | ||
| ' The example displays the following output: | ||
| ' 8 Mod 3 = 2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Code as the 8's all lining up.
Also, we don't actually output "the example..." and the spacing isn't exactly what is in the code sample regarding indenting. Not something I'd worry about as the point is clear.
| > [!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 is always in the range (`number2`, -`number2`), exclusive, and retains the sign of the dividend, (`number1`). For example: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like your correction to mine, but agree that the point is to support the great examples. I wonder if calling out ignoring the divisor makes sense.
The result of a Mod operation retains the sign of the dividend, (number1), so may be positive or negative. The sign of the divisor is ignored and the absolute value is used, (Abs(number2)). If the dividend is positive, the result is always between zero and the absolute value of the divisor (Abs(number2)). If the dividend is negative, the result is always between the negative of the absolute value of the divisor (-Abs(number2)) and zero. For example:
I'm fine with any of them, but found I had to do mental gymnastics to figure out the range explanation, even if it is more concise. If we can be verbose here, we could include both as they will speak to different people.
|
|
||
| ## Floating-Point Imprecision | ||
| ## 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). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, technically it is precise but not accurate
Added modulus/remainder note
The PR:
Fixes #4884
@BillWagner @KathleenDollard
//cc @aaronfranke @pkulikov