From: Christoph Rippel Date: 2001-03-30T08:38:16+09:00 Subject: [ruby-talk:13347] Re: A subtle Hash bug > -----Original Message----- > From: Mathieu Bouchard [mailto:matju@sympatico.ca] > Sent: Thursday, March 29, 2001 09:42 AM > To: ruby-talk ML; Pete Kernan > Cc: ruby-talk ML; ruby-talk@netlab.co.jp > Subject: [ruby-talk:13334] Re: A subtle Hash bug > > > On Fri, 30 Mar 2001, Pete Kernan wrote: > > On Thu, 29 Mar 2001 23:11:03 +0900, Christoph Rippel pontificated: > > + my reason for preferring the ladder is that the combination of > > + (-0.0 == 0.0) but (1/-0.0) != (1/0.0) seems somewhat dubious to me > > 1/0.0 == 1/0.0 is dubious enough. is this a cardinality test? :) > > % ruby -le 'p (1/0.0 == 1/0.0)' > > true > > Is there any good reason why Float's "Infinity" value is equal to itself? > And then, is there any good reason why it shouldn't be changed? Well, the outcome of floating point division of 1 by +/-0.0 is either Infinity or -Infinity and that they are different that just a reflection of standard C double behavior. Not you can use +/- Infinity as regular input and gets something sensible like p Math.atan(1/-1.0) # => -0.7853981634 a.k.a -Pi/4 p Math.atan(1/1.0) # => 0.7853981634 so +/-Infinities have to be different. The (-0.0).eql? (0.0) # => false hack is hardware independent and should work on all platforms. The exception raising behavior of zero-division and comparison is type specific on the method receiver end 2**29 <=> 1/0.0 # => -1 since 2**29 is a Fixnum 2**33 <=> 1/0.0 # FloatDomainError: Infinity since 2**33 is Bignum 1/ 0 # a ZeroDivisionError is raised for any Integer type I actually have a Ruby hack of logical +/- Infinity's constants. The semantics is closer to +/- nil and but you can perform limited Arithmetic on them - I made them instances of +/- Infinity Classes sub-typed from Numeric but this is not really necessary. (the are necessarily smaller/bigger then floats infinities). The visible instances are two singleton objects, which work as Receivers in the whole object hierarchy and rely on an internal coercion mirror singleton constant which makes them work as methods input of any numeric object (except for Rational, which is an eager beaver and tries to process input it does not understand instead of calling coerce). Christoph