marksweston / finance

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

When using XIRR get a TypeError in Rate.rb #19

Open wQwRtaufxJw7UFLCXzXz opened 11 years ago

wQwRtaufxJw7UFLCXzXz commented 11 years ago

wrong argument type Flt::DecNum (expected scalar Numeric)

in

finance (1.1.2) lib/finance/rates.rb:162:in `**'

changing

    def Rate.to_nominal(rate, periods)
      rate, periods = rate.to_d, periods.to_d                                                               

      if periods.infinite?                                                                                  
        (rate + 1).log                                                                                      
      else
         periods * ((1 + rate) ** (1 / periods) - 1)                                                         
      end                                                                                                   
    end  

by adding the line

periods = periods.to_f

to the else statements

      if periods.infinite?                                                                                  
        (rate + 1).log                                                                                      
      else
        periods = periods.to_f # my workaround
        periods * ((1 + rate) ** (1 / periods) - 1)                                                         
      end                                                                                                   

seems to fix it. Is this a bug you recognize?

wQwRtaufxJw7UFLCXzXz commented 11 years ago

Ruby version:

ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.4.2]

Rails version:

Rails 3.2.11

wQwRtaufxJw7UFLCXzXz commented 11 years ago

The most interesting thing is that SOMETIMES (I'd say about 50%) This random TypeError will show, and sometimes it works completely fine. It's so weird.

#<Class:0x007f9555d36fc0> can't be coerced into BigDecimal

in

/Users/ctwiz/.rvm/gems/ruby-1.9.3-p194/bundler/gems/finance-990fd53d46c3/lib/finance/cashflows.rb:118:in `**'
/Users/ctwiz/.rvm/gems/ruby-1.9.3-p194/bundler/gems/finance-990fd53d46c3/lib/finance/cashflows.rb:118:in `block in xnpv'
/Users/ctwiz/.rvm/gems/ruby-1.9.3-p194/bundler/gems/finance-990fd53d46c3/lib/finance/cashflows.rb:117:in `each'
/Users/ctwiz/.rvm/gems/ruby-1.9.3-p194/bundler/gems/finance-990fd53d46c3/lib/finance/cashflows.rb:117:in `inject'
/Users/ctwiz/.rvm/gems/ruby-1.9.3-p194/bundler/gems/finance-990fd53d46c3/lib/finance/cashflows.rb:117:in `xnpv'
/Users/ctwiz/.rvm/gems/ruby-1.9.3-p194/bundler/gems/finance-990fd53d46c3/lib/finance/cashflows.rb:35:in `values'
/Users/ctwiz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/bigdecimal/jacobian.rb:61:in `dfdxi'
/Users/ctwiz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/bigdecimal/jacobian.rb:80:in `block in jacobian'
/Users/ctwiz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/bigdecimal/jacobian.rb:79:in `each'
/Users/ctwiz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/bigdecimal/jacobian.rb:79:in `jacobian'
/Users/ctwiz/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/bigdecimal/newton.rb:58:in `nlsolve'
/Users/ctwiz/.rvm/gems/ruby-1.9.3-p194/bundler/gems/finance-990fd53d46c3/lib/finance/cashflows.rb:100:in `xirr'
actionpack (3.2.11) lib/action_view/template.rb:145:in `block in render'
activesupport (3.2.11) lib/active_support/notifications.rb:125:in `instrument'
actionpack (3.2.11) lib/action_view/template.rb:143:in `render'
marksweston commented 8 years ago

@ctwiz is this still a live issue for you?

wQwRtaufxJw7UFLCXzXz commented 8 years ago

@marksweston I'm no longer attached to that project. Sorry!