生成pi到第n位java [英] generating pi to nth digit java

查看:168
本文介绍了生成pi到第n位java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何将pi生成到第n位。我有几个基本想法。

I wanted to know how I can generate pi to the nth digit. I have a couple of basic ideas.


  1. 使用 Math.PI 并增加精度(如果可能的话)

  2. 使用Euler的公式来生成pi但是在这里,我需要提高精度(我认为)

  3. 还有Srinivasa Ramanujan用于生成PI的公式,它以快速收敛而着称。这个公式似乎很难实现。我相信,我还必须在这里增加deicmal精度。


  1. Use Math.PI and increase the precision (if that's possible)
  2. Use Euler's formula to generate pi but even here, I would need to increase the precision (I think)
  3. 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.

所以总之,无论哪种方式,我都需要提高 BigDecimal 取决于第n位是什么。我如何将 BigDecimal 的精度提高到第n位?此外,如果有更好更快的做法,请指点我正确的方向。

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.

编辑:我只想生成PI。我不想用于计算。这是一个关于如何使用BigDecimal来实现我生成PI的想法的问题。

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.

推荐答案


  • Math.PI 的类型为 double 。这意味着大约15个十进制数字的精度,这就是你拥有的所有数据;没有任何东西会神奇地显示PI的附加数字。

  • BigDecimal 具有任意精度。 setScale()允许您创建具有所需精度的 BigDecimal 对象,并且大多数算术方法将自动生成根据需要提高精度,但当然精度越高,所有计算都越慢。

  • 实施Ramanujan公式最困难的部分具有讽刺意味的是常数因子中的sqrt(2) ,因为 BigDecimal 没有内置的sqrt(),所以你必须自己编写。

    • Math.PI is of type double. 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 create BigDecimal 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.
    • The most difficult part of implementing Ramanujan's formula will ironically be the sqrt(2) in the constant factor, because there is not built-in sqrt() for BigDecimal, so you'll have to write your own.
    • 这篇关于生成pi到第n位java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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