'Vb.net code not performing as Javascript code
I translated a script from javascript that is correctly computing the APRs on mortgage loans into visual basic code which I'm trying to use on another site. I have posted a link below where you can see the javascript live. Here below is the code and console messages that I'm generating. The button click event starts at the computeForm at the bottom of the code.
function CalcLoanPayment(Loan_Amount, Interest_Rate, Term_Years) {
var A = Loan_Amount;
var i = Interest_Rate;
var n = Term_Years;
var amt = A / CalcPv(i / 12, n * 12);
console.log("Amount: " + amt);
return A / CalcPv(i / 12, n * 12);
}
function CalcPv(i, n) {
var num = (1 - Math.pow(1 + i, -n)) / i;
console.log("CalcPv: " + num);
return (1 - Math.pow(1 + i, -n)) / i;
}
function CalcAPR(form, Interest_Rate, Term_Years, Points, Fees, Loan_Amount) {
var i = parseFloat(Interest_Rate);
var n = parseInt(Term_Years);
var p = parseFloat(Points);
var fe = parseFloat(Fees);
var A = parseFloat(Loan_Amount);
var f = 0;
var f1 = 0;
var iflag = 0;
//var lp = (CalcLoanPayment(A, i, n) * 12);
//console.log(lp);
p = (A * (p * .01));
f1 = (A - (p + fe)) / (CalcLoanPayment(A, i, n) * 12);
console.log("A: " + A, "Fees: " + fe, "f1: " + f1);
f = 999;
i = 0;
while (f > f1) {
i = i + .01; f = CalcPv(i / 12, n * 12) / 12;
console.log("i +.01" + i + "f: " + f);
}
while (f < f1) {
i = i - .001; f = CalcPv(i / 12, n * 12) / 12;
console.log("i -.001" + i + "f: " + f);
}
while (f > f1) {
i = i + .0001; f = CalcPv(i / 12, n * 12) / 12;
console.log("i + .0001" + i + "f: " + f);
}
while (f < f1) {
i = i - .00001; f = CalcPv(i / 12, n * 12) / 12;
console.log("i - .00001" + i + "f: " + f);
}
while (f > f1) {
i = i + .000001; f = CalcPv(i / 12, n * 12) / 12;
console.log("i + .00001" + i + "f: " + f);
}
console.log("I: " + i);
return (i);
}
function computeForm(form) {
if ((form.term.value == null || form.term.value.length == 0) ||
(form.interest.value == null || form.interest.value.length == 0) ||
(form.points.value == null || form.points.value.length == 0)) {
return;
}
var i = form.interest.value * .01;
var j = form.interest.value * .01;
var n = form.term.value;
var p = form.points.value;
var fee = form.fees.value;
var A = form.amnt.value;
i = CalcAPR(form, i, n, p, fee, A);
form.AprResult.value = (i * 100);
form.PmntResult.value = CalcLoanPayment(A, j, n).toFixed(2);
return;
}
alcPv: 248.97265725134278
Amount: 1270.189680631263
CalcPv: 248.97265725134278
A: 316242.5 Fees: 11787 f1: 19.974411738298457
CalcPv: 310.9070672073437
i +.010.01f: 25.90892226727864
CalcPv: 270.54851648520196
i +.010.02f: 22.545709707100162
CalcPv: 237.18938150428227
i +.010.03f: 19.76578179202352
CalcPv: 240.25179533325704
i -.0010.028999999999999998f: 20.020982944438085
CalcPv: 239.9429959226171
i + .00010.029099999999999997f: 19.99524966021809
CalcPv: 239.63476823493866
i + .00010.029199999999999997f: 19.969564019578222
CalcPv: 239.66556531131928
i - .000010.029189999999999997f: 19.97213044260994
CalcPv: 239.69636809382035
i - .000010.029179999999999998f: 19.974697341151696
CalcPv: 239.69328755875875
i + .000010.029181f: 19.974440629896563
CalcPv: 239.69020708076602
i + .000010.029182f: 19.974183923397167
I: 0.029182
CalcPv: 248.97265725134278
Amount: 1270.189680631263
CalcPv: 248.97265725134278
The I at .029182 or 2.9182 % is the correct APR. You can do this live on my website and hit your F12 key to see the same messages. It's at https://pacificwestcapital.net/California_mortgage_rates.aspx Just scroll down the page. The entries are $316,242.50 for loan amount, 2.625% rate, 30 years, no points and fees are $11787.
In my VB.net code, the APR is different. I calculated 2.727%. I believe that I have translated the code correctly from javascript into vb.net. The very first number is suspect from the messages. THe 248.9726 is not what I see when I place a breakpoint in the vb.net code. The number I read is 4.57142...nothing close to 248.9726. The 1270.1896 rounded to 1270.19 is the correct monthly payment which is fully amortized over 30 years. The same number and the same breakpoint in the vb.net code is $69,178.04. I assume that Math.pow works exactly the same in vb.net or javascript. I'm a little lost on how these numbers are so different. Here is my vb.net code:
Protected Function CalcLoanPayment(ByVal Loan_Amount As Decimal, ByVal Interest_Rate As Decimal, ByVal Term_Years As Int16) As Double
Dim amt As Double = Loan_Amount / CalcPV(Interest_Rate / 12, Term_Years * 12)
Return Loan_Amount / CalcPV(Interest_Rate / 12, Term_Years * 12)
End Function
Protected Function CalcPV(ByVal i As Decimal, ByVal n As Int16) As Double
Dim a As Double = (1 - Math.Pow(1 + i, -n)) / i
Return (1 - Math.Pow(1 + i, -n)) / i
End Function
Function CalcAPR(ByVal Interest_Rate As Decimal, ByVal Term_Years As Decimal, ByVal Fees As Decimal, ByVal Loan_Amount As Decimal) As Double
Dim i As Decimal = Interest_Rate
Dim n As Decimal = Term_Years
Dim A As Decimal = Loan_Amount
Dim f As Double = 0
Dim f1 As Double = 0
f1 = (A - Fees) / (CalcLoanPayment(A, i, n) * 12)
f = 999
i = 0
While f > f1
i = i + 0.01
f = CalcPV(i / 12, n * 12) / 12
End While
While f < f1
i = i - 0.001
f = CalcPV(i / 12, n * 12) / 12
End While
While f > f1
i = i + 0.0001
f = CalcPV(i / 12, n * 12) / 12
End While
While f < f1
i = i - 0.00001
f = CalcPV(i / 12, n * 12) / 12
End While
While f > f1
i = i + 0.000001
f = CalcPV(i / 12, n * 12) / 12
End While
Return (i)
End Function
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
