2

To estimate the number PI, I want to particulary use Monte Carlo method applying to a dart game with a circle with radius 1 inside a 1x1 square. More description can be found here. I want a Java code to estimate PI up to 200 digits. I have used BigDecimal but it shows 49 decimal digits and put zero number for the rest. I appreciate any helps.

import java.util.*;
import java.math.BigDecimal;

public class PiFinder {

    // description
    public static void main(String[] args) {
        Scanner iteration = new Scanner(System.in);
        System.out.print("Enter the number of iteration: ");
        int NoThrows = iteration.nextInt();
        BigDecimal PI = new BigDecimal(PIcalculation(NoThrows));
        PI = PI.setScale(200);

        // Difference of our estimated PI and the actual PI
        BigDecimal Difference = new BigDecimal(0);
        BigDecimal actualPI = new BigDecimal(Math.PI);
        System.out.println("Actual PI: " + actualPI);
        Difference = PI.subtract(actualPI);

        // Display results
        System.out.println("The Number of Throws = " + NoThrows);
        System.out.println("***************************");
        System.out.println("Estimated PI = " + PI);
        System.out.println("Difference = " + Difference);
    }

    // Deteremine a thrown dart is inside the circle
    public static boolean insideCircle(double pos_X, double pos_Y) {
        double distance = Math.sqrt((pos_X * pos_X) + (pos_Y * pos_Y));

        return (distance < 1.0);
    }


    public static double PIcalculation(int NoThrows) {
        Random randomGen = new Random(System.currentTimeMillis());
        int hits = 0;
        double PI = 0;

        for (int i = 1; i <= NoThrows; i++) {

            double pos_X = (randomGen.nextDouble()) * 2 - 1.0;
            double pos_Y = (randomGen.nextDouble()) * 2 - 1.0;

            if (insideCircle(pos_X, pos_Y)) {
                hits++;
            }
        }

        double dthrows = NoThrows;

        // the formule
        PI = (4.0 * (hits / dthrows));

        return PI;
    }
}
1
  • While double has a precision of 16 decimal places the Math.sqrt can half the precision to just 8. You need to use BigDecimal throughout however, you also need alot of patience as every digit takes ten time longer. i.e. say 20 digits takes a second, then 30 digits will take 300 years,and 40 digits will take more than the age of the universe. Dec 23, 2014 at 8:33

1 Answer 1

3

Your PiCalculation returns a double, you can't expect to have more precision with this.

Use BigDecimal everywhere, if you want to go further.

But anyway with this method, even with 10^10 throws, you will only have 10 decimals.

The 49 decimals you had with your double, were due to the double lack of precision

4
  • plus 1, double will create up to 52 digits when converted to a BigDecimal but itself only has 16 digits of precision at best. Dec 23, 2014 at 8:30
  • @yunandtidus: Thank you very much. I will change them all to BigDecimal and post the output here in any case.
    – WhiteHat
    Dec 23, 2014 at 11:03
  • Note that in order to have 200 decimals, you need to run 10^200 throws, this is uncomputable ! Dec 23, 2014 at 11:05
  • So what would you suggest? Is there any tractable way?
    – WhiteHat
    Dec 23, 2014 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.