Skip to content
This repository has been archived by the owner on Sep 30, 2021. It is now read-only.

Commit

Permalink
NPV calculation is now much faster
Browse files Browse the repository at this point in the history
  • Loading branch information
waissbluth committed Aug 8, 2014
1 parent 55363f9 commit 4eea484
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/croovy_finance/npv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ def initialize(payments)
end

def call(rate)
exp = 1.0
factor = 1.0 + rate
accum_factor = 1.0

@payments.map do |pmt|
pmt / (factor ** exp).tap {exp += 1.0}
end.reduce(:+)
@payments.inject(0.0) do |result, pmt|
result + pmt / (accum_factor *= factor)
end
end

end

0 comments on commit 4eea484

Please sign in to comment.