The opposite of the operator module?

I remember in java that the modulo operator can be inverted, so instead of seeing what remains of the operation, you can invert it, so instead it will tell you many times when the number is divided by:

Console.WriteLine(1000 % 90); Console.WriteLine(100 % 90); Console.WriteLine(81 % 80); Console.WriteLine(1 % 1); 

Conclusion:

  • 10
  • 10
  • 1
  • 0

Examples are courtesy of DotNetPerls.

Instead of seeing the remainder, I want to see how many times β€œ80” entered β€œ81”. Which should be 1 with a remainder of 1.

Does the C # modulo operator support this behavior? If not, how can you achieve the desired behavior? With a minimum code, please ...: D

EDIT:

I assume that the answer will be something simple, like separating two numbers and getting rid of the value β€œ-. #” And saving the integer β€œ1.-". I know about this, but should there be an easier way to do this?

+6
source share
4 answers

You already have the answer, there is no need to deal with decimals if you assign it to an integer.

In your comment, you say that you work with decimals, then Math.Floor . those.:

 double d = Math.Floor(81.0 / 80.0); // 1.0000.... 
+5
source

What you are looking for is called whole division. It is not associated with the modulo operator at all.

To perform integer division, just make sure that none of the operands is float / double.

Example:

 int one = 81 / 80; 

This gives you 1 , and double notOne = 81.0 / 80 will give you 1.0125 , for example.

+12
source
 Console.WriteLine(1000 % 90); // modulo = 10 Console.WriteLine(1000 / 90); // integer division = 11 Console.WriteLine(1000 / 90.0); // floating point division = 11.1111111111111 

So, I get your question, even if everyone else is in your business about it. To balance integer division, you need to have a modulo operator to handle the remainder: ((1000 / 90) * 90) + (1000 % 90) == 1000 .

+1
source

Fortunately, you ask another question that would be modulo inverse. The inverse of the module could theoretically give you an unknown numerator or divisor.

For example, let's say you have n % d = r . You know r and d, but n is unknown. Modulo function is expressed as n % d = n - d*INT(n/d) . So r = n - d*INT(n/d) . I cannot think of how this can be solved directly, since you need to know INT (n / d) when you do not know n.

0
source

Source: https://habr.com/ru/post/920507/


All Articles