Sample Java code to calculate PI to arbitrary precision.円周率を計算するJavaコードのサンプルを任意の精度を実現します。 This uses Machin’s formula: pi/4 = 4*arctan(1/5) - arctan(1/239) .この使用してマシャンの公式: PI / 4の= 4 *逆正接( 1 / 5 ) -逆正接( 1 / 2 39) 。

Notes:備考:
1. 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. 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実行してみますと1000000 ( 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 [ ] ) ( int桁の数字= numberformatexception例外integer.parseint ( args [ 0 ] ) ;文字列を円周率= computepi (桁の数字) 。のtoString ( ) ; int周波数[ ] =新しいint [ 10 ] ;を( int私= 0 ;私< 10 ;私+ + )周波数[私] = 0 ; int c ;を( int私= 0 ;私< pi.length ( ) ;私+ + ) ( c = pi.charat (私) ;場合( c == ' 。 ' )続行; c -= '0 ' ;周波数設定[ C ] + + ; )を( int私= 0 ;私< 10 ;私+ + ) ( system.out.println ( " " +私+ " " +周波数[私] ) ; ) ) / **定数で使用され円周率の計算* /プライベート静的ファイナルbigdecimal 4つの= bigdecimal.valueof ( 4 ) ; / **丸めモード円周率の計算を使用する時に行う* / private静的ファイナル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. *を使用して、値が計算さマシャンの公式: * * PI / 4の= 4 *逆正接( 1 / 5 ) -逆正接( 1 / 2 39) * *と、パワーシリーズの拡大を逆正接( 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. 場合* / public static bigdecimal computepi ( int桁の数字) ( intスケール=桁の数字+ 5 ; bigdecimal arctan1_5 =逆正接( 5 、スケール) ; bigdecimal arctan1_239 =逆正接( 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 ... *の値が計算パワーシリーズ拡張を使用して、 *アークタンジェント: * *逆正接(倍) = 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;     } } 場合* / public static bigdecimal逆正接( 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から適応されています。