gpt4 book ai didi

C# BigInteger.ModPow 错误?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:19:47 29 4
gpt4 key购买 nike

我正在使用 .NET BigInteger类来执行一些数学运算。然而 ModPow方法给我错误的结果。我将它与我认为正确的 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)

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

最佳答案

这只是一个定义问题。来自 MSDN on C# :

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.

来自the JavaDocs for mod :

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 .

关于C# BigInteger.ModPow 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16884221/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com