Skip to content

Commit

Permalink
Fix pmt when interest rate is 0
Browse files Browse the repository at this point in the history
  • Loading branch information
rmm5t committed Sep 9, 2024
1 parent 7277cd5 commit 9f1679f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/exonio/financial.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ def nper(rate, pmt, pv, fv = 0, end_or_beginning = 0)
# Exonio.pmt(0.075/12, 12*15, 200_000) # ==> -1854.0247200054619
#
def pmt(rate, nper, pv, fv = 0, end_or_beginning = 0)
return (-pv - fv) / nper if rate.zero?

temp = (1 + rate) ** nper
fact = (1 + rate * end_or_beginning) * (temp - 1) / rate

Expand Down
12 changes: 12 additions & 0 deletions spec/financial_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,18 @@

expect(results).to eq(-1872.522689198246)
end

it 'computes pmt when the interest rate is 0' do
results = Exonio.pmt(0.0, 36, 36_000.0)

expect(results).to eq(-1000.0)
end

it 'computes pmt when the interest rate is 0 with fv' do
results = Exonio.pmt(0.0, 24, 24_000.0, -12_000.0)

expect(results).to eq(-500.0)
end
end

describe '#pv' do
Expand Down

0 comments on commit 9f1679f

Please sign in to comment.