Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 查找大BigInteger的日志时工作不正常?_C#_.net_Biginteger_Largenumber - Fatal编程技术网

C# 查找大BigInteger的日志时工作不正常?

C# 查找大BigInteger的日志时工作不正常?,c#,.net,biginteger,largenumber,C#,.net,Biginteger,Largenumber,我试图找到C#中一个非常大的大整数的对数。 我不管对数的底是多少。 当我尝试这个: 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 exp

我试图找到C#中一个非常大的大整数的对数。 我不管对数的底是多少。 当我尝试这个:

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的值不起作用。

您在日志和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"));

您能在这里发布更多的代码吗?您缺少的一些变量。@TheKingDave好的,我解释了这些变量并使其更简单。我希望:)尝试了这个,在我的例子中,指数是一个浮点值。你为什么要把它转换成整数?我用一个很大的myBigInt试过了,它仍然没有崩溃。最后一个b应该是1000。如果你的日志是基于1000的,那么你需要基于1000的Pow。@Kingdave感谢你的回答。是的,指数是一个浮点值,但必须将其转换为int,因为biginger.Pow方法要求指数值为int。显然,C#不允许将大整数提升为浮点值幂…谢谢,它现在可以工作了。似乎我无法投票给你们中的任何人。不幸的是,我是新来的。你可以点击复选标记将答案标记为正确。哎呀,我没注意到。完成。