Skip to content

Instantly share code, notes, and snippets.

@glaucocustodio
Created May 28, 2015 18:16
Show Gist options
  • Save glaucocustodio/58f86d7e5f184f7b6f2c to your computer and use it in GitHub Desktop.
Save glaucocustodio/58f86d7e5f184f7b6f2c to your computer and use it in GitHub Desktop.
Excel PMT Formula in Swift
class ExcelFormulas {
class func pmt(rate : Double, nper : Double, pv : Double, fv : Double = 0, type : Double = 0) -> Double {
return ((-pv * pvif(rate, nper: nper) - fv) / ((1.0 + rate * type) * fvifa(rate, nper: nper)))
}
class func pow1pm1(x : Double, y : Double) -> Double {
return (x <= -1) ? pow((1 + x), y) - 1 : exp(y * log(1.0 + x)) - 1
}
class func pow1p(x : Double, y : Double) -> Double {
return (abs(x) > 0.5) ? pow((1 + x), y) : exp(y * log(1.0 + x))
}
class func pvif(rate : Double, nper : Double) -> Double {
return pow1p(rate, y: nper)
}
class func fvifa(rate : Double, nper : Double) -> Double {
return (rate == 0) ? nper : pow1pm1(rate, y: nper) / rate
}
}
@Alexander-Sobolev
Copy link

Thanks for code!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment