Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > float equality

Reply
Thread Tools

float equality

 
 
guille lists
Guest
Posts: n/a
 
      10-27-2008
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]

 
Reply With Quote
 
 
 
 
Todd Benson
Guest
Posts: n/a
 
      10-27-2008
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

 
Reply With Quote
 
 
 
 
The Higgs bozo
Guest
Posts: n/a
 
      10-27-2008
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/.

 
Reply With Quote
 
Sebastian Hungerecker
Guest
Posts: n/a
 
      10-27-2008
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

HTH,
Sebastian
--
Jabber:
ICQ: 205544826

 
Reply With Quote
 
Matthew Moss
Guest
Posts: n/a
 
      10-27-2008
> 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 + /9 == 9/9 == 1 ?

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

x = 0.999...
10x = 9.999...
(10x - x) = 9.999... - 0.999...
9x = 9
x = 1




 
Reply With Quote
 
guille lists
Guest
Posts: n/a
 
      10-27-2008
Thanks a lot for the answers and references, and sorry for not having
check deeper on google
(http://blade.nagaokaut.ac.jp/cgi-bin...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

 
Reply With Quote
 
The Higgs bozo
Guest
Posts: n/a
 
      10-27-2008
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 + /9 == 9/9 == 1 ?
>
> And... because 0.999... == 1?
>
> x = 0.999...
> 10x = 9.999...
> (10x - x) = 9.999... - 0.999...
> 9x = 9
> x = 1


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

x == 1 - 1 + 1 - 1 + 1 - 1 + 1 - ...
x == 1 - 1 + 1 - 1 + 1 - 1 + ...
----------------------------------------
2x == 1 + 0 + 0 + 0 + 0 + 0 + 0 + ...
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/.

 
Reply With Quote
 
Tim Pease
Guest
Posts: n/a
 
      10-27-2008
On Mon, Oct 27, 2008 at 11:09 AM, Todd Benson <> 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

 
Reply With Quote
 
Axel Etzold
Guest
Posts: n/a
 
      10-27-2008

-------- Original-Nachricht --------
> Datum: Tue, 28 Oct 2008 04:31:06 +0900
> Von: "guille lists" <>
> An: ruby-
> 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...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


Dear guille,

you could use some approximate equality check:

class Float
def approx_equal?(other,threshold)
if (self-other).abs<threshold # "<" not exact either
return true
else
return false
end
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

 
Reply With Quote
 
Bilyk, Alex
Guest
Posts: n/a
 
      10-27-2008
Mathematically 1.(0) is the same thing as 0.(9). Computers simply represent=
this to whatever precision they can.

-----Original Message-----
From: Tim Pease [private.php?do=newpm&u=]
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 <> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Float precision and float equality Anton81 Python 26 12-11-2009 08:37 AM
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
float/double equality in template definition without rtti Alexander Block C++ 26 05-29-2004 02:37 PM
Testing (in)equality against a const float Michael Klatt C++ 2 05-21-2004 11:40 PM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57