marksweston / finance

A library for financial calculations in Ruby

Home Page:https://rubygems.org/gems/finance

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

can't get examples to work

derekyau opened this issue · comments

sorry, i may be missing something here (probably am :/)
but I just installed the gem (which is awesome by the way thanks for starting this!)

I wasn't able to get the examples working, this is within the context of a rails stack.

when I try the IRR example
[-4000,1200,1410,1875,1050].irr
I seem to be getting the error
Flt::DecNum can't be coerced into BigDecimal
This seems to be working fine in the rails console though.

as for the xirr example

@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)

I am getting this error
wrong argument type Float (expected Fixnum)

not quite sure what I am doing wrong, anyone have an idea?

Thanks!

Hi Derek,

Thanks for letting me know about this. It's always nice to hear from people that are using the gem!

First, I just want to confirm a few things: what version of ruby, rails, and the finance gem are you using? Right now, I am working with ruby 1.9.3-p0, no rails, and finance-1.1.1.gem. The examples you mentioned are currently in the unit tests, which pass on my machine. So I just want to make sure that we are starting from the same place.

Thanks,
Bill

hey bill,

I was overjoyed when I found a finance gem :P - I am envious of those python guys and their scipy numpy libs, this is a great start :).

The ticket above refers to rails version 3.0.9 and ruby 1.9.2p180 and p290

in particular the code seems to have problems with this line in cashflows.rb

n = t.amount / ( (1 + rate) ** ((t.date-start) / 31536000.to_d)) # 365 * 86400

Hi Derek,

I have verified the error (on the line you specified) on a fresh install of Ruby 1.9.2p180 with rails 3.2.3.

That said, I'm not sure why this is happening. Adding require 'rails' in the unit tests doesn't cause any failures, so I need to find a way to test the code inside the rails environment. I don't use rails very often, so I'll need to do some searching to find the best way to do this - here's what I found so far.

I should have more time to go through this during the weekend.

Thanks,
Bill

Thanks Bill! Very much appreciated. I've tried tinkering around with it for a few hours yesterday, I think it might have something to do with the fact that rails changes the Time class injecting its own methods in. I'm not 100% certain though, in the end I couldn't get it going :/. Thanks for your help on this :)

I can also confirm this bug happening while using ruby 1.9.3p194, Rails 3.2.5 and finance 1.1.2
The examples work fine while running as a standalone ruby file or in irb, but it doesn't work in rails console or in rails app, here is the error:
Math::DomainError: Zero or negative argument for log thrown from cashflows.rb:118
This bug is also related to #7

I get

#Class:0x000000063a0ba0 can't be coerced into BigDecimal

So, interestingly, I see this problem as well, but using the 'flt' directly (rather than through the 'finance' gem) and only on Linux for some reason (on OSX it's fine)

Specifically, these values are an example of what triggers it:

DecNum('15.47928602931972642303350358') * BigDecimal('500.00000000000000000000')

And just like @MoustafaEid, I can't reproduce from within a Rails console

So, for me at least it seems like a Ruby build version problem. Upgraded from 1.9.3-p125 to 1.9.3-p194 and it went away. Worth giving a shot.

@derekyau @MoustafaEid @wmilesn @suan

This issue is now quite old :) Is it still a live problem for anyone, and does it require further investigation?