C#BigInteger.ModPow错误? [英] C# BigInteger.ModPow bug?

查看:197
本文介绍了C#BigInteger.ModPow错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用.NET BigInteger 类执行一些数学运算.但是, ModPow 方法给了我错误的结果.我将它与我认为正确的Java进行了比较:

I'm using the .NET BigInteger class to perform some math operations. However the ModPow method is giving me the wrong results. I have compared it to Java which I think is correct:

// C#
var a = new BigInteger(-1);
var b = new BigInteger(3);
var c = new BigInteger(5);
var x = BigInteger.ModPow(a, b, c); // (x = -1)

// Java
BigInteger a = new BigInteger("-1");
BigInteger b = new BigInteger("3");
BigInteger c = new BigInteger("5");
BigInteger x = a.modPow(b, c); // (x = 4)

这是.NET类中的错误,还是我做错了什么?

Is it a bug in the .NET class or am I doing something wrong?

推荐答案

这只是定义问题.来自 C#上的MSDN :

It's just a matter of definitions. From MSDN on C#:

由模运算返回的值的符号取决于除数的符号:如果除数为正,则模运算返回正的结果;否则,取正.如果为负,则模运算将返回负结果. BigInteger值的模运算的行为与其他整数类型的模运算的行为相同.

The sign of the value returned by the modulus operation depends on the sign of dividend: If dividend is positive, the modulus operation returns a positive result; if it is negative, the modulus operation returns a negative result. The behavior of the modulus operation with BigInteger values is identical to the modulus operation with other integral types.

并从 mod 的JavaDocs:

And from the JavaDocs for mod:

此方法与remainder的不同之处在于,它始终返回非负数BigInteger.

This method differs from remainder in that it always returns a non-negative BigInteger.

有关更多信息,请参见 http://en.wikipedia.org/wiki/Modulo_operation#Remainder_calculation_for_the_modulo_operation .

For more info, see http://en.wikipedia.org/wiki/Modulo_operation#Remainder_calculation_for_the_modulo_operation.

这篇关于C#BigInteger.ModPow错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆