0

Just recently I stumbled upon a problem using the (what I assumed to be modulo) operator %:

enter image description here

Basically (-1) % 5 returns -1 instead of 4, so % seems to return the remainder instead of the mathematically correct modulus (either that or Wolfram Alpha is bad at math).

My question is quite simple: Is there a real modulo operator in C# that I don't know of? Or do I have to write my own code for that?

All results I can find in the internet only point to the % ...

Frederik Hoeft
  • 1,177
  • 1
  • 13
  • 37
  • Does this answer your question? [Modulo operation with negative numbers](https://stackoverflow.com/questions/11720656/modulo-operation-with-negative-numbers) – Strikegently Nov 11 '20 at 14:31
  • [_"The **remainder** operator `%` computes the remainder after dividing its left-hand operand by its right-hand operand"_](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/arithmetic-operators#remainder-operator-) -- (emphasis mine). – 41686d6564 stands w. Palestine Nov 11 '20 at 14:34
  • @Strikegently Thanks, I guess this work-around will work. So the answer is that there isn't an existing, build-in method for it? – Frederik Hoeft Nov 11 '20 at 14:35
  • @Strikegently that question is about C, not C#. See also [% (mod) explanation](https://stackoverflow.com/q/10065080/995714), [Why does the % operator sometimes output positive and sometimes negative?](https://stackoverflow.com/q/42661964/995714), [How does modulus differ in C# vs. Excel?](https://stackoverflow.com/q/35393745/995714) – phuclv Nov 11 '20 at 14:36

1 Answers1

2

There is no modulo operator in c#.

The % operator, is the remainder operator:https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/arithmetic-operators#remainder-operator-

The remainder operator % computes the remainder after dividing its left-hand operand by its right-hand operand.

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61