Generating pi to the nth digit of java

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

  • Use Math.PI and increase accuracy (if possible)
  • Use the Euler formula to generate pi, but even here I will need to increase the accuracy (I think) Euler's formula for PI
  • There is also the Srinivasa Ramanujan formula for generating PI, which is known for its fast convergence. This formula seems complicated. I suppose I would also need to increase the optimal accuracy.
    enter image description here

So, in any case, I need to increase the accuracy of BigDecimal depending on what the nth digit is. How can I increase the accuracy of BigDecimal to the nth digit? Also, if it will be better and faster, can you point me in the right direction.

EDIT: I just want to create a PI. I do not want to use for calculations. and this is a question about how I can use BigDecimal to implement my ideas for generating PI.

+8
java precision pi bigdecimal
source share
3 answers
  • Math.PI is of type double . This means about 15 decimal digits of accuracy, and that’s all the data you have; nothing will magically create extra PI numbers.
  • BigDecimal has arbitrary precision. setScale() allows you to create BigDecimal objects with the same precision as you want, and most arithmetic methods automatically increase accuracy as needed, but of course, the greater the accuracy, the slower all calculations will be.
  • The hardest part of implementing the Ramanujan formula, ironically, will be sqrt (2) in a constant coefficient, because there is no built-in sqrt () for BigDecimal , so you have to write your own.
+6
source share

You need to use MathContext to increase BigDecimal precision

eg.

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

It is important that ALL BigDecimal used in your calculations use a MathContext . The Heron method should give you 1000 precision digits with 10 iterations and a million digits with 20 iterations, so that of course it is good enough. In addition, create all BigDecimal constants, for example, for example. 26390 only once at the beginning of your program.

+3
source share

You can use this code

 import java.math.BigDecimal; import java.math.RoundingMode; public final class Pi { private static final BigDecimal TWO = new BigDecimal("2"); private static final BigDecimal FOUR = new BigDecimal("4"); private static final BigDecimal FIVE = new BigDecimal("5"); private static final BigDecimal TWO_THIRTY_NINE = new BigDecimal("239"); private Pi() {} public static BigDecimal pi(int numDigits) { int calcDigits = numDigits + 10; return FOUR.multiply((FOUR.multiply(arccot(FIVE, calcDigits))) .subtract(arccot(TWO_THIRTY_NINE, calcDigits))) .setScale(numDigits, RoundingMode.DOWN); } private static BigDecimal arccot(BigDecimal x, int numDigits) { BigDecimal unity = BigDecimal.ONE.setScale(numDigits, RoundingMode.DOWN); BigDecimal sum = unity.divide(x, RoundingMode.DOWN); BigDecimal xpower = new BigDecimal(sum.toString()); BigDecimal term = null; boolean add = false; for (BigDecimal n = new BigDecimal("3"); term == null || term.compareTo(BigDecimal.ZERO) != 0; n = n.add(TWO)) { xpower = xpower.divide(x.pow(2), RoundingMode.DOWN); term = xpower.divide(n, RoundingMode.DOWN); sum = add ? sum.add(term) : sum.subtract(term); add = ! add; } return sum; } } 

resource

0
source share

All Articles