求 pi 的值直到 50 位

发布于 2024-10-30 21:16:04 字数 52 浏览 3 评论 0原文

我想计算 PI 的值直到 50 位。

如何在java中实现小数点后50位?

I want to calculate the value of PI till 50 digits.

How to do this in java for 50 decimal places?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

残疾 2024-11-06 21:16:04

您无法使用默认数据类型执行此操作,因为您需要 50 位数字:50 / log(2) * log(10) = 166 位。这里 BigDecimal 是您可以使用的一种类型。但您应该记住,22/7 只是 pi 的近似值,要得到正确的 50 位数字,您需要更好的公式(例如蒙特卡洛方法、泰勒级数……)。

You cant do that with default data types, as you need for 50 digits: 50 / log(2) * log(10) = 166 bits. Here BigDecimal is one type you could use instead. But you should have in mind, that 22/7 is just an approximation of pi, and to get it right for 50 digits you need much better formula (e.g. Monte-Carlo method, taylor series, ...).

云雾 2024-11-06 21:16:04

您正在使用双精度变量,而应该使用精度更高的变量。查看 BigDecimal 类。

You are using a double variable and instead should use something that has a greater precision. Look into the BigDecimal class.

是伱的 2024-11-06 21:16:04
public class PiReCalc {
  public static final int N = 1000; // # of terms
   public static void main(String[] args) {
  BigDecimal sum = new BigDecimal(0);      // final sum
  BigDecimal term = new BigDecimal(0);           // term without sign
  BigDecimal sign = new BigDecimal(1.0);     // sign on each term

  BigDecimal one = new BigDecimal(1.0);
  BigDecimal two = new BigDecimal(2.0);

  for (int k = 0; k < N; k++) {
     BigDecimal count = new BigDecimal(k); 
     //term = 1.0/(2.0*k + 1.0);
     BigDecimal temp1 = two.multiply(count);
     BigDecimal temp2 = temp1.add(one);
     term = one.divide(temp2,50,BigDecimal.ROUND_FLOOR);

     //sum = sum + sign*term;
     BigDecimal temp3 = sign.multiply(term);
     sum = sum.add(temp3);

     sign = sign.negate();
  }
  BigDecimal pi = new BigDecimal(0);
  BigDecimal four = new BigDecimal(4);
  pi = sum.multiply(four);

  System.out.println("Calculated pi (approx., " + N + " terms and 50 Decimal Places): " + pi);
  System.out.println("Actual pi: " + Math.PI);
   }
}

输出为

计算得出的 pi(约 1000 项和 50 位小数):3.14059265383979292596359650286939597045138933077984
实际圆周率:3.141592653589793

public class PiReCalc {
  public static final int N = 1000; // # of terms
   public static void main(String[] args) {
  BigDecimal sum = new BigDecimal(0);      // final sum
  BigDecimal term = new BigDecimal(0);           // term without sign
  BigDecimal sign = new BigDecimal(1.0);     // sign on each term

  BigDecimal one = new BigDecimal(1.0);
  BigDecimal two = new BigDecimal(2.0);

  for (int k = 0; k < N; k++) {
     BigDecimal count = new BigDecimal(k); 
     //term = 1.0/(2.0*k + 1.0);
     BigDecimal temp1 = two.multiply(count);
     BigDecimal temp2 = temp1.add(one);
     term = one.divide(temp2,50,BigDecimal.ROUND_FLOOR);

     //sum = sum + sign*term;
     BigDecimal temp3 = sign.multiply(term);
     sum = sum.add(temp3);

     sign = sign.negate();
  }
  BigDecimal pi = new BigDecimal(0);
  BigDecimal four = new BigDecimal(4);
  pi = sum.multiply(four);

  System.out.println("Calculated pi (approx., " + N + " terms and 50 Decimal Places): " + pi);
  System.out.println("Actual pi: " + Math.PI);
   }
}

The output is

Calculated pi (approx., 1000 terms and 50 Decimal Places): 3.14059265383979292596359650286939597045138933077984
Actual pi: 3.141592653589793

樱&纷飞 2024-11-06 21:16:04

以下是 Bailey、Borwein 和 Plouffe 的突破性论文:http://oldweb. cecm.sfu.ca/projects/pihex/p123.pdf

与此同时,发现了更快的公式(遵循相同的原理):http://en.wikipedia.org/wiki/Bellard%27s_formula

Here is the break through paper of Bailey, Borwein and Plouffe: http://oldweb.cecm.sfu.ca/projects/pihex/p123.pdf

In the meantime, even faster formulas (following the same principles) were found: http://en.wikipedia.org/wiki/Bellard%27s_formula

扭转时空 2024-11-06 21:16:04

这是 Bellard 公式 bigPi(200,2000) 的快速而肮脏的实现,适用于 75 毫秒内超过 500 位小数。

public static BigDecimal bigPi(int max,int digits) {
    BigDecimal num2power6 = new BigDecimal(64);
    BigDecimal sum = new BigDecimal(0);
    for(int i = 0; i < max; i++ ) {
        BigDecimal tmp;
        BigDecimal term ; 
        BigDecimal divisor;
        term = new BigDecimal(-32); 
        divisor = new BigDecimal(4*i+1); 
        tmp =  term.divide(divisor, digits, BigDecimal.ROUND_FLOOR);
        term = new BigDecimal(-1); 
        divisor = new BigDecimal(4*i+3); 
        tmp = tmp.add(term.divide(divisor, digits, BigDecimal.ROUND_FLOOR));
        term = new BigDecimal(256); 
        divisor = new BigDecimal(10*i+1); 
        tmp = tmp.add(term.divide(divisor, digits, BigDecimal.ROUND_FLOOR));
        term = new BigDecimal(-64); 
        divisor = new BigDecimal(10*i+3); 
        tmp = tmp.add(term.divide(divisor, digits, BigDecimal.ROUND_FLOOR));
        term = new BigDecimal(-4); 
        divisor = new BigDecimal(10*i+5); 
        tmp = tmp.add(term.divide(divisor, digits, BigDecimal.ROUND_FLOOR));
        term = new BigDecimal(-4); 
        divisor = new BigDecimal(10*i+7); 
        tmp = tmp.add(term.divide(divisor, digits, BigDecimal.ROUND_FLOOR));
        term = new BigDecimal(1); 
        divisor = new BigDecimal(10*i+9); 
        tmp = tmp.add(term.divide(divisor, digits, BigDecimal.ROUND_FLOOR));
        int s = ((1-((i&1)<<1)));
        divisor = new BigDecimal(2); 
        divisor = divisor.pow(10*i).multiply(new BigDecimal(s));
        sum = sum.add(tmp.divide(divisor, digits, BigDecimal.ROUND_FLOOR));
    }
    sum = sum.divide(num2power6,digits, BigDecimal.ROUND_FLOOR);
    return sum;

}

This is a quick and dirty implementation of Bellard's formula bigPi(200,2000) is good for over 500 decimal places in 75ms.

public static BigDecimal bigPi(int max,int digits) {
    BigDecimal num2power6 = new BigDecimal(64);
    BigDecimal sum = new BigDecimal(0);
    for(int i = 0; i < max; i++ ) {
        BigDecimal tmp;
        BigDecimal term ; 
        BigDecimal divisor;
        term = new BigDecimal(-32); 
        divisor = new BigDecimal(4*i+1); 
        tmp =  term.divide(divisor, digits, BigDecimal.ROUND_FLOOR);
        term = new BigDecimal(-1); 
        divisor = new BigDecimal(4*i+3); 
        tmp = tmp.add(term.divide(divisor, digits, BigDecimal.ROUND_FLOOR));
        term = new BigDecimal(256); 
        divisor = new BigDecimal(10*i+1); 
        tmp = tmp.add(term.divide(divisor, digits, BigDecimal.ROUND_FLOOR));
        term = new BigDecimal(-64); 
        divisor = new BigDecimal(10*i+3); 
        tmp = tmp.add(term.divide(divisor, digits, BigDecimal.ROUND_FLOOR));
        term = new BigDecimal(-4); 
        divisor = new BigDecimal(10*i+5); 
        tmp = tmp.add(term.divide(divisor, digits, BigDecimal.ROUND_FLOOR));
        term = new BigDecimal(-4); 
        divisor = new BigDecimal(10*i+7); 
        tmp = tmp.add(term.divide(divisor, digits, BigDecimal.ROUND_FLOOR));
        term = new BigDecimal(1); 
        divisor = new BigDecimal(10*i+9); 
        tmp = tmp.add(term.divide(divisor, digits, BigDecimal.ROUND_FLOOR));
        int s = ((1-((i&1)<<1)));
        divisor = new BigDecimal(2); 
        divisor = divisor.pow(10*i).multiply(new BigDecimal(s));
        sum = sum.add(tmp.divide(divisor, digits, BigDecimal.ROUND_FLOOR));
    }
    sum = sum.divide(num2power6,digits, BigDecimal.ROUND_FLOOR);
    return sum;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文