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 * ظل الزاويه القوسي (1 / 5) -- ظل الزاويه القوسي (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. Try running with 1 million (1000000) to give your server a good workout حاول ادارة مع 1 مليون دولار (1000000) لإعطاء الخدمة الخاص بك جيدا workout : (

 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 دولي ارقام = integer.parseint (args [0]) ؛ الخيط بي computepi = (الارقام). tostring () ؛ دولي Freq [] = دولي جديد [10] ؛ ل(دولي الاول = 0 ؛ الاول <10 ؛ الاول + +) Freq [الاول] = 0 ؛ ج دولي ؛ ل(دولي الاول = 0 ؛ الاول <pi.length () ؛ الاول + +) (ج = pi.charat (ط) ؛ اذا كان (ج == '') يواصل ؛ ج -= '0' ؛ Freq [ج] + + ؛) ل(دولي الاول = 0 ؛ الاول <10 ؛ الاول + +) ( System.out.println ( "" + انا + "" + Freq [الاول]) ؛)) / ** الثوابت المستخدمة في بى * حساب الخاص ساكنة النهائي bigdecimal = bigdecimal.valueof أربعة (4) ؛ / ** طريقة التقريب لاستخدامها خلال بي * حساب الخاص ساكنة النهائي دولي 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 * ظل الزاويه القوسي (1 / 5) -- ظل الزاويه القوسي (1 / 239) * * وتوسيع نطاق السلطة سلسلة ظل الزاويه القوسي (خ) * الى الدقه الكافية. */     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 (ارقام دولي) (نطاق دولي ارقام = + 5 ؛ bigdecimal arctan1_5 = ظل الزاويه القوسي (5 ، الجدول) ؛ bigdecimal arctan1_239 = ظل الزاويه القوسي (239 ، الحجم) ؛ bigdecimal بي = arctan1_5.multiply (اربعة). مطروحا) arctan1_239). مضاعفة (اربعة) والعودة 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 -- (x ^ 3) / 3 + (س ^ 5) / 5 -- (x ^ 7) / 7 + * (س ^ 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 ظل الزاويه القوسي (inversex دولي ، على نطاق دولي) (bigdecimal نتيجة لذلك ، numer ، هذا المصطلح ؛ bigdecimal invx = bigdecimal.valueof (inversex) ؛ bigdecimal invx2 = bigdecimal.valueof (inversex * inversex) ؛ numer = bigdecimal.one.divide (invx ، النطاق ، roundingmode) ؛ نتيجة = numer ؛ دولي الاول = 1 ؛ (هل numer = numer.divide (invx2 ، النطاق ، roundingmode) ؛ دولي 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 من الامثله على ذلك.