9

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 possible)
  2. Use Euler's formula to generate pi but even here, I would need to increase the precision (I think) Euler's formula for PI
  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.
    enter image description 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.

Jeel Shah
  • 3,274
  • 17
  • 47
  • 68

3 Answers3

7
  • 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.
Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • 1
    But for the square root, the good old Heron method converges fast enough even with a million (or a billion) digits of precision. – Daniel Fischer Dec 03 '11 at 19:41
  • for the sqrt(2) part, can I not just use the pre-defined value? 1.41421356, or will that change the calculation somehow? – Jeel Shah Dec 03 '11 at 19:46
  • @user681159: well, as soon as you want your value of PI to be more precise that that "pre-defined value", you'll need more digits of sqrt(2). – Michael Borgwardt Dec 03 '11 at 19:47
  • 2
    @user681159: Isn't it pretty obvious that if you want to calculate a very precise result based in a specific constant factor, you can't do that with an imprecise approximation of that factor? – Michael Borgwardt Dec 03 '11 at 19:50
  • sorry, I missed that. Cut me some slack. – Jeel Shah Dec 03 '11 at 19:56
  • For your last point, about the square root of 2 being the hardest would you not be able to do `BigDecimal sr2 = new BigDecimal(new BigDecimal(2).pow(1/2), MathContext(1000));`, replacing 1000 with your desired precision? EDIT: Nevermind, the first argument is an int, not allowing for use of rational exponents. – tylerr147 Apr 11 '19 at 20:37
4

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.

blackpanther
  • 10,998
  • 11
  • 48
  • 78
Tesseract
  • 8,049
  • 2
  • 20
  • 37
  • What do you mean by Heron's method? – Jeel Shah Dec 04 '11 at 01:33
  • @user681159 Heron's method coincides with the Newton-Raphson method for the case of square roots, in case you are familiar with the latter. Otherwise: it's finding better approximations to `sqrt(a)` via `x_(n+1) = 1/2*(x_n + a/x_n)`. It converges for all `a > 0` and any starting value `x_0 > 0`. If you start with a fairly good approximation, in each step the number of correct digits doubles (approximately). – Daniel Fischer Dec 04 '11 at 04:10
0

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

ggmax
  • 125
  • 10