generating pi to nth digit java

前端 未结 3 557
无人共我
无人共我 2020-12-16 20:51

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

  1. Use Math.PI and increase the precision (if that\'s possib
3条回答
  •  时光说笑
    2020-12-16 21:20

    You need to use MathContext to increase the precision of the BigDecimal

    e.g.

    MathContext mc = new MathContext(1000);
    BigDecimal TWO = new BigDecimal(2, mc);
    

    It's important that ALL the BigDecimals you use in your calculations use that MathContext. 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 BigDecimals like e.g. 26390 only once at the start of your program.

提交回复
热议问题