java 计算10W 精度位π

作者: 30而立人 | 来源:发表于2017-03-15 23:04 被阅读652次
package com.study.pi;
import java.math.BigDecimal;

/**
 * Created by hekun< http://snappydata.top >on 2017/3/14.
 */
public class ComputePi {
    /**
     * 较著名的表示π的级数有莱布尼茨级数   π/4=1-1/3+1/5-1/7+1/9……
     * 以及威廉姆斯无穷乘积式   π/2=2*2/3*4/3*4/5*6/5*6/7*8/7*8/9……
     */

    /** 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.  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.
     */

    public static void main(String[] args){

        int digits = 100000;   //精度10万位
        String pi = computePi(digits).toString();
        System.out.println("length: " + pi.length());
        System.out.println(pi);

    }

    public static void computePiByLBNZ(){

    }

    public static void computePiByWLMS(){

    }

    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.  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 ...
     */
    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;
    }

}

相关文章

  • java 计算10W 精度位π

  • 2018-11-04-1

    java double计算精度问题 double计算防止精度丢失:方案:将double转成bigDecimalSy...

  • Intel x87 FPU的使用基础

    Intel x87 FPU专门用于执行标量浮点计算,可以对单精度浮点(32位)、双精度浮点(64位)以及扩展双精度...

  • Java中的精度计算

    Java开发中用double可以进行简单的+,-,*,/的计算,但是小数点为数较多时,有时候不是很精确,会差个0....

  • php精度计算函数

    最大支持3位精度计算,具体还没有研究 bcadd — 将两个高精度数字相加 bccomp — 比较两个高精度...

  • java 基础

    1. java原始类型 Java 1.1增加了两个类,用于进行高精度的计算:BigInteger和BigDecim...

  • java中精确计算,double与BigDecimal的取舍

    相信java程序员都知道double是一种不能用作精确计算的类型,因为它会有精度损失,而要想规避精度损失,大家都会...

  • 大数据阶乘运算-java高精度运算

    在java中提供了两个拥有高精度计算了类:BigInteger和BigDecimal BigInteger:支持任...

  • Java浮点数计算精度问题总结

    Java浮点数计算精度问题总结 首先看看下面几个简单的加法计算的输出结果:System.out.println(0...

  • Android(Java)计算精度校正

    现象 得到的f4值是多少?稍有经验的程序员估计都会想「肯定不是0.4」Bingo!答案是0.39999998 原理...

网友评论

    本文标题:java 计算10W 精度位π

    本文链接:https://www.haomeiwen.com/subject/xbnrnttx.html