I'm sure this is really easy for most Java people, but I'm just starting so bear with me. This is the problem I have to solve
<< A cancellation error occurs when you are manipulating a very large number with a very small number. The large number may cancel out the smaller number. For example, on some computers the result of 10000.0 + 0.000001 is equal to 10000.0. To avoid cancellation errors, and obtain more accurate results, carefully select the order of computation. For example, in computing the following series, you will obtain more accuate results by computing it from right to left:
1 + 1/2 + 1/3 + ... + 1/n
Write a program to compare the results of the summation of the preceding series, computing from left to right and from right to left with n =50000. >>
The problem is, I wouldn't know what the right answer is, even if I have it, because the book doesn't say what the answer to each series is. So can someone tell me if I'm on the right track? OK, so this is what I have programmed so far.
<< public class Cancellation
{
public static void main(String[] args)
{
double n = 1;
double x;
double sum = 0;
double m = 50000;
double y;
double sum2 = 0;
while (n <= 50000)
{
x = 1/n;
sum+=x;
n++;
}
while (m >= 1)
{
y = 1/m;
sum2+=y;
m--;
}
System.out.println("The answer for left to right is " + sum);
System.out.println("The answer for right to left is " + sum2);
}
}
>>
And here is the result the above generates.
<<
The answer for left to right is 11.397003949278504
The answer for right to left is 11.397003949278519 >>
So, am I close? I see the answers are different, like they are supposed to be, but I'm not sure if it is correct. Is my logic correct? Thanks for any help.
<< A cancellation error occurs when you are manipulating a very large number with a very small number. The large number may cancel out the smaller number. For example, on some computers the result of 10000.0 + 0.000001 is equal to 10000.0. To avoid cancellation errors, and obtain more accurate results, carefully select the order of computation. For example, in computing the following series, you will obtain more accuate results by computing it from right to left:
1 + 1/2 + 1/3 + ... + 1/n
Write a program to compare the results of the summation of the preceding series, computing from left to right and from right to left with n =50000. >>
The problem is, I wouldn't know what the right answer is, even if I have it, because the book doesn't say what the answer to each series is. So can someone tell me if I'm on the right track? OK, so this is what I have programmed so far.
<< public class Cancellation
{
public static void main(String[] args)
{
double n = 1;
double x;
double sum = 0;
double m = 50000;
double y;
double sum2 = 0;
while (n <= 50000)
{
x = 1/n;
sum+=x;
n++;
}
while (m >= 1)
{
y = 1/m;
sum2+=y;
m--;
}
System.out.println("The answer for left to right is " + sum);
System.out.println("The answer for right to left is " + sum2);
}
}
>>
And here is the result the above generates.
<<
The answer for left to right is 11.397003949278504
The answer for right to left is 11.397003949278519 >>
So, am I close? I see the answers are different, like they are supposed to be, but I'm not sure if it is correct. Is my logic correct? Thanks for any help.
