[Python-Dev] Decimal <-> float comparisons in py3k.

Glenn Linderman glenn at nevcal.com
Thu Mar 18 21:31:33 CET 2010


On 3/18/2010 12:34 PM, Steven D'Aprano wrote:
> On Fri, 19 Mar 2010 05:27:06 am Glenn Linderman wrote:
>
>    
>>> Do you envisage any problems from allowing this instead?
>>>
>>>        
>>>>>> Decimal('1.1')<   2.2
>>>>>>              
>>> True
>>>        
>> Yes.
>>
>> As any non-naïve float user is aware, the proper form of float
>> comparisons is not to use<  or>  or == or !=, but rather, instead of
>> using<  (to follow along with your example), one should use:
>>
>> Decimal('1.1') - 2.2<  epsilon
>>      
> And yet we allow
>
> 1.1<  2.2
>
> instead of forcing users to do the "proper form". One can only wonder
> why the various standards (actual and de-facto) for floating point
> allows comparisons at all.
>    

Hard to tell

1.1 < 2.2

from the second line of

diff = 1.1 - 2.2
diff < epsilon

Hard to enforce the "proper form", without being omniscient, and the 
"proper form" isn't always as simple as my example, to be truly proper 
often requires a lot more code (and a lot more omniscience).

I'm +1 on adding an exception for Decimal/float comparisons, -0 on 
allowing it to be turned off to achieve compatible behavior, since 
Raymond pointed out that the compatible sorting behavior isn't stable in 
the presence of int, Decimal, and float.


> But they do, and so does Python, and frankly even if the only reason is
> to satisfy lazy coders who don't have a requirement for high accuracy,
> then that's a good reason in my book, and one equally applicable to
> Decimal and float.
>    

A point well understood, but I personally would rather force 
Decimal/float comparisons to be explicitly converted to be lazily 
compared, if the arithmetic operations are forced to be explicit.



More information about the Python-Dev mailing list