C# 取模运算符的反面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11487731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The opposite of the modulo operator?
提问by IbrarMumtaz
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:
我记得在 java 中,模运算符可以被反转,这样你就可以反转它,而不是看到一个运算的余数是什么,所以它会告诉你一个数字被除以多次:
Console.WriteLine(1000 % 90);
Console.WriteLine(100 % 90);
Console.WriteLine(81 % 80);
Console.WriteLine(1 % 1);
Output:
输出:
- 10
- 10
- 1
- 0
- 10
- 10
- 1
- 0
Examples courtesy of DotNetPerls
示例由 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.
而不是看到余数,我想看看有多少次“80”进入“81”。应该是 1,余数是 1。
Does the c# modulo operator support this behaviour? If not, how might achieve the desired behaviour? With minimal code please ... :D
c# 模运算符是否支持这种行为?如果没有,如何实现所需的行为?请用最少的代码...: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?
我想答案很简单,比如将两个数字相除并去掉“-.#”值并保留整数“1.-”。我知道这一点,但必须有一种更巧妙的方法来做到这一点?
采纳答案by Avada Kedavra
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.Flooris a possibility. ie:
在您的评论中,您说您正在使用小数,那么Math.Floor是一种可能性。IE:
double d = Math.Floor(81.0 / 80.0); // 1.0000....
回答by ThiefMaster
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 1while double notOne = 81.0 / 80would give you 1.0125for example.
这给了你1whiledouble notOne = 81.0 / 80会给你1.0125例如。
回答by mattmc3
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.
所以我有点明白你的问题,即使其他人都在关注你的情况。为了平衡整数除法,您需要使用模运算符来处理余数:((1000 / 90) * 90) + (1000 % 90) == 1000。
回答by voluntier
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.
例如,假设您有n % d = r. 你知道 r 和 d,而 n 是未知的。模函数表示为n % d = n - d*INT(n/d)。所以r = n - d*INT(n/d)。我想不出如何直接解决这个问题,因为当您不知道 n 时,您需要知道 INT(n/d)。
回答by Mike Mounier
In OpenOffice CALC there is QUOTIENT, which returns the integer part of a division??
在 OpenOffice CALC 中有 QUOTIENT,它返回除法的整数部分??
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.
我一直认为 QUOTIENT 是 MOD 的对应物??—??也许这就是 OP 所谓的 MOD 的“对立面”。

