Java 精度测试编号

发布于 2024-12-11 18:35:23 字数 2136 浏览 1 评论 0原文


我必须编写一个方法,在不使用 Math.atan() 的情况下计算 ArcTan
我的算法正在工作,但它没有停止。

public final static double EPSILON = 1E-16; // Given Value 0,00000000000000010000
public static void main(String[] args)
{   
    double arcTan = formula;

    while(Math.abs(arcTan) < EPSILON)
    {
       ...myAlgorythm...    
    }
}

23 个循环后,我的算法计算出与 Math.atan() 几乎相同的 arcTan,但我的 while 没有停止。
如何正确指定停止条件?

为了更好地理解,请参阅循环的输出:

1:  0,45833333333333330000  0,00000000000000010000
2:  0,46458333333333330000  0,00000000000000010000
3:  0,46346726190476184000  0,00000000000000010000
4:  0,46368427579365074000  0,00000000000000010000
5:  0,46363988658910527000  0,00000000000000010000
6:  0,46364927661314370000  0,00000000000000010000
7:  0,46364724210793534000  0,00000000000000010000
8:  0,46364769089584895000  0,00000000000000010000
9:  0,46364759050907880000  0,00000000000000010000
10: 0,46364761321561010000  0,00000000000000010000
11: 0,46364760803259750000  0,00000000000000010000
12: 0,46364760922469040000  0,00000000000000010000
13: 0,46364760894874296000  0,00000000000000010000
14: 0,46364760901297210000  0,00000000000000010000
15: 0,46364760899795077000  0,00000000000000010000
16: 0,46364760900147850000  0,00000000000000010000
17: 0,46364760900064694000  0,00000000000000010000
18: 0,46364760900084356000  0,00000000000000010000
19: 0,46364760900079693000  0,00000000000000010000
20: 0,46364760900080804000  0,00000000000000010000
21: 0,46364760900080537000  0,00000000000000010000
22: 0,46364760900080600000  0,00000000000000010000
23: 0,46364760900080580000  0,00000000000000010000
24: 0,46364760900080587000  0,00000000000000010000
25: 0,46364760900080587000  0,00000000000000010000
26: 0,46364760900080587000  0,00000000000000010000
27: 0,46364760900080587000  0,00000000000000010000
28: 0,46364760900080587000  0,00000000000000010000
29: 0,46364760900080587000  0,00000000000000010000
30: 0,46364760900080587000  0,00000000000000010000

第一列是我的计数器变量 n
第二列是我计算的 arcTan - 你会发现在循环 23 之后它并没有变得更精确
第三列是我的 Epsilon

如何检查我的第二列是否具有第三列中指定的精度?

I have to write a method, that calculates the ArcTan without using Math.atan()
My algorythm is working, but it isn't stoping.

public final static double EPSILON = 1E-16; // Given Value 0,00000000000000010000
public static void main(String[] args)
{   
    double arcTan = formula;

    while(Math.abs(arcTan) < EPSILON)
    {
       ...myAlgorythm...    
    }
}

After 23 loops my algorythm has calculated nearly the same arcTan as Math.atan() but my while isn't stopping.
How do i specyfy the stop-condition the right way?

For better unterstanding see the output of my loop:

1:  0,45833333333333330000  0,00000000000000010000
2:  0,46458333333333330000  0,00000000000000010000
3:  0,46346726190476184000  0,00000000000000010000
4:  0,46368427579365074000  0,00000000000000010000
5:  0,46363988658910527000  0,00000000000000010000
6:  0,46364927661314370000  0,00000000000000010000
7:  0,46364724210793534000  0,00000000000000010000
8:  0,46364769089584895000  0,00000000000000010000
9:  0,46364759050907880000  0,00000000000000010000
10: 0,46364761321561010000  0,00000000000000010000
11: 0,46364760803259750000  0,00000000000000010000
12: 0,46364760922469040000  0,00000000000000010000
13: 0,46364760894874296000  0,00000000000000010000
14: 0,46364760901297210000  0,00000000000000010000
15: 0,46364760899795077000  0,00000000000000010000
16: 0,46364760900147850000  0,00000000000000010000
17: 0,46364760900064694000  0,00000000000000010000
18: 0,46364760900084356000  0,00000000000000010000
19: 0,46364760900079693000  0,00000000000000010000
20: 0,46364760900080804000  0,00000000000000010000
21: 0,46364760900080537000  0,00000000000000010000
22: 0,46364760900080600000  0,00000000000000010000
23: 0,46364760900080580000  0,00000000000000010000
24: 0,46364760900080587000  0,00000000000000010000
25: 0,46364760900080587000  0,00000000000000010000
26: 0,46364760900080587000  0,00000000000000010000
27: 0,46364760900080587000  0,00000000000000010000
28: 0,46364760900080587000  0,00000000000000010000
29: 0,46364760900080587000  0,00000000000000010000
30: 0,46364760900080587000  0,00000000000000010000

The 1st column is my counter variable n
The 2nd column is my calculated arcTan - you see it isn't getting more precise after loop 23
The 3rd column is my Epsilon

How Do I check if my 2nd column has the precison specified in 3rd column?

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

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

发布评论

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

评论(2

计㈡愣 2024-12-18 18:35:23

也许您需要更改

while(Math.abs(arcTan) < EPSILON || n < 70)

while(Math.abs(arcTan) < EPSILON && n < 70)

,但如果不知道循环内发生了什么,您就不会知道。

编辑:也许是这样的

public final static double EPSILON = 1E-16; // Given Value 0,00000000000000010000
public static void main(String[] args)
{   
    double arcTan = formula;
    double previous = 0, current;

    while(n<70)
    {
        current = ...myAlgorythm...

        if (current - previous < EPSILON)
            break;
        else
            previous = current;
    }
}

Perhaps you need to change

while(Math.abs(arcTan) < EPSILON || n < 70)

to

while(Math.abs(arcTan) < EPSILON && n < 70)

but won't know without knowing what happens inside the loop.

EDIT: The maybe something like

public final static double EPSILON = 1E-16; // Given Value 0,00000000000000010000
public static void main(String[] args)
{   
    double arcTan = formula;
    double previous = 0, current;

    while(n<70)
    {
        current = ...myAlgorythm...

        if (current - previous < EPSILON)
            break;
        else
            previous = current;
    }
}
哽咽笑 2024-12-18 18:35:23

您需要计算 Math.atan 的结果与您自己的计算结果之间的差异(并将其与 epsilon 进行比较):

double arcTan = Math.atan(myValue);

while(Math.abs(arcTan - myArcTan) >= EPSILON && n < 70) {
    ...
}

编辑:替换 || 与其他答案中的 && 一样。

You need to calculate the difference between the result from Math.atan and your own calculation (and compare it with epsilon):

double arcTan = Math.atan(myValue);

while(Math.abs(arcTan - myArcTan) >= EPSILON && n < 70) {
    ...
}

EDIT: replaced || with && as in other answer.

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