From: GOTO Kentaro Date: 2002-02-25T20:07:59+09:00 Subject: Re: inconsistence in class complex In message "Re: inconsistence in class complex" on Mon, 25 Feb 2002 18:43:54 +0900, Stephan K�mper writes: > Juergen Katins wrote: > > > > While translating the book "Programming Ruby" by Dave Thomas and > > Andrew Hunt into german language (see > > http://home.vr-web.de/juergen.katins/ruby/) I stumbled arcoss an > > irritating inconsistence in the standard library. In the class complex > > there is a comparision operator <=>. > > > > <=> ref <=> other -> -1, 0, +1 > > Returns ref.abs <=> other.abs. > > > > In mathematical meanings this makes no sense! You can not compare > > complex numbers! > > That depends: Comparing to complex numbers 'by absolute value' make perfect > sense, eg. when dealing with sequences of convergent complex numbers. > Anyway, talking 'mathematicese' you'd likely define a metric upon which to > construct a definition for lenght (or absolute value). > > Notwithstanding, comparing complex numbers, as with '<' or '>', makes no > sense indeed. > > Now, while I think that the current behaviour of <=> can be accepted, '>' > and '<' shouldn't work as currently implemented. Here is another odd: -2 == Complex(-2,0) #=> true 1 == Complex(1,0) #=> true -2 <=> 1 #=> -1 Complex(-2,0) <=> Complex(1,0) #=> 1 This says (a == b) && (c == d) but (a <=> c) != (b <=> d). > As far as I see, Complex inherits from Numeric and that's OK. > And it doesn't include Comparable, which relects the fact that complex > numbers can'T be compared with '>' and the like. I agree halfly. In Ruby world, Module forms partialy order, i.e., A < B and B < C implies A < C for any module A,B,C. Now, we have Complex < Numeric and Numeric < Comparable, therefore Complex < Comparable automatically even if Complex#<=> would be undefined. There are other Ruby-definable strange numeric sets, e.g., Hamilton's Quotanion, which has four imaginary units. So, Complex isn't only special. However, undef Complex#<=> and ignoring the fact Complex < Comparable seems a practical and acceptable option, IMHO. To go rigorous, Numeric must not include Comparable but Integer, Float and Rational must include severally. -- Gotoken