From: "Stephan Kämper" Date: 2002-02-25T18:43:54+09:00 Subject: Re: inconsistence in class complex 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. 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. > This wrongly comparision method leeds to the need of an additional > equation operator ==, also something you would not expect. If you see You need '==' to compare for equality, which is defined for complex numbers (they'd be pretty much useless without it...). > a=b you would expect the two numbers to be the same, not to have the > same length. Indeed after 'a=b' the numbers are the same, due to the assignment. ;-) > Following the principle of least surprise I would strongly recommend > for the next version of ruby to discard the complex operators <=> and > ==, leaving only the operator = which should work as you would expect. You might like to keep '<=>' - if keep in mind that this compares the absolute vaues. That's OK. As said above you like to have such a comparison - between _real_ numbers_ for work concerned with, eg., convergence of complex numbers You definitely need '=' for complex assignment, as with more or less other data type. And you'll nedd '==' to compare two complex numbers for equality. All three are correctly implemented in complex.rb, as far as I can tell. The 'problem' is that '>' and the like ('>=' and friends) can be used with complex numbers - although these operators are not defined with in class Complex. I guess, and it's only a guess for now (!), that this is due to some coercing happening when comparing complex numbers. > > PS: Do not try to invent a comparision which is compatible with > addition. This is possible but then the comparision will not be > compatible with multiplication. I second and repeat it (Sorry Dave & Andy for violating the DRYOU principle): Don't try to invent '<' and/or friends for complex numbers. It doesn't make any sense. Check your favourite complex analysis book for reference... Happy rubying! Stephan