Reputation: 1281
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?
Upvotes: 0
Views: 173
Reputation: 581
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;
}
}
Upvotes: 1
Reputation: 3260
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.
Upvotes: 0