Doing a simple test here and I''m not really sure why this isn''t passing, which makes me feel retarded. All that the amount method is doing is taking 10% from the argument. "#{@some_discount.amount(51)} == #{5.1}" => "5.1 == 5.1" "#{@some_discount.amount(51).class.name} == #{(5.1).class.name}" => "Float == Float" "#{@some_discount.amount(51) == 5.1}" => false What''s the deal? Thanks for your help. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
What you see is not what you get with floating point numbers. Have Ruby print out 20+ decimal places and you''ll see that there is in-fact a difference. When trying to compare floats, you need to use an epsilon and check that the two values are close enough to each other. e.g. x = 5.1 y = 5.1 epsilon = 0.001 abs(x - y) < epsilon Jason On 1/4/07, Ben Johnson <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Doing a simple test here and I''m not really sure why this isn''t passing, > which makes me feel retarded. All that the amount method is doing is > taking 10% from the argument. > > "#{@some_discount.amount(51)} == #{5.1}" > => "5.1 == 5.1" > > "#{@some_discount.amount(51).class.name} == #{(5.1).class.name}" > => "Float == Float" > > "#{@some_discount.amount(51) == 5.1}" > => false > > What''s the deal? > > Thanks for your help. > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ben Johnson wrote:> Doing a simple test here and I''m not really sure why this isn''t passing, > which makes me feel retarded. All that the amount method is doing is > taking 10% from the argument. > > "#{@some_discount.amount(51)} == #{5.1}" > => "5.1 == 5.1" > > "#{@some_discount.amount(51).class.name} == #{(5.1).class.name}" > => "Float == Float" > > "#{@some_discount.amount(51) == 5.1}" > => false > > What''s the deal? > > Thanks for your help.Floating point arithmetic cannot store some numbers exactly, and 5.1 is one of them. Try executing "#{51/10 == 5.1}" and see what you get. :] -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ben Johnson wrote:> What''s the deal? > > Thanks for your help.Ben - I''m fairly sure that your problem here is the way ruby (and computers in general) deal with floats. Floats are inherently imprecise and when you perform mathematical operations with them, this imprecision will become apparent. irb(main):001:0> a = 5.1 => 5.1 irb(main):002:0> b = 5.1 => 5.1 irb(main):003:0> a == b => true irb(main):004:0> x = 51 * 0.1 => 5.1 irb(main):005:0> y = 5.1 => 5.1 irb(main):006:0> x == y => false Hope this helps. -Drew -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jason Roelofs wrote:> What you see is not what you get with floating point numbers. Have Ruby > print out 20+ decimal places and you''ll see that there is in-fact a > difference. When trying to compare floats, you need to use an epsilon and > check that the two values are close enough to each other. e.g. > > x = 5.1 > y = 5.1 > epsilon = 0.001 > > abs(x - y) < epsilon > > JasonAssuming you are using the Ruby Test::Unit framework or something derived from it, the right assertion to use when comparing floats for equality is assert_in_delta, documented here: http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html#M002493 regards Justin Forder> On 1/4/07, Ben Johnson <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> >> >> Doing a simple test here and I''m not really sure why this isn''t passing, >> which makes me feel retarded. All that the amount method is doing is >> taking 10% from the argument. >> >> "#{@some_discount.amount(51)} == #{5.1}" >> => "5.1 == 5.1" >> >> "#{@some_discount.amount(51).class.name} == #{(5.1).class.name}" >> => "Float == Float" >> >> "#{@some_discount.amount(51) == 5.1}" >> => false >> >> What''s the deal? >> >> Thanks for your help. >> >> -- >> Posted via http://www.ruby-forum.com/. >> >> > >> > > > > > > ------------------------------------------------------------------------ > > No virus found in this incoming message. > Checked by AVG Free Edition. > Version: 7.1.409 / Virus Database: 268.16.4/615 - Release Date: 03/01/2007--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---