Calculate PI To Arbitrary Precision (Sample Java Code)計算有價證券投資,以任意精度(樣本Java代碼)
Sample Java code to calculate PI to arbitrary precision.樣本Java代碼來計算有價證券投資,以任意精度。 This uses Machin’s formula: pi/4 = 4*arctan(1/5) - arctan(1/239) .本機使用的公式: 丕/ 4 = 4 * arctan ( 1 / 5 ) -a rctan( 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 Java的有價證券投資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 [ ] )全numberformatexception (詮釋數字= integer.parseint ( args [ 0 ] ) ;字符串丕= computepi (數字) 。 tostring ( ) ;詮釋頻率[ ] =新的詮釋[ 10 ] ; (詮釋= 0 ;我< 10 ,我+ + )頻率[一] = 0 ;詮釋C組為(詮釋= 0 ;我< pi.length ( ) ;我+ + ) ( c = pi.charat ( i )條;如果(三== ' ' )繼續下去; c -= '0 ' ;頻率[中] + + ; ) (詮釋= 0 ;我< 10 ,我+ + ) ( system.out.println ( “ ” + + “ ” +頻率[一] ) ; ) ) / **常數用於有價證券投資計算* /私人靜態的最後bigdecimal 4 = bigdecimal.valueof ( 4 ) ; / **四捨五入的方式使用期間,丕計算* /私人靜態的最後詮釋roundingmode = bigdecimal.round_half_even ; / ** *計算PI值到指定的數目*後的數字小數點。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 ) -a rctan( 1 / 2 39) * *和冪級數展開的a rctan( 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 (詮釋數字) (詮釋規模=數字+五; bigdecimal arctan1_5 = arctan ( 5 ,規模) ; bigdecimal arctan1_239 = arctan ( 239 ,規模) ; bigdecimal丕= arctan1_5.multiply ( 4 ) 。減去( arctan1_239 ) 。乘以( 4 ) ;返回pi.setscale (數字, bigdecimal.round_half_up ) ; ) / ** *計算的價值,在弧度,該arctangent *逆所提供的整數到指定*的位數。小數點後。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 + * ( χ ^ 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 ( 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的例子。
Filed under提起下 Headline News頭條新聞 , , How To如何 , , Java Software Java軟件 , , Open Source Software開放源碼軟件 , , Programming編程 , , Tech Note技術說明 | |
| |
RSS 2.0 2.0 | |
Trackback Trackback跟踪 this Article |此文章|
Email this Article電子郵件此文章
You may also like to read您也可以想讀 |



