# JS function returning nagative values

I found this code for computing rate of APR and it works fine for me until i put something like

• Amount borrowed to be 2000
• return in 20 years time.
• baserate is 15%

and i get APR of -100 and so on.

``````function rate(paymentsPerYear, paymentAmount, presentValue, futureValue, dueEndOrBeginning, interest)
{
//If interest, futureValue, dueEndorBeginning was not set, set now
if (interest == null)
interest = 0.01;

if (futureValue == null)
futureValue = 0;

if (dueEndOrBeginning == null)
dueEndOrBeginning = 0;

var FINANCIAL_MAX_ITERATIONS = 128;//Bet accuracy with 128
var FINANCIAL_PRECISION = 0.0000001;//1.0e-8

var y, y0, y1, x0, x1 = 0, f = 0, i = 0;
var rate = interest;
if (Math.abs(rate) < FINANCIAL_PRECISION)
{
y = presentValue * (1 + paymentsPerYear * rate) + paymentAmount * (1 + rate * dueEndOrBeginning) * paymentsPerYear + futureValue;
}
else
{
f = Math.exp(paymentsPerYear * Math.log(1 + rate));
y = presentValue * f + paymentAmount * (1 / rate + dueEndOrBeginning) * (f - 1) + futureValue;
}
y0 = presentValue + paymentAmount * paymentsPerYear + futureValue;
y1 = presentValue * f + paymentAmount * (1 / rate + dueEndOrBeginning) * (f - 1) + futureValue;

// find root by Newton secant method
i = x0 = 0.0;
x1 = rate;
while ((Math.abs(y0 - y1) > FINANCIAL_PRECISION)
&& (i < FINANCIAL_MAX_ITERATIONS))
{
rate = (y1 * x0 - y0 * x1) / (y1 - y0);
x0 = x1;
x1 = rate;

if (Math.abs(rate) < FINANCIAL_PRECISION)
{
y = presentValue * (1 + paymentsPerYear * rate) + paymentAmount * (1 + rate * dueEndOrBeginning) * paymentsPerYear + futureValue;
}
else
{
f = Math.exp(paymentsPerYear * Math.log(1 + rate));
y = presentValue * f + paymentAmount * (1 / rate + dueEndOrBeginning) * (f - 1) + futureValue;
}

y0 = y1;
y1 = y;
++i;
}
return rate;
}
``````

upon debugging, i found (maybe i am wrong) something happens when code hit, the bold part of

``````y = presentValue * f + paymentAmount * (1 / rate + dueEndOrBeginning) * (f - 1) + futureValue;
``````

-1 + futureValue

what can be wrong?

do you have some example parameters to test the calculation?

@Dormilich It’s on user based. so there is amount, a base rate and months (say 5 years = 60 months),. so in my example.
It can be 20000 amount , 15% rating and year can be 60 months. which gives me -100% .

The function has a function below it

``````function cal(term, monthlyRepayment, netLoanAmount) {

return Math.pow(1+rate(term,0-monthlyRepayment,netLoanAmount,0),12)-1;
}
``````

I doubt that `paymentAmount` should be a negative value.

You mean, the field text i enetered? It’s positive. like 20,000. so if the year goes above 14. The result is -100%, till 14 years (168), it work fine.

nope, the value you pass to the rate() function.

When i did my alert, after
y = presentValue * f + paymentAmount * (1 / rate + dueEndOrBeginning) * f ;
It printed out, correct values, but when the value of f - 1 , then the value returns negative. It’s very complex maths going on. I guess

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.