C#高双精度 [英] C# High double precision

查看:272
本文介绍了C#高双精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写计算圆周率值,并将其作为一个双重功能。到现在为止还挺好。但是,一旦该函数的小数点后获取到14位,实在坚持不住了。我假设这是因为双有限的精度。我应该怎么做,继续小数点后获得更多的数字吗?

I'm writing a function that calculates the value of PI, and returns it as a double. So far so good. But once the function gets to 14 digits after the decimal place, it can't hold any more. I'm assuming this is because of the double's limited precision. What should I do to continue getting more numbers after the decimal place?

推荐答案

我不会做它的浮点在所有

I wouldn't do it in floating point at all.

回想一下,你的算法是:

Recall that your algorithm is:

(1 + 1 / (2 * 1 + 1)) *  
(1 + 2 / (2 * 2 + 1)) *  
(1 + 3 / (2 * 3 + 1)) *  
(1 + 4 / (2 * 4 + 1)) *  
(1 + 5 / (2 * 5 + 1)) *  
(1 + 6 / (2 * 6 + 1)) *  
(1 + 7 / (2 * 7 + 1)) *  ...

沿途每一个阶段,你计算的一小部分。为什么就不能保持在其分子/分母形式的分数?要计算的分数为:

Every stage along the way you compute a fraction. Why not simply keep that fraction in its numerator / denominator form? The fraction you want to compute is:

(4 / 3) * 
(7 / 5) *
(10 / 7) *
(13 / 9) * ...

这仅仅是4 * 7 * 10 * 13 ...上的顶部和底部3 * 5 * 7 * 9。

which is just 4 * 7 * 10 * 13 ... on the top and 3 * 5 * 7 * 9 on the bottom.

让自己的BigInteger类(一个船舶与System.Numerics 4.0框架),你可以很容易地计算的分子和分母,只要你想大的作为。然后你只需商转换为十进制的问题。嗯,这是很容易。大概的你知道该怎么做长除法的。只实现上吐出的数字所需数量的分子和分母的长除法算法。

Get yourself a BigInteger class (one ships with the 4.0 framework in System.Numerics) and you can easily compute the numerator and denominator as big as you want. Then you just have the problem of converting the quotient to decimal. Well that's easy enough. Presumably you know how to do long division. Just implement a long division algorithm on the numerator and denominator that spits out the desired number of digits.

这篇关于C#高双精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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