# 0.0 sign

**URL:** https://rubytalk.org/t/0-0-sign/11372
**Category:** ruby-talk
**Created:** [5 May 2004 17:33 UTC](https://rubytalk.org/t/0-0-sign/11372 "2004-05-05T17:33:52Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Elias\_Athanasopoulos](https://avatars.discourse-cdn.com/v4/letter/e/43a26b/32.png) [@Elias\_Athanasopoulos](https://rubytalk.org/u/Elias_Athanasopoulos)
#### Post date: [5 May 2004 17:33 UTC](https://rubytalk.org/t/0-0-sign/11372/1 "2004-05-05T17:33:52Z")

</div>

Hello!

I don’t know if this is intentional, but is there a reason  
that 0.0 holds the sign?

tiny = 0.0000000000000000001  
inf = 1/tiny

a = 1/inf  
b = -1/inf

p a, b, (a-b), (a+b)

a = -1/inf  
b = 1/inf

p a, b, (a-b), (a+b)

I can hardly imagine you can use it somehow; OTOH it can  
make the output a little uglier. 🙂

I checked Python and altough 0.0 carries the sign it is not  
used in the results of subtraction/addition, as it is in Ruby.

Regards,

> **···**
>
> –  
> University of Athens I bet the human brain  
> Physics Department is a kludge --Marvin Minsky
> 
> — /home/elathan/hacking/ruby/numeric.c.orig 2004-05-05 00:18:12.000000000 +0300  
> +++ /home/elathan/hacking/ruby/numeric.c 2004-05-05 00:18:41.000000000 +0300  
> @@ -502,6 +502,7 @@  
> avalue = fabs(value);  
> if (avalue == 0.0) {  
> fmt = “%.1f”;
> 
> - value = avalue;  
> }  
> else if (avalue \< 1.0e-3) {  
> d1 = avalue;

---

<div class="post-metadata">

### Author: ![Mark\_Hubbart](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@Mark\_Hubbart](https://rubytalk.org/u/Mark_Hubbart)
#### Post date: [5 May 2004 19:28 UTC](https://rubytalk.org/t/0-0-sign/11372/2 "2004-05-05T19:28:36Z")

</div>

IEEE 754 (floating point arithmetic standard) specifies negative zero  
for floats. For more explanation, see  
[http://www.obtuse.com/resources/negative\_zero.html](http://www.obtuse.com/resources/negative_zero.html)

if a mathematical result too small for your hardware to compute is  
returned, you can determine whether it was on the negative side, or the  
positive side.

def really\_complex\_function(num)  
num/1\_000\_000\_000\_000\_000\_000.0  
end  
==\>nil  
really\_complex\_function( 0.000\_000\_000\_000\_000\_000\_1 )  
==\>0.0  
really\_complex\_function( -0.000\_000\_000\_000\_000\_000\_1 )  
==\>-0.0

> **···**
>
> On May 5, 2004, at 10:33 AM, Elias Athanasopoulos wrote:
> 
> > Hello!
> > 
> > I don’t know if this is intentional, but is there a reason  
> > that 0.0 holds the sign?

---

<div class="post-metadata">

### Author: ![Tobias1](https://avatars.discourse-cdn.com/v4/letter/t/94ad74/32.png) [@Tobias1](https://rubytalk.org/u/Tobias1)
#### Post date: [5 May 2004 22:13 UTC](https://rubytalk.org/t/0-0-sign/11372/3 "2004-05-05T22:13:57Z")

</div>

What I find much more problematic than 0.0 having a sign is that ruby treats

0.0000000000000000001 == 0.0 as true.

If I’ve counted correct that should be 1 \* 10^-19. Doubles can represent  
Numbers down to approx. 10^-300.

1e-300 == 0.0 is false in ruby, as it should be.

There must be a bug in the interpreter, where it reads Float literals.

Tobias

---

<div class="post-metadata">

### Author: ![Elias\_Athanasopoulos](https://avatars.discourse-cdn.com/v4/letter/e/43a26b/32.png) [@Elias\_Athanasopoulos](https://rubytalk.org/u/Elias_Athanasopoulos)
#### Post date: [6 May 2004 07:17 UTC](https://rubytalk.org/t/0-0-sign/11372/4 "2004-05-06T07:17:17Z")

</div>

Yes, but how can you distinguish them _inside_ your code? Since  
0.0 == -0.0, 0.0 === -0.0 and even 0.0.eql?(-0.0) is true.

Regards,

> **···**
>
> On Thu, May 06, 2004 at 04:28:36AM +0900, Mark Hubbart wrote:
> 
> > On May 5, 2004, at 10:33 AM, Elias Athanasopoulos wrote:
> > 
> > > Hello!
> > > 
> > > I don’t know if this is intentional, but is there a reason  
> > > that 0.0 holds the sign?
> > 
> > IEEE 754 (floating point arithmetic standard) specifies negative zero  
> > for floats. For more explanation, see  
> > [http://www.obtuse.com/resources/negative\_zero.html](http://www.obtuse.com/resources/negative_zero.html)
> > 
> > if a mathematical result too small for your hardware to compute is  
> > returned, you can determine whether it was on the negative side, or the  
> > positive side.
> > 
> > def really\_complex\_function(num)  
> > num/1\_000\_000\_000\_000\_000\_000.0  
> > end  
> > ==\>nil  
> > really\_complex\_function( 0.000\_000\_000\_000\_000\_000\_1 )  
> > ==\>0.0  
> > really\_complex\_function( -0.000\_000\_000\_000\_000\_000\_1 )  
> > ==\>-0.0
> 
> –  
> University of Athens I bet the human brain  
> Physics Department is a kludge --Marvin Minsky

---

<div class="post-metadata">

### Author: ![Mark\_Hubbart](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@Mark\_Hubbart](https://rubytalk.org/u/Mark_Hubbart)
#### Post date: [5 May 2004 22:31 UTC](https://rubytalk.org/t/0-0-sign/11372/5 "2004-05-05T22:31:29Z")

</div>

For me, it cuts off after 1e-17:

0.000\_000\_000\_000\_000\_01  
==\>1e-17  
0.000\_000\_000\_000\_000\_001  
==\>0.0  
I’m not sure it’s a bug in float parsing, since you can go:  
1e-18  
==\>1e-18  
1e-300  
==\>1e-300  
…which would be preferable to typing in all those zeros anyway. But I  
would be curious to know why they decided to truncate long float  
literals?

–Mark

> **···**
>
> On May 5, 2004, at 3:13 PM, Tobias Peters wrote:
> 
> > What I find much more problematic than 0.0 having a sign is that ruby  
> > treats
> > 
> > 0.0000000000000000001 == 0.0 as true.
> > 
> > If I’ve counted correct that should be 1 \* 10^-19. Doubles can  
> > represent Numbers down to approx. 10^-300.
> > 
> > 1e-300 == 0.0 is false in ruby, as it should be.
> > 
> > There must be a bug in the interpreter, where it reads Float literals.

---

<div class="post-metadata">

### Author: ![NAKAMURA\_Hiroshi1](https://avatars.discourse-cdn.com/v4/letter/n/fbc32d/32.png) [@NAKAMURA\_Hiroshi1](https://rubytalk.org/u/NAKAMURA_Hiroshi1)
#### Post date: [6 May 2004 07:26 UTC](https://rubytalk.org/t/0-0-sign/11372/6 "2004-05-06T07:26:30Z")

</div>

Hi,

Elias Athanasopoulos wrote:

> Yes, but how can you distinguish them _inside_ your code? Since  
> 0.0 == -0.0, 0.0 === -0.0 and even 0.0.eql?(-0.0) is true.

Check XSDFloat implementation at xsd/datatypes.rb in ruby \> 1.8.1 or  
[http://www.ruby-lang.org/cgi-bin/cvsweb.cgi/lib/soap4r/lib/xsd/datatypes.rb?rev=1.13](http://www.ruby-lang.org/cgi-bin/cvsweb.cgi/lib/soap4r/lib/xsd/datatypes.rb?rev=1.13)  
I hope this helps.

Regards,  
// NaHi

---

<div class="post-metadata">

### Author: ![Mark\_Hubbart](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@Mark\_Hubbart](https://rubytalk.org/u/Mark_Hubbart)
#### Post date: [6 May 2004 08:52 UTC](https://rubytalk.org/t/0-0-sign/11372/7 "2004-05-06T08:52:46Z")

</div>

1/0.0 \> 0  
==\>true  
1/-0.0 \> 0  
==\>false

0.0 and -0.0 are considered equal. They only retain the sign for the  
case where you need to check it. If you divide 1 by a suspicious zero,  
you’ll get either Infinity or -Infinity. Or, if you need to check a  
lot:

class Numeric  
def negative?()  
zero? ? (1.0/self \< 0) : (self \< 0)  
end  
end  
==\>nil  
-0.0.negative?  
==\>true  
0.0.negative?  
==\>false

cheers,  
–Mark

> **···**
>
> On May 6, 2004, at 12:17 AM, Elias Athanasopoulos wrote:
> 
> > On Thu, May 06, 2004 at 04:28:36AM +0900, Mark Hubbart wrote:
> > 
> > > On May 5, 2004, at 10:33 AM, Elias Athanasopoulos wrote:
> > > 
> > > > Hello!
> > > > 
> > > > I don’t know if this is intentional, but is there a reason  
> > > > that 0.0 holds the sign?
> > > 
> > > IEEE 754 (floating point arithmetic standard) specifies negative zero  
> > > for floats. For more explanation, see  
> > > [http://www.obtuse.com/resources/negative\_zero.html](http://www.obtuse.com/resources/negative_zero.html)
> > > 
> > > if a mathematical result too small for your hardware to compute is  
> > > returned, you can determine whether it was on the negative side, or  
> > > the  
> > > positive side.
> > > 
> > > def really\_complex\_function(num)  
> > > num/1\_000\_000\_000\_000\_000\_000.0  
> > > end  
> > > ==\>nil  
> > > really\_complex\_function( 0.000\_000\_000\_000\_000\_000\_1 )  
> > > ==\>0.0  
> > > really\_complex\_function( -0.000\_000\_000\_000\_000\_000\_1 )  
> > > ==\>-0.0
> > 
> > Yes, but how can you distinguish them _inside_ your code? Since  
> > 0.0 == -0.0, 0.0 === -0.0 and even 0.0.eql?(-0.0) is true.

---

<div class="post-metadata">

### Author: ![Zsban\_Ambrus](https://avatars.discourse-cdn.com/v4/letter/z/e5b9ba/32.png) [@Zsban\_Ambrus](https://rubytalk.org/u/Zsban_Ambrus)
#### Post date: [6 May 2004 13:20 UTC](https://rubytalk.org/t/0-0-sign/11372/8 "2004-05-06T13:20:46Z")

</div>

I think this might be a bug in the C library, not ruby. Don’t take this for  
sure however.

Why I think this is because something similar seems to occur in Perl too.

I’ve written a japh “[http://www.perlmonks.com/index.pl?node\_id=330784](http://www.perlmonks.com/index.pl?node_id=330784)”.  
I’ve checked that it works on machines with either endianness (linux-ix86  
and sun4-solaris-sparc). However, I’ve got reports saying that it won’t  
work on their machine. One report,  
“[Re: Fun with duff's device and AUTOLOAD](http://www.perlmonks.com/index.pl?node_id=331720)” looks especially as if  
the last few digits of the floating point literals in the code were ignored.  
That last report was on a winnt machine, but it’s also said that it doesn’t  
work on solaris.

Please tell me on what platform and OS did you get the above results.

ambrus

> **···**
>
> On Thu, May 06, 2004 at 07:13:57AM +0900, Tobias Peters wrote:
> 
> > What I find much more problematic than 0.0 having a sign is that ruby treats
> > 
> > 0.0000000000000000001 == 0.0 as true.
> > 
> > If I’ve counted correct that should be 1 \* 10^-19. Doubles can represent  
> > Numbers down to approx. 10^-300.
> > 
> > 1e-300 == 0.0 is false in ruby, as it should be.
> > 
> > There must be a bug in the interpreter, where it reads Float literals.
> > 
> > Tobias

---

<div class="post-metadata">

### Author: ![Elias\_Athanasopoulos](https://avatars.discourse-cdn.com/v4/letter/e/43a26b/32.png) [@Elias\_Athanasopoulos](https://rubytalk.org/u/Elias_Athanasopoulos)
#### Post date: [6 May 2004 09:18 UTC](https://rubytalk.org/t/0-0-sign/11372/9 "2004-05-06T09:18:44Z")

</div>

Ok, got it! Very nice. 🙂 Thanx.

Regards,

> **···**
>
> On Thu, May 06, 2004 at 05:52:46PM +0900, Mark Hubbart wrote:
> 
> > class Numeric  
> > def negative?()  
> > zero? ? (1.0/self \< 0) : (self \< 0)  
> > end  
> > end  
> > ==\>nil  
> > -0.0.negative?  
> > ==\>true  
> > 0.0.negative?  
> > ==\>false
> 
> –  
> University of Athens I bet the human brain  
> Physics Department is a kludge --Marvin Minsky

---

<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: [6 May 2004 14:08 UTC](https://rubytalk.org/t/0-0-sign/11372/10 "2004-05-06T14:08:20Z")

</div>

Hi,

> **···**
>
> In message “Re: Bug in Float literal (1e-19 == 0) Re: 0.0 sign” on 04/05/06, “Zsban Ambrus” [ambrus@math.bme.hu](mailto:ambrus@math.bme.hu) writes:
> 
> > > 0.0000000000000000001 == 0.0 as true.
> 
> > I think this might be a bug in the C library, not ruby. Don’t take this for  
> > sure however.
> 
> It’s a bug in strtod(), but Ruby uses its own strtod() implementation.  
> H.Yamamoto submitted the fix in [ruby-dev:23465].
> 
> ```
> matz.
> 
> ```
