Minus operator versus unary minus

Peter Otten __peter__ at web.de
Sat May 30 05:09:36 EDT 2015


Serhiy Storchaka wrote:

> On 30.05.15 10:56, Peter Otten wrote:
>> The following modification of the collections.Counter implementation
>>
>> https://hg.python.org/cpython/rev/fe4efc0032b5
>>
>> was just checked in with the line
>>
>> result[elem] = 0 - count
>>
>> Does this have an advantage over the obvious
>>
>> result[elem] = -count
>>
>> ?
> 
>>>> x = 0.0
>>>> -x
> -0.0
>  
> 
>>>> 0 - x
> 0.0

That's a subtle difference, but does it matter in the context of the patch? 
The complete code is

+        result = Counter()
+        for elem, count in self.items():
+            if count < 0:
+                result[elem] = 0 - count
+        return result

and

>>> -0.0 < 0
False

so both +0.0 and -0.0 would be skipped anyway.




More information about the Python-list mailing list