JAVA学习初步 级数求和

题目要求 利用给定公式求PI的值
package 级数求和;

public class progression {
   public static void main(String[] args) {
    double pi;
    pi=16*jishu(1.0/5)-4*jishu(1.0/239);
    System.out.printf("%.15f",pi);
    
   }
//定义求级数函数
   public static double jishu(double x) {
    // TODO Auto-generated method stub
    double n,m;
    int i=0;
    n=0;
    m=1;
    for(i=0;Math.abs(m)>=Math.pow(10,-15);i++){
     m=Math.pow(-1,i)*Math.pow(x, 2*i+1)/(double)(2*i+1);
     n+=m;
    }
    return n;
   }
  }

你可能感兴趣的:(JAVA学习记录)