[Python-Dev] Comparing heterogeneous types

Chermside, Michael mchermside at ingdirect.com
Fri Jun 25 12:22:54 EDT 2004


> #- > I maintain that when comparing a long with a float
> #- > where the exponent is larger than the precision, that the
> #- > float should be treated as if it were EXACTLY EQUAL TO 
> #- > <coefficient>*2^<exponent>, instead of trying to treat it as
> #- > some sort of a range. Similarly, if we implemented a Rational
> #- > type, I would suggest that instances of float (or of Facundo's
> #- > Decimal) where <exponent> is LESS than the digits of
> #- > <coefficient> should be treated as if they were EXACTLY EQUAL
> #- > TO the corresponding fraction-over-a-power-of-two.

Facundo writes:
> Don't get to understand this. Could you send please an 
> example of Decimal to
> this case?

Presume a Rational type:

    >>> class Rational(object):
    >>>     def __init__(self, num, denom):
    >>>         self.num = num
    >>>         self.denom = denom
    >>>     # ....

Now define the following

    >>> x = 12300
    >>> y = Decimal.Decimal((0,(1,2,3),2))
    >>> z = Rational(1230000,100)

I maintain that x == y should be true (and it is, today).
I maintain that x == z should be true (and it is, obviously).
I maintain that y == z should be true. _IF_ you think
of Decimal.Decimal((0,(1,2,3),2)) as being "any number which,
when rounded to the nearest hundred, gives 12300" (in other
words, as the range 12250..12350), then you might not agree.
But if you think of Decimal.Decimal((0,(1,2,3),2)) as
being *exactly* 12300 (but with a precision of only 3 places),
then you'd want x == z to be true.

-- Michael Chermside


This email may contain confidential or privileged information. If you believe you have received the message in error, please notify the sender and delete the message without copying or disclosing it.




More information about the Python-Dev mailing list