From: Mark Hubbart Date: 2004-05-06T17:52:46+09:00 Subject: Re: 0.0 sign 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 >> >> 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. 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