IbrarMumtaz
IbrarMumtaz

Reputation: 4393

The opposite of the modulo operator?

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:

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?

Upvotes: 20

Views: 67441

Answers (6)

Jochen
Jochen

Reputation: 420

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

Upvotes: 0

Mike Mounier
Mike Mounier

Reputation: 267

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.

Upvotes: 4

voluntier
voluntier

Reputation: 420

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.

Upvotes: 2

Avada Kedavra
Avada Kedavra

Reputation: 8691

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....

Upvotes: 11

mattmc3
mattmc3

Reputation: 18325

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.

Upvotes: 2

ThiefMaster
ThiefMaster

Reputation: 318508

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.

Upvotes: 24

Related Questions