Skip to content

Conversation

@rpetrusha
Copy link
Contributor

Added modulus/remainder note

The PR:

  • Notes that Mod performs a remainder operation.
  • Changes headers to sentence case.
  • Makes some formatting fixes.

Fixes #4884

@BillWagner @KathleenDollard

//cc @aaronfranke @pkulikov

@rpetrusha rpetrusha requested a review from BillWagner April 24, 2018 00:35
> [!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:
Copy link
Contributor

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:

Copy link
Contributor

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:

Copy link
Contributor

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)

Copy link
Member

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.

Copy link
Contributor

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 :)

Copy link
Contributor

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.
Copy link
Contributor

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:
Copy link
Contributor

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:
Copy link
Contributor

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)

Copy link
Member

@BillWagner BillWagner left a 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:
Copy link
Member

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.

Copy link
Contributor

@aaronfranke aaronfranke left a 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}")
Copy link
Contributor

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).
Copy link
Contributor

@aaronfranke aaronfranke Apr 27, 2018

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.

Copy link
Contributor

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

@rpetrusha rpetrusha merged commit 4ee8fc3 into dotnet:master Apr 27, 2018
@rpetrusha rpetrusha deleted the vb-modulus branch April 27, 2018 19:53
End Sub
End Module
' The example displays the following output:
' 8 Mod 3 = 2
Copy link
Contributor

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:
Copy link
Contributor

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).
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

VB: Modulus operator should be referred to as Remainder

5 participants