some of my tests failed when comparing two floats. the comparison of: 1.1035 == 1.0035 + 0.1 evaluates to false - i do not really understand why. must depend on the value range, because 1.103 == 1.003 + 0.1 evaluates to true. would be great if anyone could help me getting this working and maybe giving an explanation why ruby do not handle the == operator for float as one would expect (coming from java). tested on: 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0] regards jan zimmek --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 23 Jan 2009, at 16:49, jzimmek wrote:> > some of my tests failed when comparing two floats. > > the comparison of: > > 1.1035 == 1.0035 + 0.1 > > evaluates to false - i do not really understand why. > > must depend on the value range, because > > 1.103 == 1.003 + 0.1basically because 0.1 cannot be exactly represented in binary. assert_in_delta is good for this sort of stuff. Fred> > > evaluates to true. > > would be great if anyone could help me getting this working and maybe > giving an explanation why ruby do not handle the == operator for float > as one would expect (coming from java). > > tested on: > > 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0] > > > regards > jan zimmek > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Crud, he beat me to it. You''re going to have to either change the logic or change the testing procedure for it. On Jan 23, 10:55 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 23 Jan 2009, at 16:49, jzimmek wrote: > > > > > some of my tests failed when comparing two floats. > > > the comparison of: > > > 1.1035 == 1.0035 + 0.1 > > > evaluates to false - i do not really understand why. > > > must depend on the value range, because > > > 1.103 == 1.003 + 0.1 > > basically because 0.1 cannot be exactly represented in binary. > > assert_in_delta is good for this sort of stuff. > > Fred > > > > > evaluates to true. > > > would be great if anyone could help me getting this working and maybe > > giving an explanation why ruby do not handle the == operator for float > > as one would expect (coming from java). > > > tested on: > > > 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0] > > > regards > > jan zimmek--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
thanks for clarification ... have found some further explanations/ solutions and will change my testing-logic. On Jan 23, 5:55 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 23 Jan 2009, at 16:49, jzimmek wrote: > > > > > some of my tests failed when comparing two floats. > > > the comparison of: > > > 1.1035 == 1.0035 + 0.1 > > > evaluates to false - i do not really understand why. > > > must depend on the value range, because > > > 1.103 == 1.003 + 0.1 > > basically because 0.1 cannot be exactly represented in binary. > > assert_in_delta is good for this sort of stuff. > > Fred > > > > > evaluates to true. > > > would be great if anyone could help me getting this working and maybe > > giving an explanation why ruby do not handle the == operator for float > > as one would expect (coming from java). > > > tested on: > > > 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0] > > > regards > > jan zimmek--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Quoting jzimmek <jan.zimmek-S0/GAf8tV78@public.gmane.org>:> > some of my tests failed when comparing two floats. > > the comparison of: > > 1.1035 == 1.0035 + 0.1 > > evaluates to false - i do not really understand why. > > must depend on the value range, because > > 1.103 == 1.003 + 0.1 >In general, exact comparisons of floating point numbers is a bad idea. A better idea is something along the lines of: (value1 - value2).abs <= epsilon epsilon may be a fixed value of a fraction of the values, maybe epsilon = (value1 + value2) / 100 i.e. 1/50 the average of the two. The best solution depends on the particular problem. Jeffrey --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---