java生成pi到第n位数字
我想知道如何生成 pi 的第 n 位数字。我有几个基本想法。
- 使用 Math.PI 并提高精度(如果可能的话)
- 使用欧拉公式生成 pi 但即使在这里,我也需要提高精度(我认为)
- 还有斯里尼瓦萨·拉马努金 (Srinivasa Ramanujan) 的 PI 生成公式,该公式以其快速收敛而闻名。这个公式看起来很难实现。我相信,我还必须在这里提高十进制精度。
简而言之,无论哪种方式,我都需要提高 BigDecimal
取决于第 n 位数字是什么。我如何将 BigDecimal 的精度提高到第 n 位?另外,如果有更好更快的方法,请您指出正确的方向。
编辑:我只想生成 PI。我不想用于计算。这是一个关于如何使用 BigDecimal 来实现我生成 PI 的想法的问题。
I wanted to know how I can generate pi to the nth digit. I have a couple of basic ideas.
- Use
Math.PI
and increase the precision (if that's possible) - Use Euler's formula to generate pi but even here, I would need to increase the precision (I think)
- There is also Srinivasa Ramanujan's formula for generating PI which is known for it's rapid convergence. This formula seems difficult to implement. I believe, I would have to also increase deicmal precision here.
So in short, either way, I would need to increase the precision of BigDecimal
depending on what the nth digit is. How would I go about increasing the precision of BigDecimal
to nth digit? Also, if there is a better and faster of doing this, can you please point me in the correct direction.
EDIT: I just want to generate PI. I don't want to use for calculations. and this is a question about how I can use BigDecimal to implement my ideas of generating PI.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Math.PI
的类型为double
。这意味着大约 15 位十进制数字的精度,这就是您拥有的全部数据;没有什么可以神奇地使 PI 的附加数字出现。BigDecimal
具有任意精度。setScale()
允许您创建具有任意精度的BigDecimal
对象,并且大多数算术方法会根据需要自动增加精度,但当然精度越高,所有计算都会越慢。BigDecimal
没有内置 sqrt(),因此您必须编写自己的 sqrt() 。Math.PI
is of typedouble
. That means about 15 decimal digits of precision, and that is all the data you have; nothing will magically make additional digits of PI appear.BigDecimal
has arbitrary precision.setScale()
allows you to createBigDecimal
objects with as much precision as you want and most of the arithmetic methods will automatically increase precision as required, but of course the more precision, the slower all calculations will be.BigDecimal
, so you'll have to write your own.您需要使用
MathContext
来提高BigDecimal
的精度,例如,
重要的是您在计算中使用的所有
BigDecimal
都使用该>MathContext
。Heron 的方法应该只需 10 次迭代即可为您提供 1000 位数字的精度,并且只需 20 次迭代即可为您提供 100 万位数字的精度,因此它当然已经足够好了。
另外,在程序开始时仅创建一次所有常量
BigDecimal
,例如26390
。You need to use
MathContext
to increase the precision of theBigDecimal
e.g.
It's important that ALL the
BigDecimal
s you use in your calculations use thatMathContext
.Heron's method should give you 1000 digits precision with only 10 iterations and a million digits with 20 iterations so it's certainly good enough.
Also, create all the constant
BigDecimal
s like e.g.26390
only once at the start of your program.您可以使用此代码
资源
You can use this code
resource