Skip to content

The % (Remainder) operator is named incorrectly in the docs #4827

@aaronfranke

Description

@aaronfranke

https://github.com/dotnet/docs/blob/master/docs/csharp/language-reference/operators/modulus-operator.md

https://github.com/dotnet/docs/blob/master/docs/csharp/language-reference/operators/index.md

This operator is named incorrectly in the documentation. It should be "Remainder" not "Modulus".

In C#, the functionality of the % operator is the Remainder function. The Remainder function returns a value that is the remainder after standard division. For example, 10 % 3 is 1, and this is true for both Remainder and Modulus.

The difference between these two is with negative numbers.

  • The standard integer division -10 / 3 is -3 so the Remainder can be found with -10 - (-3)*3 which is -1. Remainder of a and b returns a value on the range (-b, b) (both exclusive).

  • However, the Modulus function of -10 and 3 would return 2. Modulus satisfies a similar formula but with floored division. Floor division of -10 and 3 is -4 instead of -3, and -10 - (-4)*3 is 2. Modulus of a and b returns a value on the range [0, b) (0 inclusive, b exclusive).

  • C# does not have operators for Modulus or floored division, only Remainder and standard division. The docs should reflect this, so all documentation for % should be renamed to Remainder.

Here's Eric Lippert's thoughts on the issue: https://blogs.msdn.microsoft.com/ericlippert/2011/12/05/whats-the-difference-remainder-vs-modulus/

It's easy to see why this confusion exists, as Remainder and Modulus are equivalent functions for positive numbers. But they are not the same operations. Describing % as Modulus leads to confusion: https://stackoverflow.com/questions/10065080/mod-explanation

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions