0.0 sign

  • Thread starter Elias Athanasopoulos
  • Start date
E

Elias Athanasopoulos

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;
 
M

Mark Hubbart

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

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
 
T

Tobias Peters

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
 
M

Mark Hubbart

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.

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
 
E

Elias Athanasopoulos

IEEE 754 (floating point arithmetic standard) specifies negative zero
for floats. For more explanation, see
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.

Regards,
 
M

Mark Hubbart

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.

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
 
E

Elias Athanasopoulos

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

Ok, got it! Very nice. :) Thanx.

Regards,
 
Z

Zsban Ambrus

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

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".
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,
"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
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Bug in Float literal (1e-19 == 0) Re: 0.0 sign"

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top