'Interest Rate Function
I found some code online regarding a finance calculator function but I'm not sure what it does, I'm still new to swift so if someone could explain the purpose of the function I would appreciate it a lot
func calculateAnnualInterestRate() -> Double {
let numberOfMonths = Double(self.NumberofPayments)
var x = 1 + (((self.MonthlyPayment * numberOfMonths/self.loan) - 1) / 12)
func F(_ x: Double) -> Double {
let F = self.loan * x * pow(1 + x, numberOfMonths) / (pow(1 + x, numberOfMonths) - 1) - self.MonthlyPayment
return F;
}
func F_Prime(_ x: Double) -> Double {
let F_Prime = self.loan * pow(x + 1, numberOfMonths - 1) * (x * pow(x + 1, numberOfMonths) + pow(x + 1, numberOfMonths) - (numberOfMonths * x) - x - 1) / pow(pow(x + 1, numberOfMonths) - 1, 2)
return F_Prime
}
while(abs(F(x)) > Double(0.000001)) {
x = x - F(x) / F_Prime(x)
}
let I = Double(12 * x * 100)
if I < 0 || I.isNaN || I.isInfinite {
self.interest = 0.0;
return self.interest
} else {
self.interest = I.roundTo2()
return self.interest
}
}
I know that the function is necessary for the calculation of interest rate but I'm not sure what the F and F_Prime
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
