marksweston / finance

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

Example doesn't work with ruby 1.9.3 #7

Closed krschacht closed 12 years ago

krschacht commented 12 years ago

With ruby1.9.3p0 your XIRR example does not work:

@transactions = [] @transactions << Transaction.new(-1000, :date => Time.new(1985,01,01)) @transactions << Transaction.new( 600, :date => Time.new(1990,01,01)) @transactions << Transaction.new( 600, :date => Time.new(1995,01,01)) @transactions.xirr(0.6).round(2) #=> Rate("0.024851", :apr, :compounds => :annually)

It results in this error:

Math::DomainError: Zero or negative argument for log from /Users/krschacht/.rvm/gems/ruby-1.9.3-p0/gems/finance-1.1.0/lib/finance/cashflows.rb:106:in **' from /Users/krschacht/.rvm/gems/ruby-1.9.3-p0/gems/finance-1.1.0/lib/finance/cashflows.rb:106:inblock in xnpv' from /Users/krschacht/.rvm/gems/ruby-1.9.3-p0/gems/finance-1.1.0/lib/finance/cashflows.rb:105:in each' from /Users/krschacht/.rvm/gems/ruby-1.9.3-p0/gems/finance-1.1.0/lib/finance/cashflows.rb:105:ininject' from /Users/krschacht/.rvm/gems/ruby-1.9.3-p0/gems/finance-1.1.0/lib/finance/cashflows.rb:105:in xnpv' from /Users/krschacht/.rvm/gems/ruby-1.9.3-p0/gems/finance-1.1.0/lib/finance/cashflows.rb:35:invalues' from /Users/krschacht/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/bigdecimal/newton.rb:71:in nlsolve' from /Users/krschacht/.rvm/gems/ruby-1.9.3-p0/gems/finance-1.1.0/lib/finance/cashflows.rb:88:inxirr' from (irb):21 from /Users/krschacht/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.1.3/lib/rails/commands/console.rb:45:in start' from /Users/krschacht/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.1.3/lib/rails/commands/console.rb:8:instart' from /Users/krschacht/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.1.3/lib/rails/commands.rb:40:in <top (required)>' from script/rails:6:inrequire' from script/rails:6:in `

'

Downgrading to ruby 1.9.2p290 and ran the same thing and it error'd with:

NoMethodError: undefined method round' for Rate.new(0.024851, :apr):Finance::Rate from (irb):6 from /Users/krschacht/.rvm/gems/ruby-1.9.2-p290@rails313/gems/railties-3.1.3/lib/rails/commands/console.rb:45:instart' from /Users/krschacht/.rvm/gems/ruby-1.9.2-p290@rails313/gems/railties-3.1.3/lib/rails/commands/console.rb:8:in start' from /Users/krschacht/.rvm/gems/ruby-1.9.2-p290@rails313/gems/railties-3.1.3/lib/rails/commands.rb:40:in<top (required)>' from script/rails:6:in require' from script/rails:6:in

'

At least we're making progress. :)

@transactions.xirr(0.6).apr.round(2)

Fixed the last issue.

wkranec commented 12 years ago

Hi Keith,

Thanks for bringing this issue to my attention. I wasn't able to replicate the error you got on 1.9.3p0, I got the same one you had for 1.9.2p290:

1.9.3p0 :009 > @transactions.xirr(0.6).round(2) #=> Rate("0.024851", :apr, :compounds => :annually) NoMethodError: undefined methodround' for Rate.new(0.024851, :apr):Finance::Rate from (irb):9 from /Users/wkranec/.rvm/rubies/ruby-1.9.3-p0/bin/irb:16:in <main>'

In this case, I believe it to be an error with the docs ... rounding a rate object by itself doesn't make sense, we should only be able to round the value representing the rate applied in a specific time period (i.e. apr, monthly, etc.).

I think the appropriate fix here is to correct the docs to read:

@transactions.xirr(0.6) #=> Rate("0.024851", :apr, :compounds => :annually)

Please send along more details if you continue to see DomainErrors.

krschacht commented 12 years ago

Yea the second one was definitely just a doc correction. Maybe the first one was a combination of the ruby version + a specific version of another gem. In case it helps here is my gem list.

actionmailer (3.1.3) actionpack (3.1.3) activemodel (3.1.3) activerecord (3.1.3) activeresource (3.1.3) activesupport (3.1.3) addressable (2.2.6) ansi (1.4.1) archive-tar-minitar (0.5.2) arel (2.2.1) builder (3.0.0) bundler (1.0.22) coffee-rails (3.1.1) coffee-script (2.2.0) coffee-script-source (1.2.0) columnize (0.3.6) daemons (1.1.5) erubis (2.7.0) eventmachine (0.12.10) execjs (1.2.13) finance (1.1.0) flt (1.3.0) heroku (2.19.2) hike (1.2.1) i18n (0.6.0) jquery-rails (1.0.19) jquery_mobile_rails (1.0) json (1.6.3) launchy (2.0.5) linecache19 (0.5.12) mail (2.3.0) mechanize (2.0.1) mime-types (1.17.2) minitest (2.11.1) mobylette (1.4.2) moonshado-sms (1.1.1) multi_json (1.0.4) mysql2 (0.3.11) net-http-digest_auth (1.2) net-http-persistent (1.9) nokogiri (1.5.0) pg (0.12.0) polyglot (0.3.3) rack (1.3.5) rack-cache (1.1) rack-mount (0.8.3) rack-ssl (1.3.2) rack-test (0.6.1) rails (3.1.3) railties (3.1.3) rake (0.9.2.2) rake-compiler (0.7.9) rdoc (3.12) rest-client (1.6.7) ruby_core_source (0.1.5) rubyzip (0.9.6.1) sass (3.1.12) sass-rails (3.1.5) sequel (3.20.0) sinatra (1.0) sprockets (2.0.3) sqlite3 (1.3.5) sqlite3-ruby (1.3.3) taps (0.3.23) term-ansicolor (1.0.7) thin (1.3.1) thor (0.14.6) tilt (1.3.3) treetop (1.4.10) turn (0.8.3) tzinfo (0.3.31) uglifier (1.2.0) webrobots (0.0.12)

derekyau commented 12 years ago

I just tried the same example above and can confirm a few things

-It does work without the rails stack (on just pure irb in 1.9.3p0) -once I load it within the rails console, it throws the Math::DomainError: Zero or negative argument for log error

It seems there's a conflict with rails somewhere...