Sample Java code to calculate PI to arbitrary precision. 파이 자바 코드를 계산하는 샘플을 임의 정확도합니다. This uses Machin’s formula: pi/4 = 4*arctan(1/5) - arctan(1/239) . 이것은 기계의 공식적인 : 파이 / 4 = 4 * arctan (1 / 5) - arctan (1 / 239).

Notes: 참고 사항 :
1. Run Pi with a positive integer precision value. 정확한 값을 양의 정수로 파이를 실행합니다. For example to calculate PI to 10 decimal places run: 예를 들면 파이 ~ 10 자릿수를 계산을 실행한다 :
java Pi 10 자바 파이 10
2. 두합니다. There are other algorithms and even better ones to calculate PI too. 다른 알고리즘과 파이를 계산하는 것들도 더 나은 너무합니다. This is just an example. 이것은 단지 예입니다. I use it to load test my server. 나는이 도구를 사용하여 내 서버 부하 테스트합니다.
3. 3합니다. Try running with 1 million (1000000) to give your server a good workout 실행해 보시기 바랍니다 1 만명 (1000000)에게 당신의 서버에 좋은 운동 :)

 import java.math.BigDecimal;  public class Pi {     public static void main(String args[]) throws NumberFormatException {         int digits = Integer.parseInt(args[0]);         String pi = computePi(digits).toString();         int freq[] = new int[10];         for(int i = 0;i < 10;i++) freq[i] = 0;         int c;         for(int i = 0;i < pi.length();i++) {             c = pi.charAt(i);             if(c == '.') continue;             c -= '0';             freq[c]++;         }         for(int i = 0;i < 10;i++) {             System.out.println("" + i + " " + freq[i]);         }     }     /** constants used in pi computation */     private static final BigDecimal FOUR = BigDecimal.valueOf(4);      /** rounding mode to use during pi computation */     private static final int roundingMode = BigDecimal.ROUND_HALF_EVEN;      /**      * Compute the value of pi to the specified number of      * digits after the decimal point. 수입 java.math.bigdecimal; 공용 클래스 파이 (공공 정적 무효 메인 (문자열 args []) 예외 numberformatexception (int 자릿수 = integer.parseint (args [0]); 문자열을 파이 = computepi (자리). toString ((); int 주파수 [] = 새로운 int [10];을 (int 전 = 0; 나는 <10; 나는 + +) 주파수 [전] = 0; int c;을 (int 전 = 0; 나는 <pi.length (); 나는 + +) (c = pi.charat (1); 경우 (c == '.') 계속; c -= '0 '; 주파수 [℃] + +;)을 (int 전 = 0; 나는 <10; 나는 + +) ( system.out.println ( ""+ 나는 + ""+ 주파수 [전]);)) / ** 상수를 계산에 사용되는 파이 * / 민간 정적 최종 bigdecimal 4 개 = bigdecimal.valueof (4); / ** 라운딩 모드로 파이를 사용하는 동안 계산 * / 민간 정적 최종 int roundingmode = bigdecimal.round_half_even; / ** * 계산을 지정된 개수의 값을 파이 * 자리 숫자 다음에 소수점합니다. The value is      * computed using Machin's formula:      *      *          pi/4 = 4*arctan(1/5) - arctan(1/239)      *      * and a power series expansion of arctan(x) to      * sufficient precision. * 컴퓨터를 사용하여이 값은 기계의 공식적인 : * * 파이 / 4 = 4 * arctan (1 / 5) - arctan (1 / 239) * *와 파워 시리즈 확장을 arctan (x)를 * 충분한 정밀도합니다. */     public static BigDecimal computePi(int digits) {         int scale = digits + 5;         BigDecimal arctan1_5 = arctan(5, scale);         BigDecimal arctan1_239 = arctan(239, scale);         BigDecimal pi = arctan1_5.multiply(FOUR).subtract(                                   arctan1_239).multiply(FOUR);         return pi.setScale(digits,                            BigDecimal.ROUND_HALF_UP);     }     /**      * Compute the value, in radians, of the arctangent of      * the inverse of the supplied integer to the specified      * number of digits after the decimal point. * / 공공 정적 bigdecimal computepi (int 자리 숫자) (int 규모 = 숫자 + 5; bigdecimal arctan1_5 = arctan (5, 규모); bigdecimal arctan1_239 = arctan (239, 규모); bigdecimal 파이 = arctan1_5.multiply (4). 빼기 ( arctan1_239). 번식 (4); 반환 pi.setscale (숫자, bigdecimal.round_half_up);) / ** * 계산 값을 라디안에 대해 * 반대의 아크 탄젠트의 정수를 사용하면 지정한 * 자릿수 공급 이후에 소수점합니다. The value      * is computed using the power series expansion for the      * arc tangent:      *      * arctan(x) = x - (x^3)/3 + (x^5)/5 - (x^7)/7 +      *     (x^9)/9 ... *은 계산된 값을 시리즈 확장에 전력을 사용하여 * 아크 탄젠트 : * * arctan (x) = x - (x ^ 3) / 3 + (x ^ 5) / 5 - (x ^ 7) / 7 + * (x ^ 9) / 9 ... */     public static BigDecimal arctan(int inverseX,                                     int scale)     {         BigDecimal result, numer, term;         BigDecimal invX = BigDecimal.valueOf(inverseX);         BigDecimal invX2 =             BigDecimal.valueOf(inverseX * inverseX);          numer = BigDecimal.ONE.divide(invX,                                       scale, roundingMode);          result = numer;         int i = 1;         do {             numer =                 numer.divide(invX2, scale, roundingMode);             int denom = 2 * i + 1;             term =                 numer.divide(BigDecimal.valueOf(denom),                              scale, roundingMode);             if ((i % 2) != 0) {                 result = result.subtract(term);             } else {                 result = result.add(term);             }             i++;         } while (term.compareTo(BigDecimal.ZERO) != 0);         return result;     } } * / 공공 정적 bigdecimal arctan (int inversex, int 규모) (bigdecimal 결과, numer, 용어; bigdecimal invx = bigdecimal.valueof (inversex); bigdecimal invx2 = bigdecimal.valueof (inversex * inversex); numer = bigdecimal.one.divide (invx, 규모, roundingmode); 결과 = numer; int 전 = 1;하지 (numer = numer.divide (invx2, 규모, roundingmode); int denom = 2 * 나는 + 1; 용어 = numer.divide (bigdecimal.valueof (denom), 규모, roundingmode); 경우 ((나는 % 2)! = 0) (결과 = result.subtract (검색어);) 다른 사람 (결과 = result.add (검색어);) 나는 + +;) 동안 (term. compareto (bigdecimal.zero)! = 0); 반환 결과;)) 

Note: The code has been adapted from a java.sun example. 참고 :이 코드가에서 적응 java.sun 예.