Greg Hauptmann
2008-Nov-07 23:18 UTC
[rspec-users] any tricks re using " eql(5.5)", but where 5.5 is a decimal not float?
-- Sent from my mobile device
Greg Hauptmann
2008-Nov-07 23:24 UTC
[rspec-users] any tricks re using " eql(5.5)", but where 5.5 is a decimal not float?
(woops - full email below) Hi, My rails migrations / mysql database uses Decimal and I''ve noticed in my rspec when I do a "...should eql(5.5)" that they''re failing as the expected result here is a float not a decimal. For example see extract below: expected 6.5, got #<BigDecimal:23d8284,''0.65E1'',8(8)> (using .eql?) What''s the best way of addressing this? Is there trick regarding the easiest way to address this? Thanks On Sat, Nov 8, 2008 at 9:18 AM, Greg Hauptmann <greg.hauptmann.ruby at gmail.com> wrote:> -- > Sent from my mobile device >
Mark Wilden
2008-Nov-07 23:47 UTC
[rspec-users] any tricks re using " eql(5.5)", but where 5.5 is a decimal not float?
On Fri, Nov 7, 2008 at 3:24 PM, Greg Hauptmann < greg.hauptmann.ruby at gmail.com> wrote:> > expected 6.5, got #<BigDecimal:23d8284,''0.65E1'',8(8)> (using .eql?) >How about just expecting BigDecimal(''5.5'')? ///ark -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20081107/19fc9829/attachment.html>
Mark Wilden
2008-Nov-07 23:48 UTC
[rspec-users] any tricks re using " eql(5.5)", but where 5.5 is a decimal not float?
There''s my C roots showing. :) That should be BigDecimal.new(''5.5''), of course. On Fri, Nov 7, 2008 at 3:47 PM, Mark Wilden <mark at mwilden.com> wrote:> On Fri, Nov 7, 2008 at 3:24 PM, Greg Hauptmann < > greg.hauptmann.ruby at gmail.com> wrote: > >> >> expected 6.5, got #<BigDecimal:23d8284,''0.65E1'',8(8)> (using .eql?) >> > > How about just expecting BigDecimal(''5.5'')? > > ///ark >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20081107/61d24d87/attachment.html>
Pat Maddox
2008-Nov-07 23:51 UTC
[rspec-users] any tricks re using " eql(5.5)", but where 5.5 is a decimal not float?
"Greg Hauptmann" <greg.hauptmann.ruby at gmail.com> writes: Do you mean BigDecimal? Anyway, you should probably be using == instead of eql.>> 5.5 == BigDecimal.new(''5.5'')=> true>> 5.5.eql? BigDecimal.new(''5.5'')=> false eql? is object identity and is generally not what you''re after. Pat
Mark Wilden
2008-Nov-08 00:09 UTC
[rspec-users] any tricks re using " eql(5.5)", but where 5.5 is a decimal not float?
On Fri, Nov 7, 2008 at 3:51 PM, Pat Maddox <pergesu at gmail.com> wrote:> "Greg Hauptmann" <greg.hauptmann.ruby at gmail.com> writes: > > Do you mean BigDecimal? Anyway, you should probably be using == instead > of eql. > > >> 5.5 == BigDecimal.new(''5.5'') > => true > >> 5.5.eql? BigDecimal.new(''5.5'') > => false > > eql? is object identity and is generally not what you''re after. >Yeah, but>> BigDecimal(''5.5'').eql? BigDecimal(''5.5'')=> true So it''s either creating value objects (like Symbol) or BigDecimal.eql? doesn''t do object equality. What''s interesting is that you can create BigDecimals this way (without calling new). You can do it with Strings, too.>> String(''asdf'')=> "asdf" But not everything:>> Hash(:a => 5)NoMethodError: undefined method `Hash'' for #<Object:0x3799c> from (irb):12 As Johnny Carson used to say, "I did not know that." ///ark -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20081107/db95fe98/attachment.html>
Greg Hauptmann
2008-Nov-08 01:00 UTC
[rspec-users] any tricks re using " eql(5.5)", but where 5.5 is a decimal not float?
in fact I guess I was fishing for an easier way than this...if one exists? or is ruby & rspec "eql()" just strict here in terms of types? On 11/7/08, Mark Wilden <mark at mwilden.com> wrote:> On Fri, Nov 7, 2008 at 3:24 PM, Greg Hauptmann < > greg.hauptmann.ruby at gmail.com> wrote: > >> >> expected 6.5, got #<BigDecimal:23d8284,''0.65E1'',8(8)> (using .eql?) >> > > How about just expecting BigDecimal(''5.5'')? > > ///ark >-- Sent from my mobile device
Zach Dennis
2008-Nov-08 01:48 UTC
[rspec-users] any tricks re using " eql(5.5)", but where 5.5 is a decimal not float?
On Fri, Nov 7, 2008 at 6:24 PM, Greg Hauptmann <greg.hauptmann.ruby at gmail.com> wrote:> (woops - full email below) > > Hi, > > My rails migrations / mysql database uses Decimal and I''ve noticed in > my rspec when I do a "...should eql(5.5)" that they''re failing as the > expected result here is a float not a decimal. For example see > extract below: > > expected 6.5, got #<BigDecimal:23d8284,''0.65E1'',8(8)> (using .eql?) > > What''s the best way of addressing this? Is there trick regarding the > easiest way to address this?You aren''t by chance using decimals to represent money are you? -- Zach Dennis http://www.continuousthinking.com http://www.mutuallyhuman.com
Greg Hauptmann
2008-Nov-08 02:12 UTC
[rspec-users] any tricks re using " eql(5.5)", but where 5.5 is a decimal not float?
yep - this was recommended on forums over float - why? On Sat, Nov 8, 2008 at 11:48 AM, Zach Dennis <zach.dennis at gmail.com> wrote:> On Fri, Nov 7, 2008 at 6:24 PM, Greg Hauptmann > <greg.hauptmann.ruby at gmail.com> wrote: >> (woops - full email below) >> >> Hi, >> >> My rails migrations / mysql database uses Decimal and I''ve noticed in >> my rspec when I do a "...should eql(5.5)" that they''re failing as the >> expected result here is a float not a decimal. For example see >> extract below: >> >> expected 6.5, got #<BigDecimal:23d8284,''0.65E1'',8(8)> (using .eql?) >> >> What''s the best way of addressing this? Is there trick regarding the >> easiest way to address this? > > You aren''t by chance using decimals to represent money are you? > > -- > Zach Dennis > http://www.continuousthinking.com > http://www.mutuallyhuman.com > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Zach Dennis
2008-Nov-08 02:18 UTC
[rspec-users] any tricks re using " eql(5.5)", but where 5.5 is a decimal not float?
On Fri, Nov 7, 2008 at 9:12 PM, Greg Hauptmann <greg.hauptmann.ruby at gmail.com> wrote:> yep - this was recommended on forums over float - why?You are in for a lot of headaches in you application resorting to decimal fields in the database, since it restores them as BigDecimals. It sucks to deal with BigDecimals in your app. I very highly recommend the Money library. I recommend installing it as a plugin and using the CollectiveIdea fork. http://github.com/collectiveidea/money/tree/master Even if all you do is take 20 minutes to check out the Money library in a dummy app, I think you will be glad you did. Zach> > On Sat, Nov 8, 2008 at 11:48 AM, Zach Dennis <zach.dennis at gmail.com> wrote: >> On Fri, Nov 7, 2008 at 6:24 PM, Greg Hauptmann >> <greg.hauptmann.ruby at gmail.com> wrote: >>> (woops - full email below) >>> >>> Hi, >>> >>> My rails migrations / mysql database uses Decimal and I''ve noticed in >>> my rspec when I do a "...should eql(5.5)" that they''re failing as the >>> expected result here is a float not a decimal. For example see >>> extract below: >>> >>> expected 6.5, got #<BigDecimal:23d8284,''0.65E1'',8(8)> (using .eql?) >>> >>> What''s the best way of addressing this? Is there trick regarding the >>> easiest way to address this? >> >> You aren''t by chance using decimals to represent money are you? >> >> -- >> Zach Dennis >> http://www.continuousthinking.com >> http://www.mutuallyhuman.com >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> >-- Zach Dennis http://www.continuousthinking.com http://www.mutuallyhuman.com
Pat Maddox
2008-Nov-08 04:10 UTC
[rspec-users] any tricks re using " eql(5.5)", but where 5.5 is a decimal not float?
"Greg Hauptmann" <greg.hauptmann.ruby at gmail.com> writes:> in fact I guess I was fishing for an easier way than this...if one > exists? or is ruby & rspec "eql()" just strict here in terms of > types? > > On 11/7/08, Mark Wilden <mark at mwilden.com> wrote: >> On Fri, Nov 7, 2008 at 3:24 PM, Greg Hauptmann < >> greg.hauptmann.ruby at gmail.com> wrote: >> >>> >>> expected 6.5, got #<BigDecimal:23d8284,''0.65E1'',8(8)> (using .eql?) >>> >> >> How about just expecting BigDecimal(''5.5'')? >> >> ///ark >>I''m not sure if you got my email before, but 6.5 =BigDecimal.new(''6.5''). So use == instead and you should be good. Pat
Greg Hauptmann
2008-Nov-08 05:37 UTC
[rspec-users] any tricks re using " eql(5.5)", but where 5.5 is a decimal not float?
thanks Pat - yes I missed it when reading your email - I''d been using ".eql" for everything....thanks On Sat, Nov 8, 2008 at 2:10 PM, Pat Maddox <pergesu at gmail.com> wrote:> "Greg Hauptmann" <greg.hauptmann.ruby at gmail.com> writes: > >> in fact I guess I was fishing for an easier way than this...if one >> exists? or is ruby & rspec "eql()" just strict here in terms of >> types? >> >> On 11/7/08, Mark Wilden <mark at mwilden.com> wrote: >>> On Fri, Nov 7, 2008 at 3:24 PM, Greg Hauptmann < >>> greg.hauptmann.ruby at gmail.com> wrote: >>> >>>> >>>> expected 6.5, got #<BigDecimal:23d8284,''0.65E1'',8(8)> (using .eql?) >>>> >>> >>> How about just expecting BigDecimal(''5.5'')? >>> >>> ///ark >>> > > I''m not sure if you got my email before, but 6.5 => BigDecimal.new(''6.5''). So use == instead and you should be good. > > Pat > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Greg Hauptmann
2008-Nov-08 06:32 UTC
[rspec-users] any tricks re using " eql(5.5)", but where 5.5 is a decimal not float?
very bizzare but whilst I needed to use this approach to fix one of my tests, I realised the test before was working without having to make it a big decimal - go figure :) -------------------------------- it "should return last IR when date > last interest rate" do @source_bank.ir(Time.now.to_date + 1.years).should == 8.5 # <== WORKED FINE end it "should return latest IR not greater than date specified when a date is passed" do @source_bank.ir(Time.now.to_date + 8.days).should =BigDecimal.new(''6.5'') # <== NEEDED TO USE BigDecimal here end -------------------------------- On Sat, Nov 8, 2008 at 3:37 PM, Greg Hauptmann <greg.hauptmann.ruby at gmail.com> wrote:> thanks Pat - yes I missed it when reading your email - I''d been using > ".eql" for everything....thanks > > On Sat, Nov 8, 2008 at 2:10 PM, Pat Maddox <pergesu at gmail.com> wrote: >> "Greg Hauptmann" <greg.hauptmann.ruby at gmail.com> writes: >> >>> in fact I guess I was fishing for an easier way than this...if one >>> exists? or is ruby & rspec "eql()" just strict here in terms of >>> types? >>> >>> On 11/7/08, Mark Wilden <mark at mwilden.com> wrote: >>>> On Fri, Nov 7, 2008 at 3:24 PM, Greg Hauptmann < >>>> greg.hauptmann.ruby at gmail.com> wrote: >>>> >>>>> >>>>> expected 6.5, got #<BigDecimal:23d8284,''0.65E1'',8(8)> (using .eql?) >>>>> >>>> >>>> How about just expecting BigDecimal(''5.5'')? >>>> >>>> ///ark >>>> >> >> I''m not sure if you got my email before, but 6.5 =>> BigDecimal.new(''6.5''). So use == instead and you should be good. >> >> Pat >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> >
Ashley Moran
2008-Nov-09 12:27 UTC
[rspec-users] any tricks re using " eql(5.5)", but where 5.5 is a decimal not float?
On Nov 08, 2008, at 2:18 am, Zach Dennis wrote:> <greg.hauptmann.ruby at gmail.com> wrote: >> yep - this was recommended on forums over float - why? > > You are in for a lot of headaches in you application resorting to > decimal fields in the database, since it restores them as BigDecimals. > It sucks to deal with BigDecimals in your app. I very highly recommend > the Money library. I recommend installing it as a plugin and using the > CollectiveIdea fork.Ha, I wrote the ActiveRecord patch so this was possible :) The reason for storing monetary values as SQL decimals is that they aren''t subject to rounding errors[1]. I used to work in the finance sector, and it was absolutely unacceptable to expose financial calculations to these errors. Hence I added support in ActiveRecord so we could use Rails; it was just not an option otherwise. The decision you have to make is between the extra effort now of using decimals in your code vs the extra effort later of explaining to your users why your calculations sometimes come out wrong. And that depends on how much your users value knowing that these figures are correct. Ashley [1] http://www.theregister.co.uk/2006/08/12/floating_point_approximation/ -- http://www.patchspace.co.uk/ http://aviewfromafar.net/
Greg Hauptmann
2008-Nov-10 00:21 UTC
[rspec-users] any tricks re using " eql(5.5)", but where 5.5 is a decimal not float?
Ashley - what''s you''re recommendation re using BigDecimal (which does work as you point out) and the Money gem (which it sounds like makes a dev''s life a bit easier)??? Is it worth trying to port an application from use of BigDecimal to Money gem??? On Sun, Nov 9, 2008 at 10:27 PM, Ashley Moran <ashley.moran at patchspace.co.uk> wrote:> > On Nov 08, 2008, at 2:18 am, Zach Dennis wrote: > >> <greg.hauptmann.ruby at gmail.com> wrote: >>> >>> yep - this was recommended on forums over float - why? >> >> You are in for a lot of headaches in you application resorting to >> decimal fields in the database, since it restores them as BigDecimals. >> It sucks to deal with BigDecimals in your app. I very highly recommend >> the Money library. I recommend installing it as a plugin and using the >> CollectiveIdea fork. > > > Ha, I wrote the ActiveRecord patch so this was possible :) > > The reason for storing monetary values as SQL decimals is that they aren''t > subject to rounding errors[1]. I used to work in the finance sector, and it > was absolutely unacceptable to expose financial calculations to these > errors. Hence I added support in ActiveRecord so we could use Rails; it was > just not an option otherwise. > > The decision you have to make is between the extra effort now of using > decimals in your code vs the extra effort later of explaining to your users > why your calculations sometimes come out wrong. And that depends on how > much your users value knowing that these figures are correct. > > Ashley > > > [1] http://www.theregister.co.uk/2006/08/12/floating_point_approximation/ > > -- > http://www.patchspace.co.uk/ > http://aviewfromafar.net/ > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Ashley Moran
2008-Nov-10 11:10 UTC
[rspec-users] any tricks re using " eql(5.5)", but where 5.5 is a decimal not float?
On Nov 10, 2008, at 12:21 am, Greg Hauptmann wrote:> Ashley - what''s you''re recommendation re using BigDecimal (which does > work as you point out) and the Money gem (which it sounds like makes a > dev''s life a bit easier)??? Is it worth trying to port an application > from use of BigDecimal to Money gem???Hi Greg I''d use the money gem if and only if it''s underlying implementation is BigDecimal or some other fixed-precision system. I''ve had a look over it and it appears to use integers to store values as small as 1/100 of the base unit. So as long as you don''t need to use anything smaller than that in calculations (ie, you only add, and never multiply, values - eg for tax calculations) it will be fine. I''ll try not to say too much more, we''re straying well away from RSpec territory... Ashley -- http://www.patchspace.co.uk/ http://aviewfromafar.net/
Apparently Analagous Threads
- anyone able to explain logic behind "rake spec" startup (e.g. db:test:prepare => abort_if_pending_migration => invoke environment => etc etc)
- stub_model() and ActiveRecord Associations
- should !=
- autospec is not picking latest changes
- wrong number of arguments (2 for 1)