.NET C#双等效项System.Numerics.BigInteger [英] .NET C# Double equivalent System.Numerics.BigInteger

查看:61
本文介绍了.NET C#双等效项System.Numerics.BigInteger的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一个计算pi位数的程序,但是我遇到了问题.经过三轮迭代后,正确位数的数量超过了可用内存的两倍.
我听说过System.Numerics.BigInteger类型(在System.Numerics.dll中),但是我需要使用浮点数.我对算法的理解不够充分,无法使用整数.

I'm am currently writing a program that calculates the digits of pi, and I have a problem. After three iterations the number of correct digits exceeds the memory available in a double.
I heard of the System.Numerics.BigInteger type (in System.Numerics.dll), but I need to use floating point numbers. I do not understand the algorithm well enough to use integers.

如果存在支持小数点的BigInteger版本,那就太好了.下面我放了我的C#代码:

It would be great if a version of the BigInteger exists which supports a decimal point. Below I have put my C# code:

var a = 1.0;
var b = 1 / Math.Sqrt(2);
var t = 0.25;
var p = 1.0;
double anext, bnext, tnext, pnext;
int count = 0;

for (int i = 1; i <= accuracy; i++ )
{
            anext = (a + b) / 2;
            bnext = Math.Sqrt(a * b);
            tnext = (t - p * ((a - anext) * (a - anext)));
            pnext = 2 * p;

            a = anext;
            b = bnext;
            t = tnext;
            p = pnext;

            var pi = ((a + b) * (a + b)) / (4 * t);

            Console.WriteLine("Iteration = " + i.ToString());
            Console.WriteLine("Pi =        " + pi + "\n\n\n\n");
}

推荐答案

BigInteger类由BCL团队的Melitta Andersen(她的第一个项目)编写.它实际上是在.NET 3.5中提供的,但被隐藏了,该类被标记为 internal .直到.NET 4.0才公开.她还编写了一个名为BigRational的浮点版本.类似的问题,没有进入.NET 4.0.这些是我无法理解的奇怪而无法解释的决定.

The BigInteger class was written by Melitta Andersen of the BCL team, her first project. It was actually shipped in .NET 3.5 but was hidden, the class was marked internal. Not to become public until .NET 4.0. She also wrote a floating point version, called BigRational. Similar problem, didn't make it into .NET 4.0. These are strange and unexplainable decisions whose reasoning I'm not privy to.

尽管如此,BigRational的源代码仍然可用.您可以在此处下载.

Nevertheless, the source code for BigRational is available. You can download it here.

这篇关于.NET C#双等效项System.Numerics.BigInteger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆