From: Greg Hauptmann Date: 2008-11-08T16:44:53+09:00 Subject: Re: what's easiest way to compare a Float & BigDecimal (i.e. like a equals mechanism) that works - but my 6.5 number doesn't work - does the following also not work for you? >> a = BigDecimal.new('6.5') => # >> b = 6.5 => 6.5 >> a == b => false >> a.class => BigDecimal >> b.class => Float >> On Sat, Nov 8, 2008 at 5:03 PM, Michael Morin wrote: > Greg Hauptmann wrote: >> >> Hi, >> >> what's easiest way to compare a Float & BigDecimal (i.e. like a equals >> mechanism)? It seems that "==" does NOT work as they are different >> types. Is there a way to do a comparison without having to convert >> types? >> >> ?> a >> => # >>>> >>>> x >> >> => 6.5 >>>> >>>> a == x >> >> => false #<<== DIDN'T WORK >> >> tks >> > > > It works fine for me. If I do something like this: > > f = 0.1 > b = BigDecimal.new('0.1') > puts 'yes' if f == b > > It will return true. I've always stayed clear of the equality operator with > floating point numbers. Floating point cannot represent all numbers > exactly, so comparing it with something like a BigDecimal (which can > represent all numbers exactly) might be problematic. > >