Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
Version with BigDecimal to show more digits.
  • Loading branch information
potiron-lotte7 authored Apr 28, 2021
1 parent fb34c04 commit 292124a
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions MainPi.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,53 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.math.RoundingMode;

public class MainPi {

public static void main(String[] args) throws NumberFormatException, IOException {

/*manual precision*/
/*
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter number of time to do the sum: ");
long nbRepetition = Long.parseLong(reader.readLine());

/*Max precision:*/
//long nbRepetition = Long.MAX_VALUE;

*/


/*Max precision:*/
long nbRepetition = Long.MAX_VALUE;



//the taylor series
Double pi = 0.0 , temp = 0.0 ;
Double piDouble = 0.0;

BigDecimal pi = new BigDecimal(piDouble);
for(long i = 0; i < nbRepetition; i++) {
//temporary variable
temp = (Math.pow((-1), i)) / ((2*i)+1);
//add previous temporary variable to actual sum
pi = pi + temp;

BigDecimal temp = new BigDecimal((2.0*i)+1.0);
BigDecimal temp2 = new BigDecimal(Math.pow((-1), i));

BigDecimal operation = temp2.divide(temp, 5000, RoundingMode.HALF_UP);
pi = pi.add(operation);

BigDecimal temp3 = new BigDecimal(4.0);
System.out.println(i + ": " + (pi.multiply(temp3)));
}
// ***4*** arctanx x = 1 = Pi
pi = pi*4.0;



//print
System.out.println("\n\nAproximation de Pi: \n\n" + pi);
// ***4*** arctanx x = 1 = Pi
BigDecimal mult = new BigDecimal(4.0);
pi = pi.multiply(mult);


//difference with Pi from Eclipse -- Math.PI
System.out.println(pi - Math.PI);

//print
System.out.println("\n\nPi: \n\n" + pi + "\n\n");
}
}

0 comments on commit 292124a

Please sign in to comment.