Re: Discussion of why java.lang.Number does not implement Comparable
Twisted wrote:
On Jul 28, 7:38 am, Michael Jung <m...@phantasia.org> wrote:
* First, sorting a NaN in some arbitrary but consistent way, e.g. NaN
< -inf < -10 < -1 < 0 < 1 < 10 < inf, allows well-defined behavior and
makes collections of floats that may include NaNs safe to put into
e.g. a TreeSet. So I'm not sure I disagree with Float's implementation
of this behavior.
That wont really help, because you will lose certain features of
comparing.
Explain please.
- You can usually determine algebraically whether they are equal or
not.
No.
Do not bluntly contradict me in a public forum.
Shrug. Different people have different preferences, so it is hard for
writers to keep track. I'm here to learn, as well as to pass on any
relevant insights I have, so if someone disagrees with me I *want* them
to say so, simply and directly. I won't be offended as long as they
explain their reasoning, so that I can either be convinced and learn, or
be able to explain why I disagree with their reasoning.
Depending on what you do, solutions to these problems are unknown.
There's an equation that can in principle be solved to see if they are
equal. In practise it will be difficult with big enough (degree 5 or
more) polynomials and various transcendental functions. In many cases,
you know one is algebraic and the other transcendental and therefore
they must be unequal.
I think you may be underestimating the difficulties of dealing with
general real numbers. In many cases we can calculate the value to any
required finite number of decimal, or binary, places. That is enough to
give the order for two unequal values, but not enough to ever be able to
declare equality.
Even if a class represents values of some transcendental function there
can be some points at which the function has an algebraic solution. For
example, consider the sines of angles that can be expressed as rational
numbers of degrees. sin(30), sin(45), and sin(60) are exact solutions to
quadratic equations.
One could write a comparator between sines and quadratic equation
solutions by special casing angles that are exact multiples of three
degrees, and then calculating the binary expansions of the other values
until there is a difference.
However, to make Number implement Comparable we would need a general
approach to comparing that does not depend on writing special code for
every pair of Number subclasses.
Here's a challenge for those who want to make Number implement
Comparable<Number>. Write the API documentation for the compareTo
method. Tell me what I have to do if I am writing a new Number subclass
in order to conform to the contract.
Patricia