Java JScience:如何打印整个实数? [英] Java JScience: How to print the whole Real number?

查看:114
本文介绍了Java JScience:如何打印整个实数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一些测试代码,将Pi计算为我想要计算的任何东西.看起来像这样:

I wrote some testing code which calculated Pi to whatever thing I wanted it to calculate to. It looks something like this:

public static void piCalculatorMethod1() {
    int iteration = 1000000;

    Real pi = Real.valueOf(0);

    for (int i = 1; i < iteration + 1; i++) {
        Real current = pi;
        Real addendum = Real.valueOf((1/Math.pow(i, 2)));

        pi = current.plus(addendum);
    }

    pi = pi.times(6);

    pi = pi.sqrt();

    System.out.println(pi.toString());
}

很不幸,输出决定它看起来像这样:

Quite unfortunately, the output decides it would look like this:

3.14159169866

我非常确定最终值比那要精确得多,因为我已经看到了它们实际添加的价值,并且比那要精确得多.

I'm quite sure the end value is much more accurate than that, because I've seen what values they are actually adding, and that's much more accurate than that.

如何获取System.out.println来显示整个Real而不是仅显示前几个数字?

How do I get System.out.println to show me the whole Real instead of just the first few digits?

推荐答案

您可能需要质疑关于该系列收敛的假设. π的这种近似依赖于Euler对巴塞尔问题的解决方案.根据经验,下面的示例找到许多迭代计数与π 2 /6的偏差.如您所见,迭代计数中的每个数量级都增加了不超过十进制的精度.

You may need to question your assumption about the convergence of the series. This approximation of π relies on Euler's solution to the Basel problem. Empirically, the example below finds the deviation from π2/6 for a number of iteration counts. As you can see, each order of magnitude in the iteration count adds no more than one decimal digit of accuracy.

代码:

Real PI_SQUARED_OVER_6 = Real.valueOf(Math.pow(Math.PI, 2) / 6);
for (int p = 0; p < 7; p++) {
    int iterations = (int) Math.pow(10, p);
    Real pi = Real.valueOf(0);
    for (int i = 1; i < iterations + 1; i++) {
        pi = pi.plus(Real.valueOf(1 / Math.pow(i, 2)));
    }
    System.out.println("10^" + p + ": " + PI_SQUARED_OVER_6.minus(pi));
}

控制台:


10^0: 6.44934066848226E-1
10^1: 9.5166335681686E-2
10^2: 9.950166663334E-3
10^3: 9.99500166667E-4
10^4: 9.9995000167E-5
10^5: 9.999950000E-6
10^6: 9.99999500E-7

这篇关于Java JScience:如何打印整个实数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆