Java programmers, I need some homework help

trmiv

Lifer
Oct 10, 1999
14,670
18
81
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. :)




 

DDCSpeed

Golden Member
Nov 30, 2000
1,494
0
0
Can you explain more what your question is?

I dont seem to get what the program is trying to do.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Var starting from 1/50000: 11.39700394927
Var starting from 1/1: 11.3970039492785



hmm, that's what I got. I wrote it in perl though, not java :)

#!/usr/bin/perl
$ var =0;
for ($i=50000;$i>=1;$i--) {
$var = $var + (1/$i);
}

print "Var starting from 1/50000: $var\n";

$var=0;
for ($i=1;$i<=50000;$i++) {
$var = $var + (1/$i);
}

print "Var starting from 1/1: $var\n";
 

jpsj82

Senior member
Oct 30, 2000
958
0
0
i don't know java (only know pascal, c++, and vb) but your logic looks right. and I think your answers are right, becasue the difference is noticable, but not by much.
 

trmiv

Lifer
Oct 10, 1999
14,670
18
81
Thanks everyone.

DDCSpeed: The program is trying to show that the order of computation matters. That's why the answer is different when going from left to right, and from right to left. When it goes from left to right, it first does 1 + 1/2 + 1/3, and so on to + 1/50000. When it does it right to left, it starts at 1/50000 + 1/49999 and so on to 1/2 + 1. The answer is slighy different both ways, and accoring to the book, more accurate going from right to left.

rangeLife: How would I do it in one while block? I'm pretty new at this.