Hi i need help with a maths problem
I have to find the inital value of a loan
The information given is the payment per month, rate of interest, months of loan
Premium(double payment, double rate, int months);
for (int i=1; i<= months; i++)
{
double newRate = rate;
newRate /= (100 * 12);
premium += payment;
premium -= (premium * newRate)
}
This does not work please help
example if
for Interest = 12.0
payment = 101.0
m = 1
Expected inital loal = 100.0
thanks
[533 byte] By [
gambo2da] at [2007-10-2 9:37:49]

What does "doesn't work" mean?
jverda at 2007-7-16 23:43:57 >

What is the magic variable premium? Why don't you post your actual code for the full method using the code tags. It is impossible to tell anything if as noted you don't really describe your problem and you don't post your actual source either.
jverda at 2007-7-16 23:43:57 >

The loan before interest is not calculated correctly in the example the inital loan is not 100
> The loan before interest is not calculated correctly> > in the example the inital loan is not 100Okay. Post your real code please. Not this pseudo or random snippet stuff.
> > The loan before interest is not calculated
> correctly
> >
> > in the example the inital loan is not 100
>
> Okay. Post your real code please. Not this pseudo or
> random snippet stuff.
And also post what result you're getting. Lots of things are not 100. It will make it easier for people to help you if you're specific about what's going wrong--and it's in your best interest to make it as easy as possible for people to help you.
jverda at 2007-7-16 23:43:57 >

sorry for lack of info
public void computePremium(double payment, double rate, int months);
for (int i=1; i<= months; i++)
{
double newRate = rate; // yearly rate changed to months
newRate /= (100 * 12);
premium += payment;
premium -= (premium * newRate)
}
premium being the inital loan
This is not in code tags is it?So premium is a class variable I guess. This += nonsense doesn't seem like something you want to do. Why notpremium = 0 first?
for rate = 12.0 payment = 101.0 months = 1premium Expected: 100.0 got: 99.99 for rate = 12.0 payment = 102.01 months = 2premium Expected: 201.0 got: 298.9701 for rate = 24.0 payment = 10300.99 months = 2premiumExpected: 20000.0 got: 20275.171880039998
> sorry for lack of info
>
> public void computePremium(double payment, double
> rate, int months);
>
> for (int i=1; i<= months; i++)
> {
> double newRate = rate; // yearly rate
> changed to months
> newRate /= (100 * 12);
>
> premium += payment;
> premium -= (premium * newRate)
> }
> premium being the inital loan
That doesn't even compile.
Kaj
kajbja at 2007-7-16 23:43:58 >

> > sorry for lack of info
> >
> > public void computePremium(double payment, double
> > rate, int months);
> >
> > for (int i=1; i<= months; i++)
> > {
> > double newRate = rate; // yearly rate
> > changed to months
> > newRate /= (100 * 12);
> >
> > premium += payment;
> > premium -= (premium * newRate)
> > }
> > premium being the inital loan
>
> That doesn't even compile.
>
> Kaj
I think premium is a class variable of some sort. But it doesn't get reinitialized which can't be good.
kajbja at 2007-7-16 23:43:58 >

Thanks for you help
this will compile
public class computePremium
{
private double rate;
private double payment;
private int months;
private double premium;
public computePremium()
{
}
public double computePremium(double payment, double rate, int months)
{
for (int i=1; i<= months; i++)
{
double newRate = rate; // yearly rate chance to month
newRate /= (100 * 12);
premium += payment;
premium -= (premium * newRate);
}
return premium;
}
}
> > > sorry for lack of info
> > >
> > > public void computePremium(double payment,
> double
> > > rate, int months);
> > >
> > > for (int i=1; i<= months; i++)
> > > {
> > > double newRate = rate; // yearly rate
> > > changed to months
> > > newRate /= (100 * 12);
> > >
> > > premium += payment;
> > > premium -= (premium * newRate)
> > > }
> > > premium being the inital loan
> >
> > That doesn't even compile.
> >
> > Kaj
>
> I think premium is a class variable of some sort. But
> it doesn't get reinitialized which can't be good.
I was rather refering to the fact that the method declaration had an ; at the end, and that there were no "body" of the method declared. The code was just "hanging" in the air.
kajbja at 2007-7-16 23:43:58 >

Oi vey!
Here is a version just cleaned up. I have not tested the math yet but at least to get rid of the bad names, class and method variable confusion and having variables in for loops that shouldn't be.
public class PremiumCalculator
{
public double computePremium(double payment, double rate, int months)
{
double premium = 0.0D;
double newRate = rate; // yearly rate chance to month
for (int i=1; i<= months; i++)
{
newRate /= (100 * 12);
premium += payment;
premium -= (premium * newRate);
}
return premium;
}
}
kajbja at 2007-7-16 23:43:58 >

> Oi vey!
>
> Here is a version just cleaned up.
I just cleaned up your code ;)
public class PremiumCalculator {
public double computePremium(double payment, double rate, int months) {
double premium = 0.0D;
double newRate = rate; // yearly rate chance to month
for (int i = 1; i <= months; i++) {
newRate /= (100 * 12);
premium += payment;
premium -= (premium * newRate);
}
return premium;
}
}
Kaj
kajbja at 2007-7-16 23:43:58 >

> > Oi vey!> > > > Here is a version just cleaned up.> > I just cleaned up your code ;)> Hell man I agree. I can't stand that **** bracing style. But I figure those who post using it have enough problems.
at 2007-7-20 20:19:54 >

> for rate = 12.0 payment = 101.0 months = 1
>premium Expected: 100.0 got: 99.99
>
> for rate = 12.0 payment = 102.01 months = 2
>premium Expected: 201.0 got: 298.9701
>
> for rate = 24.0 payment = 10300.99 months = 2
> premiumExpected: 20000.0 got:
> got: 20275.171880039998
Well the second and third ones are dead wrong. Do your really mean to recalculate your rate every month? No, no you don't.
I don't really understand what the point of this is.
at 2007-7-20 20:19:54 >

> Well the second and third ones are dead wrong.And by this I mean that your expectations are also wrong. I believe. For 2 months with 12 rate and "payment" which I believe is actually the running total of 102.01 isn't the starting amount 100?
at 2007-7-20 20:19:54 >

If i have the rate of a loan, the lenght and payment per month i should be able to calculate the inital load i.e. loan before interest I know not really very useful but a essential part of a projectyour right the rate will not change
> If i have the rate of a loan, the lenght and payment
> per month i should be able to calculate the inital
> load i.e. loan before interest
>
> I know not really very useful but a essential part of
> a project
>
> your right the rate will not change
Is it a loan with interest on interest?
Kaj
kajbja at 2007-7-20 20:19:54 >

> > If i have the rate of a loan, the lenght and
> payment
> > per month i should be able to calculate the inital
> > load i.e. loan before interest
> >
> > I know not really very useful but a essential part
> of
> > a project
> >
> > your right the rate will not change
>
> Is it a loan with interest on interest?
>
> Kaj
Compounding... yes. But the OP was recaluting the monthly rate on each month which was wrong. The rate is constant. The interest paid changes.
kajbja at 2007-7-20 20:19:54 >

This is a nice [url= http://oakroadsystems.com/math/loan.htm]site[/url] that explains all those darn financial formulas quite well.kind regards,Jos
JosAHa at 2007-7-20 20:19:54 >
