Unexpected results comparing float to Fraction

Terry Reedy tjreedy at udel.edu
Mon Jul 29 16:48:40 EDT 2013


On 7/29/2013 1:29 PM, Steven D'Aprano wrote:
> On Mon, 29 Jul 2013 13:08:20 -0400, Terry Reedy wrote:
>
>> In other words, there can be multiple unequal Franctions that have the
>> same float value: for instance, Fraction(1,3) and
>> Fraction(6004799503160661, 18014398509481984)
>>
>>   > So from that standpoint it makes sense to me to cast to Fraction when
>>   > comparing.
>>
>> Otherwise, == becomes non-transitive
>
> This is Python, and we can make __eq__ methods that do anything,
> including be non-transitive, non-reflexive, and nonsensical if we like :-)

Yes, Python's developers can intentionally introduce bugs, but we try 
not to. The definitions of sets and dicts and containment assume that == 
means equality as mathematically defined. As one time, we had 0 == 0.0 
and 0 == Decimal(0) but 0.0 != Decimal(0) (and so on for all integral 
float values. That 'misfeature' was corrected because of the 'problems' 
it caused. That lesson learned, one of the design requirements for the 
new enum class (metaclass) was that it not re-introduce non-transitivity.

-- 
Terry Jan Reedy




More information about the Python-list mailing list