0


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?

4

2 回答 2

1

也许你需要改变

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;
    }
}
于 2011-10-25T09:19:47.077 回答
0

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

double arcTan = Math.atan(myValue);

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

编辑:替换||&&其他答案。

于 2011-10-25T09:20:11.293 回答