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 に答える