Java 精度测试编号
我必须编写一个方法,在不使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您需要更改
为
,但如果不知道循环内发生了什么,您就不会知道。
编辑:也许是这样的
Perhaps you need to change
to
but won't know without knowing what happens inside the loop.
EDIT: The maybe something like
您需要计算 Math.atan 的结果与您自己的计算结果之间的差异(并将其与 epsilon 进行比较):
编辑:替换
||
与其他答案中的&&
一样。You need to calculate the difference between the result from
Math.atan
and your own calculation (and compare it with epsilon):EDIT: replaced
||
with&&
as in other answer.