Skip to content

Commit

Permalink
Added an intermediary variable in nvp formula
Browse files Browse the repository at this point in the history
This will prevent npv from changing the given array content.
  • Loading branch information
tubedude committed May 9, 2014
1 parent 696e9bc commit 412118c
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/finance/cashflows.rb
Expand Up @@ -70,10 +70,10 @@ def method_missing(name, *args, &block)
# @see http://en.wikipedia.org/wiki/Net_present_value
# @api public
def npv(rate)
self.collect! { |entry| Flt::DecNum.new(entry.to_s) }
cashflow = self.collect { |entry| Flt::DecNum.new(entry.to_s) }

rate, total = Flt::DecNum.new(rate.to_s), Flt::DecNum.new(0.to_s)
self.each_with_index do |cashflow, index|
cashflow.each_with_index do |cashflow, index|
total += cashflow / (1 + rate) ** index
end

Expand Down

0 comments on commit 412118c

Please sign in to comment.