0

我试图在 C# 中找到一个非常大的 BigInteger 的对数。我不在乎对数的底是什么。当我尝试这个时:

BigInteger b = 1000; // the base
// myBigInt is a huge BigInt i want to find the Log of.

exponent = BigInteger.Log(myBigInt, 1000); //Find the Log
// Re-create the orignal BigInt now that I know base and exponent
BigInteger.Pow(b, Convert.ToInt32(exponent)); 

我得到一个溢出异常,因为 Int32 不能保存日志的结果。增加 base 的值是行不通的。

4

1 回答 1

0

您在 log 和 pow 中没有使用相同的基础

BigInteger bigInt = 10000000000000000000;  // myBigInt is a huge BigInt i want to find the Log of.
double log;
log = BigInteger.Log(bigInt, 1000); //Find the Log
System.Diagnostics.Debug.WriteLine(log.ToString());
System.Diagnostics.Debug.WriteLine(((Int32)log).ToString());
BigInteger bigIntRecreate;
// unless log is an integer value it will round down and will not recreate to proper value
bigIntRecreate = BigInteger.Pow(1000, (Int32)log);
System.Diagnostics.Debug.WriteLine(bigInt.ToString("N0"));
System.Diagnostics.Debug.WriteLine(bigIntRecreate.ToString("N0"));
System.Diagnostics.Debug.WriteLine((bigInt - bigIntRecreate).ToString("N0"));
于 2013-05-24T15:56:58.847 回答