# Infinity (?!)

**URL:** <https://rubytalk.org/t/infinity/52>\
**Category:** ruby-talk\
**Created:** [3 June 2002 13:31 UTC](https://rubytalk.org/t/infinity/52 "2002-06-03T13:31:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_Brannan](https://avatars.discourse-cdn.com/v4/letter/p/76d3ee/32.png) [@Paul\_Brannan](https://rubytalk.org/u/Paul_Brannan)\
**Post date:** [3 June 2002 13:31 UTC](https://rubytalk.org/t/infinity/52/1 "2002-06-03T13:31:53Z")

</div>

I would expect that it is valid for two NaN’s to equal each other, but  
that because there are many different possible NaN’s, they need not  
necessarily equal each other.

Paul

> **···**
>
> On Sat, Jun 01, 2002 at 11:27:23AM +0900, Sean Russell wrote:
> 
> > Yukihiro Matsumoto wrote:
> > 
> > > ruby -ve ‘p((0/0.0) == (0/0.0))’  
> > > ruby 1.6.7 (2002-03-19) [i386-linux]  
> > > false
> > 
> > Strange:
> > 
> > ser@ender rexml/rexml% ruby -ve ‘p((0/0.0) == (0/0.0))’  
> > ruby 1.6.7 (2002-03-01) [i586-linux-gnu]  
> > true

---

<div class="post-metadata">

**Author:** ![Philip\_Mak2](https://avatars.discourse-cdn.com/v4/letter/p/c57346/32.png) [@Philip\_Mak2](https://rubytalk.org/u/Philip_Mak2)\
**Post date:** [3 June 2002 13:35 UTC](https://rubytalk.org/t/infinity/52/2 "2002-06-03T13:35:28Z")

</div>

I tried the exact same command on my machine and got an inconsistent  
result:

$ ruby -ve ‘p((0/0.0) == (0/0.0))’  
ruby 1.6.7 (2002-03-01) [i686-linux]  
false

> **···**
>
> On Mon, Jun 03, 2002 at 10:31:53PM +0900, Paul Brannan wrote:
> 
> > > ser@ender rexml/rexml% ruby -ve ‘p((0/0.0) == (0/0.0))’  
> > > ruby 1.6.7 (2002-03-01) [i586-linux-gnu]  
> > > true

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [3 June 2002 15:09 UTC](https://rubytalk.org/t/infinity/52/3 "2002-06-03T15:09:29Z")

</div>

Hi,

> **···**
>
> In message “Re: Infinity (?!)” on 02/06/03, Paul Brannan [pbrannan@atdesk.com](mailto:pbrannan@atdesk.com) writes:
> 
> > I would expect that it is valid for two NaN’s to equal each other, but  
> > that because there are many different possible NaN’s, they need not  
> > necessarily equal each other.
> 
> No, NaN should not be equal each other in any case, by its definition  
> (IEEE 754). I don’t know why Sean get “true”.
> 
> ```
> matz.
> 
> ```

---

<div class="post-metadata">

**Author:** ![Lyle\_Johnson1](https://avatars.discourse-cdn.com/v4/letter/l/bcef8e/32.png) [@Lyle\_Johnson1](https://rubytalk.org/u/Lyle_Johnson1)\
**Post date:** [3 June 2002 15:18 UTC](https://rubytalk.org/t/infinity/52/4 "2002-06-03T15:18:11Z")

</div>

> I would expect that it is valid for two NaN’s to equal each other, but  
> that because there are many different possible NaN’s, they need not  
> necessarily equal each other.

I thought that it was part of the _definition_ of NaN that it wasn’t equal  
to any other number, including itself. So (despite Sean’s result) I would  
expect:

```
ruby -ve 'p((0/0.0) == (0/0.0))'

```

to always return false.

---

<div class="post-metadata">

**Author:** ![SER1](https://avatars.discourse-cdn.com/v4/letter/s/3e96dc/32.png) [@SER1](https://rubytalk.org/u/SER1)\
**Post date:** [3 June 2002 17:38 UTC](https://rubytalk.org/t/infinity/52/5 "2002-06-03T17:38:25Z")

</div>

Yukihiro Matsumoto wrote:

> No, NaN should not be equal each other in any case, by its definition  
> (IEEE 754). I don’t know why Sean get “true”.

Since I appear to be the only person who sees this, I’m guessing that my  
Ruby installation is broken somehow.

> **···**
>
> –
> 
> > … How is a Texas tornado like a Tennessee divorce?  
> > \<|\> Someone’s going to lose a trailer…  
> > /|\   
> > /|

---

<div class="post-metadata">

**Author:** ![Rick\_Bradley1](https://avatars.discourse-cdn.com/v4/letter/r/2acd7d/32.png) [@Rick\_Bradley1](https://rubytalk.org/u/Rick_Bradley1)\
**Post date:** [3 June 2002 18:22 UTC](https://rubytalk.org/t/infinity/52/6 "2002-06-03T18:22:58Z")

</div>

- Sean Russell ([ser@germane-software.com](mailto:ser@germane-software.com)) [020603 12:40]:

> Yukihiro Matsumoto wrote:
> 
> > No, NaN should not be equal each other in any case, by its definition  
> > (IEEE 754). I don’t know why Sean get “true”.
> 
> Since I appear to be the only person who sees this, I’m guessing that my  
> Ruby installation is broken somehow.

I see it too, though it’s ruby 1.6.4 on this machine:

irb(main):052:0\> (0/0.0)==(0/0.0)  
true  
irb(main):053:0\> (0.0/0.0)==(0.0/0.0)  
true  
irb(main):054:0\> (0/0)==(0/0)  
ZeroDivisionError: divided by 0  
from (irb):54:in `/’  
from (irb):54  
$ ruby -v  
ruby 1.6.4 (2001-06-04) [i586-linux-gnu]

While on a slightly more recent install:

$ ruby -v  
ruby 1.6.5 (2001-09-08) [i386-freebsd4.4]  
$ irb  
irb(main):001:0\> (0/0.0)==(0/0.0)  
false

Rick

> **···**
>
> –  
> [http://www.rickbradley.com](http://www.rickbradley.com) MUPRN: 406 (92F/98F)  
> \> It’s like having your  
> random email haiku | own private search agent 24x7.  
> \> It’s quick. It’s easy.

---

<div class="post-metadata">

**Author:** ![Kyle\_Rawlins](https://avatars.discourse-cdn.com/v4/letter/k/779978/32.png) [@Kyle\_Rawlins](https://rubytalk.org/u/Kyle_Rawlins)\
**Post date:** [3 June 2002 20:34 UTC](https://rubytalk.org/t/infinity/52/7 "2002-06-03T20:34:17Z")

</div>

If ruby is compiled (for whatever reason) with -ffast-math and any -O on gcc,  
you might see this behavior. That option tells gcc that it can violate IEEE  
specs to be faster. This option alone doesn’t do it, but -O or -O2 etc in  
conjunction seems to. (I guess -ffast-math just allows optimization, but  
doesn’t do it)

At the heart of it (at least in 1.6.7) a ruby float is stored as a C double in  
the RFloat struct, and comparison is eventually done using C ‘==’. This should  
either invoke glibc fp functions or possibly do the fp comparison directly.  
The only way I could see ruby messing up is if its types were munged, resulting  
in calling Object.== I think, but this seems very unlikely.

If you compile the following C snippet with optimization on and with and  
without -ffast-math, you can see this happening. If it prints true without  
that option on, something is odd about your compiler or your glibc. (There is  
probably some equivelent option for other compilers, but I don’t know it)

int main(int argc, char \*\*argv)  
{  
double x, y, n1, n2;  
x = 0.0;  
y = 0.0;  
n1 = x / y;  
n2 = x / y;  
printf(“n1: %f, n2: %f, n1==n2: %s\n”, n1, n2, (n1 == n2) ? “true” :  
“false”);  
}

[rawlins@ymir compiled]$ gcc -O test.c -o test && ./test  
n1: nan, n2: nan, n1==n2: false  
[rawlins@ymir compiled]$ gcc -O -ffast-math test.c -o test && ./test  
n1: nan, n2: nan, n1==n2: true

I have no idea why ruby would have been compiled with this option though; it  
probably shouldn’t be ever.

hope this helps,  
-kyle

> **···**
>
> On Tue, Jun 04, 2002 at 02:38:25AM +0900, Sean Russell wrote:
> 
> > Yukihiro Matsumoto wrote:
> > 
> > > No, NaN should not be equal each other in any case, by its definition  
> > > (IEEE 754). I don’t know why Sean get “true”.
> > 
> > Since I appear to be the only person who sees this, I’m guessing that my  
> > Ruby installation is broken somehow.
> 
> ## – [moved](http://mas.cs.umass.edu/~rawlins)
> 
> Mit diesem Zeichen bann’Ich deinen Zauber!
