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��Ҳ������� |




































