The for loop below seems to be working for me, at the moment. I have
the interest rates at full numbers eg 5, 6 etc. but i need to change
them to 0.05, 0.06 but not sure how to go about this. Think it might be
something to do with precision?
// calculate amount of deposit for each of ten years
for ( int year = 1; year <= 10; year ++ ) {
//calculate interest rates
for ( int rate = 5; rate <=10; rate ++) {
// calculate new amount for specified year
amount = principal * Math.pow( 1.0 + rate, year );
// append one line of text to outputTextAea
outputTextArea.append( year + "\t" + moneyFormat.format( amount
) + "\t" + moneyFormat.format( amount )
+ "\t" + moneyFormat.format( amount ) + "\t" +
moneyFormat.format( amount ) + "\t" + moneyFormat.format( amount ) +
"\t" + moneyFormat.format( amount ) + "\n" );
}// end for
} //end for
Rajah - 31 Jan 2006 22:14 GMT
Shannon, it's not so much the precision, but rather the declaration
that you have for rate.
Since you declared it as (int rate = 5; rate <= 10; rate++) rate can
only be an integer.
Without giving you the answer, could you think of another way to
declare rate so that it might hold fractional parts?
By the way, the "rate <= 10" will end the loop with rate = 11. I don't
know if you want to write this as "rate < 10" instead.
> The for loop below seems to be working for me, at the moment. I have
> the interest rates at full numbers eg 5, 6 etc. but i need to change
> them to 0.05, 0.06 but not sure how to go about this. Think it might be
> something to do with precision?
shannon - 01 Feb 2006 09:02 GMT
Should I use double instead of int?
(double rate =5; rate > 10; rate ++)
> Shannon, it's not so much the precision, but rather the declaration
> that you have for rate.
[quoted text clipped - 12 lines]
> > them to 0.05, 0.06 but not sure how to go about this. Think it might be
> > something to do with precision?
Steve Bosman - 01 Feb 2006 09:39 GMT
> Should I use double instead of int?
> (double rate =5; rate > 10; rate ++)
I'd strongly recommend never using doubles in loops, for this problem
it isn't too much of an issue, but eventually the practice will lead
you into trouble.
Steve
Steve Bosman - 01 Feb 2006 09:37 GMT
> By the way, the "rate <= 10" will end the loop with rate = 11. I don't
> know if you want to write this as "rate < 10" instead.
No it won't - try it
Steve
Rajah - 01 Feb 2006 21:47 GMT
You're right, Steve. I stand corrected.
Shannon, if you wrote the for loop as
for ( double rate = 5; rate <=10; rate ++)
then it would usually go through the loop the last time with rate =
10.0.
Steve's warning probably refers to the notion that if it looped a large
number of times or using a very small increment (like .01) then you
might end at 10.0, or you might end at something lower.
I tried this code:
for (double rate = 5; rate <= 10; rate+= .01)
System.out.println("Value: " + rate );
and the last time through, rate was 9.999999999999893.
If you're going from .05 to .10 by .01, you probably won't face this
kind of roundoff error. Your program results would look correct,
especially with the moneyFormat function rounding things off.
(Why is there roundoff error? In part, it is because numbers that look
even in our decimal system, like 0.1, cannot be represented exactly in
binary, because they have repeating digits.)
In contrast with doubles, ints are represented exactly. If you and your
prof don't mind being off by a tiny fraction, or if your moneyFormat
function is going to round off correctly, then your for loop with the
double will work just fine.
Best wishes.
Steve Bosman - 01 Feb 2006 09:35 GMT
> The for loop below seems to be working for me, at the moment. I have
> the interest rates at full numbers eg 5, 6 etc. but i need to change
[quoted text clipped - 9 lines]
> // calculate new amount for specified year
> amount = principal * Math.pow( 1.0 + rate, year );
This line is going to give you problems, ask yourself the question what
value does 1.0 + rate give me for each value of rate and what value do
I want it to give me?
> // append one line of text to outputTextAea
> outputTextArea.append( year + "\t" + moneyFormat.format( amount
[quoted text clipped - 5 lines]
> }// end for
> } //end for