Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c中的数学模#_C#_Modulo - Fatal编程技术网

C# c中的数学模#

C# c中的数学模#,c#,modulo,C#,Modulo,在c#中是否有一个函数库函数,用于一个数字的数学模——我的意思是,负整数模正整数应该产生一个正结果 编辑以提供示例: -5模3应返回1可能是%运算符 试试(a%b)*数学符号(a) 试试这个;它工作正常 static int MathMod(int a, int b) { return (Math.Abs(a * b) + a) % b; } x

在c#中是否有一个函数库函数,用于一个数字的数学模——我的意思是,负整数模正整数应该产生一个正结果

编辑以提供示例:


-5模3应返回1

可能是%运算符

试试
(a%b)*数学符号(a)

试试这个;它工作正常

static int MathMod(int a, int b) {
    return (Math.Abs(a * b) + a) % b;
}
x<0?((x%m)+m)%m:x%m;
这个定义(如果我没弄错的话)是这样的

a模块b=a-b*楼层(a/b)

它可能非常慢,而且要注意整数除法,就像内置的模:)

另一个选项是根据操作数的符号修改内置模的结果。大概是这样的:

if(a < 0 && b > 0)
{
    return (a % b + b) % b;
}
else if ....
if(a<0&&b>0)
{
返回(a%b+b)%b;
}
否则,如果。。。。
修复:


(ans=a%b)如果您正在使用这些算法中的任何一种,并且还需要进行除法,请不要忘记确保在适当的时候减去1

如果
-5%2=-1
-5/2=-2
,并且如果你关心
-5/2*2+-5%2=-5
,那么当你计算
-5%2=1
时,你也会计算
-5/2=-3
a<0?((a+1)%b+b-1):(a%b);
a < 0 ? ((a+1)%b + b-1) : (a%b);

这只需要一个%运算(
和一个三元op
)并且不需要乘法

我知道这个问题没有要求它,但我只是编写并测试了一个方法,该方法同时返回商。我找的时候没有找到这个,所以我想我应该把它放在那里

/// <summary>
/// Compute integer quotient and remainder of <paramref name="dividend"/> / <paramref name="divisor"/>
/// where the <paramref name="remainder"/> has the same sign as <paramref name="divisor"/>, and is
/// between zero (inclusive) and the <paramref name="divisor"/> (exclusive). As always,
/// (quotientResult * <paramref name="divisor"/> + <paramref name="remainder"/> == <paramref name="dividend"/>).
/// </summary>
public static int DivRemPeriodic(int dividend, int divisor, out int remainder) {
    var quotient = Math.DivRem(dividend, divisor, out remainder);
    if (divisor > 0 ? remainder < 0 : remainder > 0) {
        remainder += divisor;
        quotient -= 1;
    }
    return quotient;
}
//
///计算整数商和/
///其中的符号与,和相同
///介于零(包括)和(排除)之间。一如既往,
///(商结果*+==)。
/// 
公共静态整数除数周期(整数除数、整数除数、整数余数){
var商=数学除数(股息、除数、余数);
if(除数>0?余数<0:余数>0){
余数+=除数;
商-=1;
}
返回商;
}


No这不会在执行-5%2时产生积极的结果,他希望
-5%2
为+1,而不是-1.+1,因为他是第一个发布正确算法的人(但要注意溢出!)。严格地回答他的问题,虽然,没有,这个框架中没有内置任何内容。被认为是第一个正确的算法,但我认为我更可能使用((a%b)+b)%b数学。Abs在这个实现中非常慢,((a%b)+b)%b是我使用c#.net 4的基准测试中速度最快的。用这个方法警告溢出!!我唯一想改变的是变量的名称分别是被除数和除数,类似于为什么要使用三元运算符?@penguat:因为如果x是非负的,那么就没有理由进行额外的工作。我不确定平均来说哪个是更多的工作,是有条件的还是固定的加法和模,这对我来说并不重要。无论如何,这个答案是正确的。谢谢。@penguat:很难说;我总是认为除法和模运算是比较昂贵的(除非它们是微不足道的,如除以2或mod 2)。我想三元运算符的开销只是检查和跳转的几个周期。另外,在这个计算中,非负x不可能溢出。但是,如果没有三元运算符,如果
x=1
m=int.MaxValue
(或者在0int.MaxValue的任何情况下),您可能会溢出。我肯定会猜测,额外的模比现代处理器上的分支更快。然而,我从未测试过它,所以我真的不知道。而且,这是一个相当细微的区别。我不确定哪一个更可读。一开始我想说没有三元运算符,但是当我想到x<0时,它确实告诉读者为什么我们做一些奇特的事情而不需要评论。那么-10 mod 3呢?你的解决方案将给出-1,其中要求的答案是2。@penguat a=-10,b=3 ans=-1<0,所以解决方案=(-1+3)%3=2怎么了?我不太确定你在这里得到的是什么。。。我通常会保留原始数字,而不是试图重建它。我不是建议你重建它。我只是担心你的算法可能也会使用/运算符,并且依赖于它们的一致性。这里的余数等于我所要求的模,以及商。对于小于零的除数,这可能会有不同的结果。Doh!是的,我的意思是“同时返回商。”更正。它适用于负除数,结果如注释文档中所示。余数(也称为“模”)将与除数具有相同的符号。对于负因子,模在整个范围内是周期性的(-divisor,0)。
/// <summary>
/// Compute integer quotient and remainder of <paramref name="dividend"/> / <paramref name="divisor"/>
/// where the <paramref name="remainder"/> has the same sign as <paramref name="divisor"/>, and is
/// between zero (inclusive) and the <paramref name="divisor"/> (exclusive). As always,
/// (quotientResult * <paramref name="divisor"/> + <paramref name="remainder"/> == <paramref name="dividend"/>).
/// </summary>
public static int DivRemPeriodic(int dividend, int divisor, out int remainder) {
    var quotient = Math.DivRem(dividend, divisor, out remainder);
    if (divisor > 0 ? remainder < 0 : remainder > 0) {
        remainder += divisor;
        quotient -= 1;
    }
    return quotient;
}