Minus operator versus unary minus

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Minus operator versus unary minus

Peter Otten
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

?



Reply | Threaded
Open this post in threaded view
|

Minus operator versus unary minus

Serhiy Storchaka-2
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
 





Reply | Threaded
Open this post in threaded view
|

Minus operator versus unary minus

Peter Otten
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.



Reply | Threaded
Open this post in threaded view
|

Minus operator versus unary minus

Serhiy Storchaka-2
On 30.05.15 12:09, Peter Otten wrote:

> 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?

I don't know. Perhaps there is more meaningful difference for decimals.

This code is not new (see
https://hg.python.org/cpython/rev/e908ebc160f0).



Reply | Threaded
Open this post in threaded view
|

Minus operator versus unary minus

Dave Farrance
In reply to this post by Serhiy Storchaka-2
Peter Otten <__peter__ at web.de> wrote:

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

Maybe the coder was simply aiming for visibility.  The unary minus can
be hard to spot in some circumstances.  e.g.: I've sneaked a unary minus
into this maths proof, which makes it horrible (although correct):

u and v are vectors (so have an anti-commutative cross-product)
Show that (u - v) x (u + v) = 2u x v

u x (u + v) - v x (u + v)
u x u + u x v - v x u - v x v
u x v - v x u  [ because u x u = 0, v x v = 0]
u x v - (u x -v)
u x v - (-1)(u x v)
u x v + u x v
2(u x v)
2u x v