Lpthw - ex3.py video - the - -2 problem

Hi,

I’m learning Python 3 and to code with this excellent book.
I saw the video of the exercise 3 and the apparent problem or the minus minus.

I thought that in math it’s is like -(-2) that it’s equal to + 2.
In both casses 5 is greater… so I tried with:

Python 3.7.0 (default, Jul 15 2018, 10:44:58) 
[GCC 8.1.1 20180531] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> print("Is it greater?", 5 > -6)
Is it greater? True

>>> print("Is it greater?", 5 > --6)
Is it greater? False

and it’s works!
I hope it’s useful to you.

Thak you very much,

Pablo

2 Likes

Wow, that is weird. Ok, so your evaluation of what’s going on isn’t quite correct, but also what Python does is really not correct. Here’s me playing with it:

Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> --6
6
>>> --6
6
>>> ---6
-6
>>> ^D
$ python
Python 2.7.10 (default, Oct  6 2017, 22:29:07) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> --6
6
>>> ---6
-6
>>> -----6
-6
>>> ----6
6
>>> 

Apparently, each - acts as a negation, so that it flips it from positive to negative then back. It’s more like each - is * -1.

Yes, in math every -(xxx) is exactly the same as -1*(xxx).
But here what we would have apparently is what in math calls an “abuse of notation”.
Maybe here is a “simplification of notation”. I know some math, but in code these are my first steps.

Like: - (- (- (6) ) ) = - 6

Here is an - - - 6 = - 6

Python 3.7.0 (default, Jul 15 2018, 10:44:58) 
[GCC 8.1.1 20180531] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> -6
-6
>>> -(6)
-6
>>> -(-6)
6
>>> ---6
-6
>>> -(-(-(6)))
-6
>>> -1*6
-6
>>> (-1)*6
-6
>>> ++++6
6
>>> ++++-6
-6
>>> +-+-+-6
-6
1 Like

So the decrement acts like an increment of the decrement.
Love it.
:sweat_smile:

1 Like

Multipy a number by -1 changes the sign of the number:

524 * (-1) = - 524
– 524 * (-1) = 524
and so…

– (a) = (-1) * a = - a
-(-(a)) = (-1)*(-1) * a = 1 * a = a
and so…

1 Like

I’d have to see something authoritative before I believed that every - is a double negation, but then again I’m not so great at math.

The significance of this though is that in many other languages – means “decrement” so anyone accidentally using this would be stumped about why it behaved this way.

1 Like

Oooh, now I can see the confusion!:

You’re right, because the - - z exists in code lenguages, but really doesn’t exists in math, I just assumed that it’s an abuse of notation of the - ( - z), wich yes exists in maths.

In fact, there are 2 basic operations defined, the addition and the multiplication. And the definition of the substraction is derivated from de addition:

One of the basics properties of for the numbers, except for zero, is the existence of the inverse additive, that is the number that summed gives zero result, and is noted like:

a + (- a) = 0

(- a) is the inverse additive of a and, because definition, -(-a) is the invers additive of (-a);
but a is the inverse additive of (-a) too, so…

-(-a) = a

Usually the notation is simplificated from a + (-a) to a - a, and you get the substraction, which is tecnicaly an addition.

Note that, “if you substract a negative number, you add the positive number”:

b - (- b) = b + b

You can explore this some using objects if you like. So take this:

a-(-a)

Alright, that’s not ambiguous and makes sense because of the parenthesis. Now if you get rid of those:

a - -a

It’s ambiguous in a language with --. Do you mean the first one, or a --a which is a syntax error. Python though doesn’t have – so that means it’s using the first one, which is fine. Ok great. However, what does this mean?

a - --a

If you try to do use parenthesis to disambiguate this then you’d get:

a - (- (-a)).

Now suddenly the middle - is ambiguous because do you mean:

a minus (negative (negative a))

or

a minus (minus (negative a))

If it’s the first one, then why is it not then “a negative (negative (negative a)”? Because that’s a syntax error since the unary minus only applies to one operand. If it’s the second one then why isn’t is “a minus (minus (minus a)”? Again, because that’s a syntax error because binary minus needs two operands on either side and the parenthesis around the middle one block that.

The way you solve it is to simply decided what one you want and then go with it, but there’s an interesting implication to it when you have object oriented programming. Let’s say I have some objects that intercept __sub__ and __neg__ so I can do obj1 - obj2 and get something meaningful. Take a look:

>>> class SubTest:
...   def __neg__(obj):
...     print("neg", obj)
...     return obj
...   def __sub__(obj, x):
...     print("neg", obj)
...     return x
...
>>> left = SubTest()
>>> right = SubTest()
>>> left
<__main__.SubTest object at 0x7f9b74c8b4e0>
>>> right
<__main__.SubTest object at 0x7f9b74c8b4a8>
>>>
>>> left ----right
neg <__main__.SubTest object at 0x7f9b74c8b4a8>
neg <__main__.SubTest object at 0x7f9b74c8b4a8>
neg <__main__.SubTest object at 0x7f9b74c8b4a8>
neg <__main__.SubTest object at 0x7f9b74c8b4e0>
<__main__.SubTest object at 0x7f9b74c8b4a8>
>>>

You can play around with “operator overloading” using this https://docs.python.org/3/reference/datamodel.html#emulating-numeric-types and it looks like Python takes the first - and treats it like subtract, then all subsequent - are negation, which makes sense since Python doesn’t have --.

Basically, it’s not really “wrong”, since it’s just a design decision and it works in the universe of Python. It is however a questionable design decision given the universe of other languages that do have -- or ++ as operators. Programmers coming from those languages will run into bugs if they accidentally use that without knowing that this happens. In fact, I bet this is one of the reasons Python doesn’t have these operators. They decided to do this for multiple negation math and then ooops oh uhhhhhhhhhhh ok no – operator then.

1 Like

Thank you, I really understand the half of it… jajaja
But it’s interesting, especially the different meaning of the minus according to the context in which it is found.
I hadn’t thought of that.