# Float equality

**URL:** <https://rubytalk.org/t/float-equality/49867>\
**Category:** ruby-talk\
**Created:** [27 October 2008 16:50 UTC](https://rubytalk.org/t/float-equality/49867 "2008-10-27T16:50:29Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![guille\_lists](https://avatars.discourse-cdn.com/v4/letter/g/c89c15/32.png) [@guille\_lists](https://rubytalk.org/u/guille_lists)\
**Post date:** [27 October 2008 16:50 UTC](https://rubytalk.org/t/float-equality/49867/1 "2008-10-27T16:50:29Z")

</div>

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.

guille

PD: I'm using ruby 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0]

---

<div class="post-metadata">

**Author:** ![Todd\_Benson](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@Todd\_Benson](https://rubytalk.org/u/Todd_Benson)\
**Post date:** [27 October 2008 17:09 UTC](https://rubytalk.org/t/float-equality/49867/2 "2008-10-27T17:09:46Z")

</div>

Most people will point you to this:

> **[Floating-point arithmetic](https://en.wikipedia.org/wiki/Floating_point_arithmetic)**
>
> In computing, floating-point arithmetic (FP) is arithmetic that represents subsets of real numbers using an integer with a fixed precision, called the significand, scaled by an integer exponent of a fixed base.
> Numbers of this form are called floating-point numbers.: 3 : 10 
> For example, 12.345 is a floating-point number in base ten with five digits of precision:
> However, unlike 12.345, 12.3456 is not a floating-point number in base ten with five digits of precision—it needs six digits of pre...

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

> **···**
>
> On Mon, Oct 27, 2008 at 11:50 AM, guille lists \<guilledist@gmail.com\> 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.

---

<div class="post-metadata">

**Author:** ![The\_Higgs\_bozo](https://avatars.discourse-cdn.com/v4/letter/t/57b2e6/32.png) [@The\_Higgs\_bozo](https://rubytalk.org/u/The_Higgs_bozo)\
**Post date:** [27 October 2008 17:10 UTC](https://rubytalk.org/t/float-equality/49867/3 "2008-10-27T17:10:25Z")

</div>

guille lists wrote:

> I'm a bit lost here, shouldn't (0.1) and (1 - 0.9) be equals regarding  
> the == operator?

Nope. Floating-point is always inexact. This is a computer thing, not  
a Ruby thing. The issue applies to all languages everywhere which use  
floating point.

Here you can see the two values are slightly different:

irb(main):001:0\> 1 - 0.9 == 0.1  
=\> false  
irb(main):002:0\> [1 - 0.9].pack("D")  
=\> "\230\231\231\231\231\231\271?"  
irb(main):003:0\> [0.1].pack("D")  
=\> "\232\231\231\231\231\231\271?"

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Sebastian\_Hungereck1](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastian\_Hungereck1](https://rubytalk.org/u/Sebastian_Hungereck1)\
**Post date:** [27 October 2008 17:15 UTC](https://rubytalk.org/t/float-equality/49867/4 "2008-10-27T17:15:32Z")

</div>

guille lists wrote:

> I'm a bit lost here, shouldn't (0.1) and (1 - 0.9) be equals regarding  
> the == operator?

No. The result of 1 - 0.9 using floating point math is not actually 0.1. In  
irb it is displayed as 0.1, but that's only because Float#inspect rounds.  
Using printf you can see that the result of 1-0.9 actually is 0.09999lots:

> > printf "%.30f", 1-0.9

0.099999999999999977795539507497  
0.1 itself isn't actually 0.1 either - it's  
0.100000000000000005551115123126...  
Because of this you should not check two floats for equality (usually you want  
to check for a delta or not use floats at all). This is so because of the  
inherent inaccuracy of floating point maths. See this for more information:  
[http://docs.sun.com/source/806-3568/ncg\_goldberg.html](http://docs.sun.com/source/806-3568/ncg_goldberg.html)

HTH,  
Sebastian

> **···**
>
> --  
> Jabber: sepp2k@jabber.org  
> ICQ: 205544826

---

<div class="post-metadata">

**Author:** ![Matthew\_Moss](https://avatars.discourse-cdn.com/v4/letter/m/91b2a8/32.png) [@Matthew\_Moss](https://rubytalk.org/u/Matthew_Moss)\
**Post date:** [27 October 2008 17:51 UTC](https://rubytalk.org/t/float-equality/49867/5 "2008-10-27T17:51:50Z")

</div>

> 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... 🙂

Ummm... because 1/9 + 8/9 == (1 + 8)/9 == 9/9 == 1 ?

And... because 0.999... == 1?

&nbsp;&nbsp;&nbsp;&nbsp;x = 0.999...  
&nbsp;&nbsp;10x = 9.999...  
&nbsp;&nbsp;(10x - x) = 9.999... - 0.999...  
&nbsp;&nbsp;&nbsp;9x = 9  
&nbsp;&nbsp;&nbsp;&nbsp;x = 1

---

<div class="post-metadata">

**Author:** ![Tim\_Pease](https://avatars.discourse-cdn.com/v4/letter/t/3e96dc/32.png) [@Tim\_Pease](https://rubytalk.org/u/Tim_Pease)\
**Post date:** [27 October 2008 20:10 UTC](https://rubytalk.org/t/float-equality/49867/6 "2008-10-27T20:10:41Z")

</div>

I figured it out once, but I can't remember precisely how it worked.

TwP

> **···**
>
> On Mon, Oct 27, 2008 at 11:09 AM, Todd Benson \<caduceass@gmail.com\> wrote:
> 
> > 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... 🙂

---

<div class="post-metadata">

**Author:** ![John\_W\_Kennedy](https://avatars.discourse-cdn.com/v4/letter/j/c77e96/32.png) [@John\_W\_Kennedy](https://rubytalk.org/u/John_W_Kennedy)\
**Post date:** [28 October 2008 02:49 UTC](https://rubytalk.org/t/float-equality/49867/7 "2008-10-28T02:49:32Z")

</div>

The Higgs bozo wrote:

> guille lists wrote:
> 
> > I'm a bit lost here, shouldn't (0.1) and (1 - 0.9) be equals regarding  
> > the == operator?
> 
> Nope. Floating-point is always inexact.

What you mean is that binary floating point inexactly represents decimal fractions.

> This is a computer thing, not a Ruby thing. The issue applies to all languages everywhere which use floating point.

...binary floating point.

> **···**
>
> --  
> John W. Kennedy  
> &nbsp;&nbsp;"I want everybody to be smart. As smart as they can be. A world of ignorant people is too dangerous to live in."  
> &nbsp;&nbsp;&nbsp;-- Garson Kanin. "Born Yesterday"

---

<div class="post-metadata">

**Author:** ![guille\_lists](https://avatars.discourse-cdn.com/v4/letter/g/c89c15/32.png) [@guille\_lists](https://rubytalk.org/u/guille_lists)\
**Post date:** [27 October 2008 19:31 UTC](https://rubytalk.org/t/float-equality/49867/8 "2008-10-27T19:31:06Z")

</div>

Thanks a lot for the answers and references, and sorry for not having  
check deeper on google  
([http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/9655](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/9655)).

So I guess that if one wants to work for example with float numbers in  
the range [0,1], the best way to do it is by normalising from an  
integer interval depending on the precision you want, say [0,100] for  
two decimal digits precision, and so on. Is there any other better  
approach? Does the use of BigDecimal impose a severe penalty on performance?

guille

---

<div class="post-metadata">

**Author:** ![The\_Higgs\_bozo](https://avatars.discourse-cdn.com/v4/letter/t/57b2e6/32.png) [@The\_Higgs\_bozo](https://rubytalk.org/u/The_Higgs_bozo)\
**Post date:** [27 October 2008 19:48 UTC](https://rubytalk.org/t/float-equality/49867/9 "2008-10-27T19:48:13Z")

</div>

Matthew Moss wrote:

> > 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... 🙂
> 
> Ummm... because 1/9 + 8/9 == (1 + 8)/9 == 9/9 == 1 ?
> 
> And... because 0.999... == 1?
> 
> &nbsp;&nbsp;&nbsp;&nbsp;x = 0.999...  
> &nbsp;&nbsp;10x = 9.999...  
> &nbsp;&nbsp;(10x - x) = 9.999... - 0.999...  
> &nbsp;&nbsp;&nbsp;9x = 9  
> &nbsp;&nbsp;&nbsp;&nbsp;x = 1

While your answer is correct, you cannot subtract infinities as shown  
in your proof. Look at this:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x == 1 - 1 + 1 - 1 + 1 - 1 + 1 - ...  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x == 1 - 1 + 1 - 1 + 1 - 1 + ...

> **···**
>
> ----------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2x == 1 + 0 + 0 + 0 + 0 + 0 + 0 + ...  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x == 0.5
> 
> Does x == 0.5? No, because x was never a number in the first place  
> because the given series does not converge. Your proof appears to  
> work because you've already assumed 0.99999... converges, but that is  
> what you are trying to prove.
> 
> P.S. Euler thought the answer was x == 0.5.
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Bilyk\_Alex](https://avatars.discourse-cdn.com/v4/letter/b/59ef9b/32.png) [@Bilyk\_Alex](https://rubytalk.org/u/Bilyk_Alex)\
**Post date:** [27 October 2008 20:29 UTC](https://rubytalk.org/t/float-equality/49867/10 "2008-10-27T20:29:59Z")

</div>

Mathematically 1.(0) is the same thing as 0.(9). Computers simply represent this to whatever precision they can.

> **···**
>
> -----Original Message-----  
> From: Tim Pease [mailto:tim.pease@gmail.com]  
> Sent: Monday, October 27, 2008 1:11 PM  
> To: ruby-talk ML  
> Subject: Re: float equality
> 
> On Mon, Oct 27, 2008 at 11:09 AM, Todd Benson \<caduceass@gmail.com\> wrote:
> 
> > 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... 🙂
> 
> I figured it out once, but I can't remember precisely how it worked.
> 
> TwP

---

<div class="post-metadata">

**Author:** ![Axel\_Etzold](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@Axel\_Etzold](https://rubytalk.org/u/Axel_Etzold)\
**Post date:** [27 October 2008 20:13 UTC](https://rubytalk.org/t/float-equality/49867/11 "2008-10-27T20:13:32Z")

</div>

-------- Original-Nachricht --------

> Datum: Tue, 28 Oct 2008 04:31:06 +0900  
> Von: "guille lists" \<guilledist@gmail.com\>  
> An: ruby-talk@ruby-lang.org  
> Betreff: Re: float equality

> Thanks a lot for the answers and references, and sorry for not having  
> check deeper on google  
> ([http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/9655\](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/9655%5C)).
> 
> So I guess that if one wants to work for example with float numbers in  
> the range [0,1], the best way to do it is by normalising from an  
> integer interval depending on the precision you want, say [0,100] for  
> two decimal digits precision, and so on. Is there any other better  
> approach? Does the use of BigDecimal impose a severe penalty on  
> performance?
> 
> guille

Dear guille,

you could use some approximate equality check:

class Float  
&nbsp;&nbsp;&nbsp;def approx\_equal?(other,threshold)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (self-other).abs\<threshold # "\<" not exact either 😉  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end  
end

a=0.1  
b=1.0-0.9  
threshold=10\*\*(-5)

p a.approx\_equal?(b,threshold)

Best regards,

Axel

> **···**
>
> --  
> Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten  
> Browser-Versionen downloaden: [http://www.gmx.net/de/go/browser](http://www.gmx.net/de/go/browser)

---

<div class="post-metadata">

**Author:** ![Tim\_Pease](https://avatars.discourse-cdn.com/v4/letter/t/3e96dc/32.png) [@Tim\_Pease](https://rubytalk.org/u/Tim_Pease)\
**Post date:** [27 October 2008 20:40 UTC](https://rubytalk.org/t/float-equality/49867/12 "2008-10-27T20:40:27Z")

</div>

> Mathematically 1.(0) is the same thing as 0.(9). Computers simply represent this to whatever precision they can.

Hmmm ... my humor is a little to obtuse today. I was hoping the word "precisely" would cause a mental link to the word "precision" which is what this problem is all about -- precision and computer representations of floating point values.

Alas. I'll stick with my day job of writing software.

Blessings,  
TwP

> **···**
>
> On Oct 27, 2008, at 2:29 PM, Bilyk, Alex wrote:
> 
> > -----Original Message-----  
> > From: Tim Pease [mailto:tim.pease@gmail.com]  
> > Sent: Monday, October 27, 2008 1:11 PM  
> > To: ruby-talk ML  
> > Subject: Re: float equality
> > 
> > On Mon, Oct 27, 2008 at 11:09 AM, Todd Benson \<caduceass@gmail.com\> \> wrote:
> > 
> > > 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... 🙂
> > 
> > I figured it out once, but I can't remember precisely how it worked.
> > 
> > TwP

---

<div class="post-metadata">

**Author:** ![Matthew\_Moss](https://avatars.discourse-cdn.com/v4/letter/m/91b2a8/32.png) [@Matthew\_Moss](https://rubytalk.org/u/Matthew_Moss)\
**Post date:** [27 October 2008 20:56 UTC](https://rubytalk.org/t/float-equality/49867/13 "2008-10-27T20:56:57Z")

</div>

But 0.999... does converge, while 1 - 1 + 1 - 1 +... does not.

> **···**
>
> On Oct 27, 2008, at 2:48 PM, The Higgs bozo wrote:
> 
> > Matthew Moss wrote:
> > 
> > > > 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... 🙂
> > > 
> > > Ummm... because 1/9 + 8/9 == (1 + 8)/9 == 9/9 == 1 ?
> > > 
> > > And... because 0.999... == 1?
> > > 
> > > &nbsp;&nbsp;&nbsp;x = 0.999...  
> > > 10x = 9.999...  
> > > (10x - x) = 9.999... - 0.999...  
> > > &nbsp;&nbsp;9x = 9  
> > > &nbsp;&nbsp;&nbsp;x = 1
> > 
> > While your answer is correct, you cannot subtract infinities as shown  
> > in your proof. Look at this:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x == 1 - 1 + 1 - 1 + 1 - 1 + 1 - ...  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x == 1 - 1 + 1 - 1 + 1 - 1 + ...  
> > ----------------------------------------  
> > &nbsp;&nbsp;&nbsp;&nbsp;2x == 1 + 0 + 0 + 0 + 0 + 0 + 0 + ...  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x == 0.5
> > 
> > Does x == 0.5? No, because x was never a number in the first place  
> > because the given series does not converge. Your proof appears to  
> > work because you've already assumed 0.99999... converges, but that is  
> > what you are trying to prove.

---

<div class="post-metadata">

**Author:** ![Matthew\_Moss](https://avatars.discourse-cdn.com/v4/letter/m/91b2a8/32.png) [@Matthew\_Moss](https://rubytalk.org/u/Matthew_Moss)\
**Post date:** [27 October 2008 21:01 UTC](https://rubytalk.org/t/float-equality/49867/14 "2008-10-27T21:01:53Z")

</div>

> > > &nbsp;&nbsp;x = 0.999...  
> > > 10x = 9.999...  
> > > (10x - x) = 9.999... - 0.999...  
> > > 9x = 9  
> > > &nbsp;&nbsp;x = 1
> > 
> > While your answer is correct, you cannot subtract infinities as shown  
> > in your proof. Look at this:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;x == 1 - 1 + 1 - 1 + 1 - 1 + 1 - ...  
> > &nbsp;&nbsp;&nbsp;&nbsp;x == 1 - 1 + 1 - 1 + 1 - 1 + ...  
> > ----------------------------------------  
> > &nbsp;&nbsp;&nbsp;2x == 1 + 0 + 0 + 0 + 0 + 0 + 0 + ...  
> > &nbsp;&nbsp;&nbsp;&nbsp;x == 0.5
> > 
> > Does x == 0.5? No, because x was never a number in the first place  
> > because the given series does not converge. Your proof appears to  
> > work because you've already assumed 0.99999... converges, but that is  
> > what you are trying to prove.
> 
> But 0.999... does converge, while 1 - 1 + 1 - 1 +... does not.

On re-reading, I see that you weren't so much questioning whether 0.999... converges, but that my proof uses circular reasoning. Yeah, I suppose that's correct.

Still, 0.999... does converge and is 1. Nyah! 😛 I just don't remember the better proof I once knew.
