[rspec-users] Why can not a BigDecimal be compared to a Float via "==". How should I handle this???
Hi, Any suggestions on how to write an rspec expectation for equality when you get a BigDecimal back. I see that "big_decimal_variable.should == 123.23" doesn't work as the ruby BigDecimal comparison (via ==) to the same Float value gives false (not true as you'd expect). For background / as an example see below: ?> bd => # >> f => -323.03 >> f.class => Float >> bd.should == f Spec::Expectations::ExpectationNotMetError: expected: -323.03, got: # (using ==) from /opt/local/lib/ruby/gems/1.8/gems/rspec-1.1.11/lib/spec/expectations.rb:52:in `fail_with' from /opt/local/lib/ruby/gems/1.8/gems/rspec-1.1.11/lib/spec/matchers/operator_matcher.rb:46:in `fail_with_message' from /opt/local/lib/ruby/gems/1.8/gems/rspec-1.1.11/lib/spec/matchers/operator_matcher.rb:61:in `__delegate_method_missing_to_given' from /opt/local/lib/ruby/gems/1.8/gems/rspec-1.1.11/lib/spec/matchers/operator_matcher.rb:12:in `==' from (irb):73 >> bd == f => false >> f == bd => false thanks Greg ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] Why can not a BigDecimal be compared to a Float via "==". How should I handle this???
On Sun, Jan 11, 2009 at 12:17 AM, Greg Hauptmann < greg.hauptmann.r...@gmail.com> wrote: > > Any suggestions on how to write an rspec expectation for equality when you > get a BigDecimal back. I see that "big_decimal_variable.should == 123.23" > doesn't work as the ruby BigDecimal comparison (via ==) to the same Float > value gives false (not true as you'd expect). > How about bd.to_s.should == 123.23.to_s ? ///ark ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] Why can not a BigDecimal be compared to a Float via "==". How should I handle this???
I've gone with the following ai.amount.should == BigDecimal('-323.03') However I'm still a bit surprised that Ruby itself does allow a good "==" test between a Float and a BigDecimal. Perhaps there's a reason that I'm missing? On Sun, Jan 11, 2009 at 7:23 PM, Mark Wilden wrote: > On Sun, Jan 11, 2009 at 12:17 AM, Greg Hauptmann < > greg.hauptmann.r...@gmail.com> wrote: > >> >> Any suggestions on how to write an rspec expectation for equality when you >> get a BigDecimal back. I see that "big_decimal_variable.should == 123.23" >> doesn't work as the ruby BigDecimal comparison (via ==) to the same Float >> value gives false (not true as you'd expect). >> > > How about bd.to_s.should == 123.23.to_s ? > > ///ark > > ___ > rspec-users mailing list > rspec-users@rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
[rspec-users] getting started - very much a beginner
A hope my ignorance is acceptable here. I have, to this point, programmed in Ruby mostly using my awareness of procedural programming; I've written a small number of very useful programs for myself. I'm starting to use classes now - just starting. I have a small project I'm working on - setting up a database based on the directed acyclic graph (DAG) model (using Ruby only). I'm been studying test-directed development, which led me to BDD, at which point I got really excited. I truly want to go forward with learning cucumber, and rspec. So here I am trying to started using cucumber, and I'm having a little trouble. The documentation in the cucumber wiki isn't, to my poverty stricken perception, at all procedural, although individual pages seem quite lucid and accessible. After reading and rereading (I've looked at every page, and most several times), it appears that the "start" page is "Cucumber Backgrounder". The problem is that this assumes you are working with Rails, about which I know little and desire to know less. I get stuck, on that page, at the phrase... ==When you run "script/generate cucumber" == Huh? Is this something you do in Rails? Then there's this -- - Running script/generate cucumber adds this layout to the existing structure: ||-- features | |-- step_definitions | | `-- webrat_steps.rb | `-- support | `-- env.rb | We are now ready to begin testing with cucumber. - Well, maybe for some people...but not for me. I simply cannot see what to do first. Do I manually create some kind of directory structure and fill it with...well, with what? Feature description files? And then what do I do? I'm guessing that this is covered by the "Running features" page. Am I right? Is there documentation somewhere that talks about using cucumber to launch BDD using a non-Rails Ruby program? I think that's what I really need. I cannot quite get past this stuck place, so any help at all would be much appreciated. I'm sorry to bother folks with this - I'm sure it's all quite obvious to you; I wish it were to me, and hope that it soon will be. Hopefully, Tom -- ~ Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist Bellingham, Washington, U.S.A: (360) 920-1226 << t...@tomcloyd.com >> (email) << TomCloyd.com >> (website) << sleightmind.wordpress.com >> (mental health weblog) ~ ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
[rspec-users] maybe I 'get it' after all
I hope I'm not the only one in the world who posts to a discussion list only to (maybe) get the answer to a question moments after hitting posting. Sigh. Looking again, in the cucumber wiki page "Running Features", I had a sense that I maybe I DID know what to do, went to /examples/dos_line_endings in the cucumber gems installation on my machine, and entered to the CLI - ~$ cucumber features/dos_line_endings.feature After I stopped laughing at the feature description text (I too love Linux), I have to admit that this output simply works for me. Whew. So...my sense of "what to do" now, is to write *.feature files, with corresponding step definition files, then...well, there's lots of what-to-do info very lucidly written in the wiki. I much enjoy reading it - although I wish the Rails stuff was factored out, because it's of no use if you're not doing Rails, and I think I'm going to have to try to translate some of it, and will likely do it badly... If someone wants to redirect me in any way, I'll give very close attention to their advice. Meanwhile, I'm feeling like I can go forward with this and I'll be learning a great deal. This sure looks like fun...and very very useful, in the long run. I'm endlessly grateful for the hard work of others in the Ruby world, from which I benefit so much. t. -- ~ Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist Bellingham, Washington, U.S.A: (360) 920-1226 << t...@tomcloyd.com >> (email) << TomCloyd.com >> (website) << sleightmind.wordpress.com >> (mental health weblog) ~ ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] maybe I 'get it' after all
> I hope I'm not the only one in the world who posts to a discussion list > only to (maybe) get the answer to a question moments after hitting > posting. Sigh. Hey, that's my trick! Get your own! 2009/1/11 Tom Cloyd > I hope I'm not the only one in the world who posts to a discussion list > only to (maybe) get the answer to a question moments after hitting > posting. Sigh. > > Looking again, in the cucumber wiki page "Running Features", I had a > sense that I maybe I DID know what to do, went to > /examples/dos_line_endings in the cucumber gems installation on my > machine, and entered to the CLI - > > ~$ cucumber features/dos_line_endings.feature > > After I stopped laughing at the feature description text (I too love > Linux), I have to admit that this output simply works for me. Whew. > > So...my sense of "what to do" now, is to write *.feature files, with > corresponding step definition files, then...well, there's lots of > what-to-do info very lucidly written in the wiki. I much enjoy reading > it - although I wish the Rails stuff was factored out, because it's of > no use if you're not doing Rails, and I think I'm going to have to try > to translate some of it, and will likely do it badly... > > If someone wants to redirect me in any way, I'll give very close > attention to their advice. Meanwhile, I'm feeling like I can go > forward with this and I'll be learning a great deal. This sure looks > like fun...and very very useful, in the long run. > > I'm endlessly grateful for the hard work of others in the Ruby world, > from which I benefit so much. > > t. > > -- > > ~ > Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist > Bellingham, Washington, U.S.A: (360) 920-1226 > << t...@tomcloyd.com >> (email) > << TomCloyd.com >> (website) > << sleightmind.wordpress.com >> (mental health weblog) > ~ > > > ___ > rspec-users mailing list > rspec-users@rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] getting started - very much a beginner
On Sun, Jan 11, 2009 at 1:26 PM, Tom Cloyd wrote: > A hope my ignorance is acceptable here. I have, to this point, > programmed in Ruby mostly using my awareness of procedural programming; > I've written a small number of very useful programs for myself. I'm starting > to use classes now - just starting. > > I have a small project I'm working on - setting up a database based on the > directed acyclic graph (DAG) model (using Ruby only). I'm been studying > test-directed development, which led me to BDD, at which point I got really > excited. I truly want to go forward with learning cucumber, and rspec. > > So here I am trying to started using cucumber, and I'm having a little > trouble. The documentation in the cucumber wiki isn't, to my poverty > stricken perception, at all procedural, although individual pages seem > quite lucid and accessible. After reading and rereading (I've looked at > every page, and most several times), it appears that the "start" page is > "Cucumber Backgrounder". The problem is that this assumes you are working > with Rails, about which I know little and desire to know less. I get stuck, > on that page, at the phrase... > > ==When you run "script/generate cucumber" == > > Huh? Is this something you do in Rails? > Yes, this is Rails only. I recommend you take a look at the examples/i18n folder. You'll find some really simplistic Cucumber code that is simple Ruby. No Rails or other complex material. Aslak > Then there's this -- > > - > > Running script/generate cucumber adds this layout to the existing > structure: > > ||-- features > | |-- step_definitions > | | `-- webrat_steps.rb > | `-- support > | `-- env.rb > | > > We are now ready to begin testing with cucumber. > > - > > Well, maybe for some people...but not for me. > > I simply cannot see what to do first. Do I manually create some kind of > directory structure and fill it with...well, with what? Feature > description files? And then what do I do? I'm guessing that this is > covered by the "Running features" page. Am I right? > > Is there documentation somewhere that talks about using cucumber to > launch BDD using a non-Rails Ruby program? I think that's what I really > need. I cannot quite get past this stuck place, so any help at all would > be much appreciated. I'm sorry to bother folks with this - I'm sure it's > all quite obvious to you; I wish it were to me, and hope that it soon > will be. > > Hopefully, > > Tom > > -- > > ~ > Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist > Bellingham, Washington, U.S.A: (360) 920-1226 > << t...@tomcloyd.com >> (email) > << TomCloyd.com >> (website) > << sleightmind.wordpress.com >> (mental health weblog) > ~ > > > ___ > rspec-users mailing list > rspec-users@rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] Is the Fit Table documentation correct?
On 11 Jan 2009, at 01:22, MarkMT wrote: Thanks Matt. I have now found the page that deals with scenario outlines (GH is back up) - http://wiki.github.com/aslakhellesoy/cucumber/scenario-outlines so that is now pretty clear. Might be worth a note in the fit table page referring to that. The wiki is open to all... be our guest! Seriously it's terrific if first-timers can polish these pages, as they're often written by those of us who are quite immersed in the product and consequently find it harder to put on the 'newbie hat' to proof-read documentation. cheers, Matt Wynne http://blog.mattwynne.net http://www.songkick.com ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] maybe I 'get it' after all
On Sun, Jan 11, 2009 at 1:56 PM, Tom Cloyd wrote: > I hope I'm not the only one in the world who posts to a discussion list > only to (maybe) get the answer to a question moments after hitting > posting. Sigh. > > Looking again, in the cucumber wiki page "Running Features", I had a > sense that I maybe I DID know what to do, went to > /examples/dos_line_endings in the cucumber gems installation on my > machine, and entered to the CLI - > > ~$ cucumber features/dos_line_endings.feature > > After I stopped laughing at the feature description text (I too love > Linux), I have to admit that this output simply works for me. Whew. > > So...my sense of "what to do" now, is to write *.feature files, with > corresponding step definition files, then...well, there's lots of > what-to-do info very lucidly written in the wiki. I much enjoy reading > it - although I wish the Rails stuff was factored out, because it's of > no use if you're not doing Rails, and I think I'm going to have to try > to translate some of it, and will likely do it badly... > Good point. Keeping Rails-related content apart from the more general Cucumber docs has been my goal since I started. However, several contributors to the Wiki are using Rails and it seems they often think that everybody is using Rails. Or maybe they are so ingrained in the Rails way of doing things that it's hard to describe non-Rails environments. Keep this in mind folks! Aslak > > If someone wants to redirect me in any way, I'll give very close > attention to their advice. Meanwhile, I'm feeling like I can go > forward with this and I'll be learning a great deal. This sure looks > like fun...and very very useful, in the long run. > > I'm endlessly grateful for the hard work of others in the Ruby world, > from which I benefit so much. > > t. > > -- > > ~ > Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist > Bellingham, Washington, U.S.A: (360) 920-1226 > << t...@tomcloyd.com >> (email) > << TomCloyd.com >> (website) > << sleightmind.wordpress.com >> (mental health weblog) > ~ > > > ___ > rspec-users mailing list > rspec-users@rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] Why can not a BigDecimal be compared to a Float via "==". How should I handle this???
On Sun, Jan 11, 2009 at 4:05 AM, Greg Hauptmann wrote: > I've gone with the following > ai.amount.should == BigDecimal('-323.03') > However I'm still a bit surprised that Ruby itself does allow a good "==" > test between a Float and a BigDecimal. Perhaps there's a reason that I'm > missing? Very telling is this: >> require 'bigdecimal' => true >> BigDecimal.new(333.0) == 333.0 TypeError: can't convert Float into String from (irb):4:in `new' from (irb):4 As for why, I think you'll get some good insights if you post the ruby-lang mailing list, but I can take shot. BigDecimal has explicit precision. Float does not. Imagine the developer at the bank explaining that the thousands of dollars discrepancy last year was due to an average miscalculation of 0.5 per transaction because sometimes the code used BigDecimal, and sometimes it used Float. Personally, I think this seeming annoyance is actually a good thing. FWIW, David > > On Sun, Jan 11, 2009 at 7:23 PM, Mark Wilden wrote: >> >> On Sun, Jan 11, 2009 at 12:17 AM, Greg Hauptmann >> wrote: >>> >>> Any suggestions on how to write an rspec expectation for equality when >>> you get a BigDecimal back. I see that "big_decimal_variable.should == >>> 123.23" doesn't work as the ruby BigDecimal comparison (via ==) to the same >>> Float value gives false (not true as you'd expect). >> >> How about bd.to_s.should == 123.23.to_s ? >> >> ///ark ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] Why can not a BigDecimal be compared to a Float via "==". How should I handle this???
On Sun, Jan 11, 2009 at 9:21 AM, David Chelimsky wrote: > On Sun, Jan 11, 2009 at 4:05 AM, Greg Hauptmann > wrote: > > I've gone with the following > > ai.amount.should == BigDecimal('-323.03') > > However I'm still a bit surprised that Ruby itself does allow a good "==" > > test between a Float and a BigDecimal. Perhaps there's a reason that I'm > > missing? > > Very telling is this: > > >> require 'bigdecimal' > => true > >> BigDecimal.new(333.0) == 333.0 > TypeError: can't convert Float into String >from (irb):4:in `new' >from (irb):4 > > As for why, I think you'll get some good insights if you post the > ruby-lang mailing list, but I can take shot. > > BigDecimal has explicit precision. Float does not. Imagine the > developer at the bank explaining that the thousands of dollars > discrepancy last year was due to an average miscalculation of 0.5 > per transaction because sometimes the code used BigDecimal, and > sometimes it used Float. > Even more telling is this: irb(main):001:0> 1.0 / 3.0 => 0.333 irb(main):002:0> (1.0 / 3.00) == 0.333 => false This has little to do with rspec or Ruby, and everything to do with floats. Floats are approximations, it's a mistake to thing of them as equivalent to the mathematical concept of real numbers, or even rational numbers. There are several issues here including 1. Floats are not infinite precision, they have a fixed number of bits or digits, this means that in-between any two consecutive real number which CAN be represented by a float, there are an infinite number of reals which cannot. 2. As is the case in decimal fractions, where some rational numbers such as 1/3 cannot be represented without an infinite number of decimal digits, there are similar values dependent on the base used for the float representation. There's a whole branch of computer science, Numerical Analysis, comprised in large part of understanding how Floats differ from the mathematical ideal. -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] Why can not a BigDecimal be compared to a Float via "==". How should I handle this???
it would be nice in one's project if you could basically say "use BigDecimal wherever you normally would have used a float", just to get the benefit but maintain ease of coding/readability etc - anyone know if this is possible? Would it be enough to override Float's initializer and return a BigDecimal instead? On Mon, Jan 12, 2009 at 4:53 AM, Rick DeNatale wrote: > On Sun, Jan 11, 2009 at 9:21 AM, David Chelimsky wrote: > >> On Sun, Jan 11, 2009 at 4:05 AM, Greg Hauptmann >> wrote: >> > I've gone with the following >> > ai.amount.should == BigDecimal('-323.03') >> > However I'm still a bit surprised that Ruby itself does allow a good >> "==" >> > test between a Float and a BigDecimal. Perhaps there's a reason that >> I'm >> > missing? >> >> Very telling is this: >> >> >> require 'bigdecimal' >> => true >> >> BigDecimal.new(333.0) == 333.0 >> TypeError: can't convert Float into String >>from (irb):4:in `new' >>from (irb):4 >> >> As for why, I think you'll get some good insights if you post the >> ruby-lang mailing list, but I can take shot. >> >> BigDecimal has explicit precision. Float does not. Imagine the >> developer at the bank explaining that the thousands of dollars >> discrepancy last year was due to an average miscalculation of 0.5 >> per transaction because sometimes the code used BigDecimal, and >> sometimes it used Float. >> > > Even more telling is this: > irb(main):001:0> 1.0 / 3.0 > => 0.333 > irb(main):002:0> (1.0 / 3.00) == 0.333 > => false > > This has little to do with rspec or Ruby, and everything to do with floats. > > Floats are approximations, it's a mistake to thing of them as equivalent to > the mathematical concept of real numbers, or even rational numbers. There > are several issues here including > > 1. Floats are not infinite precision, they have a fixed number of bits or > digits, this means that in-between any two consecutive real number which CAN > be represented by a float, there are an infinite number of reals which > cannot. > > 2. As is the case in decimal fractions, where some rational numbers such as > 1/3 cannot be represented without an infinite number of decimal digits, > there are similar values dependent on the base used for the float > representation. > > There's a whole branch of computer science, Numerical Analysis, comprised > in large part of understanding how Floats differ from the mathematical > ideal. > > > -- > Rick DeNatale > > Blog: http://talklikeaduck.denhaven2.com/ > Twitter: http://twitter.com/RickDeNatale > > ___ > rspec-users mailing list > rspec-users@rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > -- Greg http://blog.gregnet.org/ ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] Why can not a BigDecimal be compared to a Float via "==". How should I handle this???
This is a very bad idea, as you can break the whole runtime by doing this, as many internal classes use floating point math. You would also slow the world down, as operations using BigDecimals are some order of magnitude slower than pure floating point math. - Maurício Linhares http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/ (en) On Sun, Jan 11, 2009 at 6:33 PM, Greg Hauptmann wrote: > it would be nice in one's project if you could basically say "use > BigDecimal wherever you normally would have used a float", just to get the > benefit but maintain ease of coding/readability etc - anyone know if this is > possible? Would it be enough to override Float's initializer and return a > BigDecimal instead? > > > On Mon, Jan 12, 2009 at 4:53 AM, Rick DeNatale > wrote: >> >> On Sun, Jan 11, 2009 at 9:21 AM, David Chelimsky >> wrote: >>> >>> On Sun, Jan 11, 2009 at 4:05 AM, Greg Hauptmann >>> wrote: >>> > I've gone with the following >>> > ai.amount.should == BigDecimal('-323.03') >>> > However I'm still a bit surprised that Ruby itself does allow a good >>> > "==" >>> > test between a Float and a BigDecimal. Perhaps there's a reason that >>> > I'm >>> > missing? >>> >>> Very telling is this: >>> >>> >> require 'bigdecimal' >>> => true >>> >> BigDecimal.new(333.0) == 333.0 >>> TypeError: can't convert Float into String >>>from (irb):4:in `new' >>>from (irb):4 >>> >>> As for why, I think you'll get some good insights if you post the >>> ruby-lang mailing list, but I can take shot. >>> >>> BigDecimal has explicit precision. Float does not. Imagine the >>> developer at the bank explaining that the thousands of dollars >>> discrepancy last year was due to an average miscalculation of 0.5 >>> per transaction because sometimes the code used BigDecimal, and >>> sometimes it used Float. >> >> Even more telling is this: >> irb(main):001:0> 1.0 / 3.0 >> => 0.333 >> irb(main):002:0> (1.0 / 3.00) == 0.333 >> => false >> >> This has little to do with rspec or Ruby, and everything to do with >> floats. >> >> Floats are approximations, it's a mistake to thing of them as equivalent >> to the mathematical concept of real numbers, or even rational numbers. There >> are several issues here including >> >> 1. Floats are not infinite precision, they have a fixed number of bits or >> digits, this means that in-between any two consecutive real number which CAN >> be represented by a float, there are an infinite number of reals which >> cannot. >> >> 2. As is the case in decimal fractions, where some rational numbers such >> as 1/3 cannot be represented without an infinite number of decimal digits, >> there are similar values dependent on the base used for the float >> representation. >> >> There's a whole branch of computer science, Numerical Analysis, comprised >> in large part of understanding how Floats differ from the mathematical >> ideal. >> >> >> -- >> Rick DeNatale >> >> Blog: http://talklikeaduck.denhaven2.com/ >> Twitter: http://twitter.com/RickDeNatale >> >> ___ >> rspec-users mailing list >> rspec-users@rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users > > > > -- > Greg > http://blog.gregnet.org/ > > > > ___ > rspec-users mailing list > rspec-users@rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] Why can not a BigDecimal be compared to a Float via "==". How should I handle this???
2009-01-11 18:17, Greg Hauptmann: > Any suggestions on how to write an rspec expectation for equality when you > get a BigDecimal back. I see that "big_decimal_variable.should == > 123.23" If you register keywords "comparison" and "float", you should train yourself to cry out "delta" without even thinking. ;) Would this work for you? big_decimal_variable.should be_close(123.23, 0.005) -- Tero Tilus ## 050 3635 235 ## http://tero.tilus.net/ ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] Why can not a BigDecimal be compared to a Float via "==". How should I handle this???
On Sun, Jan 11, 2009 at 12:17 AM, Greg Hauptmann < greg.hauptmann.r...@gmail.com> wrote: > Any suggestions on how to write an rspec expectation for equality when you > get a BigDecimal back. I see that "big_decimal_variable.should == 123.23" > doesn't work as the ruby BigDecimal comparison (via ==) to the same Float > value gives false (not true as you'd expect). > Noting there are two decimal places in your data, I might suggest using the Money plugin. ///ark ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] Why can not a BigDecimal be compared to a Float via "==". How should I handle this???
ok - thanks - seems like if one remembers to use "BigDecimal('10.1')", then instead of 10.1 in your code you should be ok then. On Mon, Jan 12, 2009 at 6:45 AM, Maurício Linhares < mauricio.linha...@gmail.com> wrote: > This is a very bad idea, as you can break the whole runtime by doing > this, as many internal classes use floating point math. You would also > slow the world down, as operations using BigDecimals are some order of > magnitude slower than pure floating point math. > > - > Maurício Linhares > http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/ (en) > > > > On Sun, Jan 11, 2009 at 6:33 PM, Greg Hauptmann > wrote: > > it would be nice in one's project if you could basically say "use > > BigDecimal wherever you normally would have used a float", just to get > the > > benefit but maintain ease of coding/readability etc - anyone know if this > is > > possible? Would it be enough to override Float's initializer and return > a > > BigDecimal instead? > > > > > > On Mon, Jan 12, 2009 at 4:53 AM, Rick DeNatale > > wrote: > >> > >> On Sun, Jan 11, 2009 at 9:21 AM, David Chelimsky > >> wrote: > >>> > >>> On Sun, Jan 11, 2009 at 4:05 AM, Greg Hauptmann > >>> wrote: > >>> > I've gone with the following > >>> > ai.amount.should == BigDecimal('-323.03') > >>> > However I'm still a bit surprised that Ruby itself does allow a good > >>> > "==" > >>> > test between a Float and a BigDecimal. Perhaps there's a reason that > >>> > I'm > >>> > missing? > >>> > >>> Very telling is this: > >>> > >>> >> require 'bigdecimal' > >>> => true > >>> >> BigDecimal.new(333.0) == 333.0 > >>> TypeError: can't convert Float into String > >>>from (irb):4:in `new' > >>>from (irb):4 > >>> > >>> As for why, I think you'll get some good insights if you post the > >>> ruby-lang mailing list, but I can take shot. > >>> > >>> BigDecimal has explicit precision. Float does not. Imagine the > >>> developer at the bank explaining that the thousands of dollars > >>> discrepancy last year was due to an average miscalculation of 0.5 > >>> per transaction because sometimes the code used BigDecimal, and > >>> sometimes it used Float. > >> > >> Even more telling is this: > >> irb(main):001:0> 1.0 / 3.0 > >> => 0.333 > >> irb(main):002:0> (1.0 / 3.00) == 0.333 > >> => false > >> > >> This has little to do with rspec or Ruby, and everything to do with > >> floats. > >> > >> Floats are approximations, it's a mistake to thing of them as equivalent > >> to the mathematical concept of real numbers, or even rational numbers. > There > >> are several issues here including > >> > >> 1. Floats are not infinite precision, they have a fixed number of bits > or > >> digits, this means that in-between any two consecutive real number which > CAN > >> be represented by a float, there are an infinite number of reals which > >> cannot. > >> > >> 2. As is the case in decimal fractions, where some rational numbers such > >> as 1/3 cannot be represented without an infinite number of decimal > digits, > >> there are similar values dependent on the base used for the float > >> representation. > >> > >> There's a whole branch of computer science, Numerical Analysis, > comprised > >> in large part of understanding how Floats differ from the mathematical > >> ideal. > >> > >> > >> -- > >> Rick DeNatale > >> > >> Blog: http://talklikeaduck.denhaven2.com/ > >> Twitter: http://twitter.com/RickDeNatale > >> > >> ___ > >> rspec-users mailing list > >> rspec-users@rubyforge.org > >> http://rubyforge.org/mailman/listinfo/rspec-users > > > > > > > > -- > > Greg > > http://blog.gregnet.org/ > > > > > > > > ___ > > rspec-users mailing list > > rspec-users@rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > > ___ > rspec-users mailing list > rspec-users@rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > -- Greg http://blog.gregnet.org/ ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users