marksweston / finance

A library for financial calculations in Ruby
https://rubygems.org/gems/finance
Other
221 stars 91 forks source link

NPV calculation has side effects #28

Open adailey14 opened 10 years ago

adailey14 commented 10 years ago

the NPV calculation (and probably others) have unexpected side effects on the input array. It will turn all numbers in the input array into DecNums.

Example:

x = [1,2,3] => [1, 2, 3] x.npv(1.5) => DecNum('2.28') x => [DecNum('1'), DecNum('2'), DecNum('3')]

This example is from the rails console using ruby 2.0.0-p353 and rails 3.2.13

tubedude commented 10 years ago

@adailey14 , I was able to solve this issue adding an intermediary variable to the NVP ( #30 ) calculation. I haven't tested it much, but it preserves the original array: https://github.com/tubedude/finance/commit/412118c99c63f62224a7301042c675d155b5efc0

x = [1,2,3] # => [1, 2, 3]
x.npv(1.5) # =>DecNum('2.28')
x # => [1, 2, 3]