17

I remember in java that, the modulo operator could be inverted so that rather than seeing what the remainder is of an operation, you could invert it, so instead it will tell you many times a number was divided by:

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

Output:

  • 10
  • 10
  • 1
  • 0

Examples courtesy of DotNetPerls

Rather than seeing the remainder, I want to see how many times '80' went into '81'. Which should be 1 with a remainder of 1.

Does the c# modulo operator support this behaviour? If not, how might achieve the desired behaviour? With minimal code please ... :D

EDIT:

I imagine the answer is going to be something simple like dividing the two numbers and getting rid of the '-.#' value and keeping the integer '1.-'. I know about this but there must be a slicker way of doing this?

IbrarMumtaz
  • 4,235
  • 7
  • 44
  • 63

6 Answers6

23

What you are looking for is called integer division. It is not related to the modulo operator at all.

To perform an integer division, simply ensure that neither operand is a float/double.

Example:

int one = 81 / 80;

This gives you 1 while double notOne = 81.0 / 80 would give you 1.0125 for example.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • TY - I have been doing C# for a few years now so something as trivial as this never really came up until now. IN java I was taught about this, all using the modulo operator. In C# I don't remember seeing anything like this. Hence the question. – IbrarMumtaz Jul 14 '12 at 22:17
10

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

In your comment you say that you are working with decimals, then Math.Floor is a possibility. ie:

double d = Math.Floor(81.0 / 80.0); // 1.0000....
Avada Kedavra
  • 8,523
  • 5
  • 32
  • 48
  • thanks for this ... (95.45 / 50) <-- just need to know how many times 50 does into 95.45. Let me check out Math.Floor out first - = ) – IbrarMumtaz Jul 14 '12 at 23:04
  • 1
    `95.45/50=1.909`, Math.Floor will round this down to 1.000. `195.45/ 50=3.909`, Math.Floor will return 3.000. etc. – Avada Kedavra Jul 14 '12 at 23:07
  • 1
    The opposite of Math.Floor is Math.Ceiling which will instead round up to the nearest number. – Avada Kedavra Jul 14 '12 at 23:09
  • WARNING note for .NET: System.Math.Floor(2,3) returns 0 (correct) while System.Math.Floor(-2,3) returns -1 (incorrect). This solution in working for positive results, only, and therefore NOT recommended. – Jochen Apr 28 '23 at 13:10
4

In OpenOffice CALC there is QUOTIENT, which returns the integer part of a division ᎓

      MOD(17;4) returns 1, while
 QUOTIENT(17;4) returns 4.

      MOD(33;9) returns 6, while
 QUOTIENT(33;9) returns 3.

I've always thought of QUOTIENT as MOD's counterpart  —  perhaps this is what the OP meant by "opposite" of MOD.

Mike Mounier
  • 257
  • 2
  • 4
  • This is exactly what I was trying to remember. The use of say, int x = 22/12 returns 1, because of the truncation in c makes the int the largest full number and discards the floating-point. If you need the remainder and the total groups you could use this in conjunction with mod – Steve Byrne Jun 30 '20 at 20:11
2
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 kinda get your question even though everyone else is on your case about it. In order to balance integer division you need to have the modulo operator in order to handle the remainder: ((1000 / 90) * 90) + (1000 % 90) == 1000.

mattmc3
  • 17,595
  • 7
  • 83
  • 103
  • Thanks it is a weird one I have to admit, it's just the ex java guy in me that bubbled up the surface and refused to be contained any longer - lol – IbrarMumtaz Jul 14 '12 at 22:23
2

Luckily you're asking a different question from what an inverse of a modulo would be. An inverse of a modulo would theoretically be able to give you an unknown numerator or divisor.

For instance, say you have n % d = r. You know r and d, and n is unknown. The modulo function is expressed as n % d = n - d*INT(n/d). So r = n - d*INT(n/d). I can't think of how this can be directly solved, since you would need to know INT(n/d) when you don't know n.

voluntier
  • 330
  • 4
  • 11
0

For .NET, you might want to use System.Math.DivRem: Calculates the quotient of two numbers and also returns the remainder in an output parameter.

quotient = System.Math.DivRem (x, y, bufferForRemainder);

also see https://learn.microsoft.com/en-US/dotnet/api/system.math.divrem?view=net-7.0

Jochen
  • 380
  • 1
  • 3
  • 9