From: Todd Benson Date: 2008-10-28T02:09:46+09:00 Subject: Re: float equality On Mon, Oct 27, 2008 at 11:50 AM, guille lists wrote: > Hi, > > sorry if this is a very naive question (I'm new to ruby), but I > haven't found an explication yet. When comparing to floats in ruby I > came across this: > >>> a = 0.1 > => 0.1 >>> b = 1 - 0.9 > => 0.1 >>> a == b > => false >>> a > b > => true >>> a < b > => false >>> a <=> b > => 1 > > I'm a bit lost here, shouldn't (0.1) and (1 - 0.9) be equals regarding > the == operator? I also found that for example 0.3 == (0.2 + 0.1) > returns false, etc. Most people will point you to this: http://en.wikipedia.org/wiki/Floating_point_arithmetic There are several ways around it (using BigDecimal, Rational, Integers, etc.) Totally off-topic, but has any one figured out exactly why 1/9 (0.111...) plus 8/9 (0.888...) is 1 instead of 0.999... :-) Todd