[英]C# BigInteger.ModPow bug?
I'm using the .NET BigInteger class to perform some math operations.我正在使用 .NET BigInteger类来执行一些数学运算。 However the ModPow method is giving me the wrong results.
但是ModPow方法给了我错误的结果。 I have compared it to Java which I think is correct:
我将它与我认为正确的 Java 进行了比较:
// 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)
Is it a bug in the .NET class or am I doing something wrong?它是 .NET 类中的错误还是我做错了什么?
It's just a matter of definitions.这只是定义的问题。 From MSDN on C# :
从C# 上的 MSDN :
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.BigInteger
值的模运算的行为与其他整数类型的模运算相同。
And from the JavaDocs for mod
:从JavaDocs for
mod
:
This method differs from
remainder
in that it always returns a non-negativeBigInteger
.该方法与
remainder
不同之处在于它总是返回一个非负的BigInteger
。
For more info, see http://en.wikipedia.org/wiki/Modulo_operation#Remainder_calculation_for_the_modulo_operation .有关更多信息,请参阅http://en.wikipedia.org/wiki/Modulo_operation#Remainder_calculation_for_the_modulo_operation 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.