Last active
January 8, 2018 07:27
-
-
Save SauloSilva/8d387ec19d2eb6478f7c3cf3719e5776 to your computer and use it in GitHub Desktop.
The Excel PMT function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Excel | |
module Formulas | |
## rate = The interest rate, per period. | |
## nper = The number of periods over which the loan or investment is to be paid. | |
## pv = The present value of the loan / investment. | |
## fv = An optional argument that specifies the future value of the loan / investment, at the end of nper payments. If omitted, [fv] has the default value of 0. | |
## type = An optional argument that defines whether the payment is made at the start or the end of the period. 0 for the payment is made at the end of the period and 1 for the payment is made at the beginning of the period. If the type argument is omitted, it takes on the default value of 0 (denoting payments made at the end of the period). | |
## Eg 1. pmt(0.0199, 1, 100) => tax of 1.99% in the 1 parcel of $100 ~= $101.99 with $1.99 tax | |
## Eg 2. pmt(0.0199, 2, 100) => tax of 1.99% in the 2 parcels of $100 ~= $51.50 with $1.5 tax per parcel | |
## Eg x. pmt(0.0199, x, 100) => tax of 1.99% in the x parcels of $100 ~= ∞ | |
def pmt(rate, nper, pv, fv=0, type=0) | |
((pv * pvif(rate, nper) - fv ) / ((1.0 + rate * type) * fvifa(rate, nper))) | |
end | |
protected | |
def pow1pm1(x, y) | |
(x <= -1) ? ((1 + x) ** y) - 1 : Math.exp(y * Math.log(1.0 + x)) - 1 | |
end | |
def pvif(rate, nper) | |
(rate.abs > 0.5) ? ((1 + rate) ** nper) : Math.exp(nper * Math.log(1.0 + rate)) | |
end | |
def fvifa(rate, nper) | |
(rate == 0) ? nper : pow1pm1(rate, nper) / rate | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment