Re: semantics of ** (unexpected/inconsistent?)

2009-12-07 Thread Albert van der Horst
In article <87eingrbh9@benfinney.id.au>,
Ben Finney   wrote:
>Lie Ryan  writes:
>
>> I generally do not expect operator precedence to be reliable at all
>
>Have another read of the thread. The OP's confusion was not over
>operator precedence, but over how names resolve to values in
>expressions.

Operator precedence comes naturally into this matter.
For example, in algol 68 -3**2 is parsed as
  (-3)**2
because of the simple rule that all unary operators have precedence
over all binary operators.

(It is a good rule, and this is about the only way to get a
somewhat surprising result. Unary operators -- as long as they
are always put up front -- need not have
a precedence among themselves, so with this rule they don't
need a precedence full stop. )

>Ben Finney

Groetjes Albert

--
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
alb...@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: semantics of ** (unexpected/inconsistent?)

2009-11-30 Thread inhahe
On Mon, Nov 30, 2009 at 2:05 PM, Lie Ryan  wrote:
> On 12/1/2009 5:58 AM, inhahe wrote:
>>
>> i wasn't suggesting it as a feature for python, just pointing out why
>> it might seem counterintuitive.
>
> I'm interested, what do YOU (inhahe) think the result should be? Should both
> become -9 or both become 9. What was your expectation when you wrote that
> post?
> --
> http://mail.python.org/mailman/listinfo/python-list
>

i think the way it works currently is the sane way..pays respect to
the standard order of operations, doesn't have to do anything special
with x = -3, just treats it as an integer, and corresponds with normal
algebra.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: semantics of ** (unexpected/inconsistent?)

2009-11-30 Thread Brian J Mingus
On Sun, Nov 29, 2009 at 5:58 PM, Esmail  wrote:

> Brian J Mingus wrote:
>
>>
>>
>>
>> I think you answered your own question. 3**2 comes first in the order of
>> operations, followed by the negation.
>>
>
> No, that's not the problem, I'm ok with the operator precedence of - vs **
>
> My problem is why I don't get the same result if I use the literal -3 or
> a variable that contains -3 (x in my example)


Yes, that is the problem. Setting x=-3 is the same as writing (-3)**2 vs.
-(3**2).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: semantics of ** (unexpected/inconsistent?)

2009-11-30 Thread Lie Ryan

On 12/1/2009 5:58 AM, inhahe wrote:

i wasn't suggesting it as a feature for python, just pointing out why
it might seem counterintuitive.


I'm interested, what do YOU (inhahe) think the result should be? Should 
both become -9 or both become 9. What was your expectation when you 
wrote that post?

--
http://mail.python.org/mailman/listinfo/python-list


Re: semantics of ** (unexpected/inconsistent?)

2009-11-30 Thread inhahe
On Mon, Nov 30, 2009 at 1:53 PM, Chris Rebert  wrote:
>> On Mon, Nov 30, 2009 at 3:46 AM, Gregory Ewing
>>  wrote:
>>> Esmail wrote:
>>>
 Wow .. never heard of Concatenative_languages languages before or the
 distinction you make. Your distinction explains the behavior, but I
 find it somewhat counter-intuitive.
>>>
>>> You shouldn't find it any more surprising than the fact that
>>>
>>>  a = 2 + 3
>>>  print a * 5
>>>
>>> gives a different result from
>>>
>>>  print 2 + 3 * 5
>
> On Mon, Nov 30, 2009 at 3:41 AM, inhahe  wrote:
>> one point of confusion could be the use of ** instead of superscript.
>> it might make things a little bit more counterintuitive-looking than
>> with superscripts, since the issue with
>
> Well, since source code is almost universally just plain ASCII and not
> in some file format with typesetting, superscripts aren't going to
> happen any time soon.
> (Also, avoid top-posting in the future.)
>

i wasn't suggesting it as a feature for python, just pointing out why
it might seem counterintuitive.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: semantics of ** (unexpected/inconsistent?)

2009-11-30 Thread Chris Rebert
> On Mon, Nov 30, 2009 at 3:46 AM, Gregory Ewing
>  wrote:
>> Esmail wrote:
>>
>>> Wow .. never heard of Concatenative_languages languages before or the
>>> distinction you make. Your distinction explains the behavior, but I
>>> find it somewhat counter-intuitive.
>>
>> You shouldn't find it any more surprising than the fact that
>>
>>  a = 2 + 3
>>  print a * 5
>>
>> gives a different result from
>>
>>  print 2 + 3 * 5

On Mon, Nov 30, 2009 at 3:41 AM, inhahe  wrote:
> one point of confusion could be the use of ** instead of superscript.
> it might make things a little bit more counterintuitive-looking than
> with superscripts, since the issue with

Well, since source code is almost universally just plain ASCII and not
in some file format with typesetting, superscripts aren't going to
happen any time soon.
(Also, avoid top-posting in the future.)

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: semantics of ** (unexpected/inconsistent?)

2009-11-30 Thread MRAB

Lie Ryan wrote:

On 11/30/2009 12:38 PM, Esmail wrote:

Thanks all!! I get it now :-)

It helped to have a number of different explanations, thanks
for taking the time to post. Much appreciated.


I generally do not expect operator precedence to be reliable at all 
except for:


+ - (binary ops, not the unary)
* /
**

for other operators I would have explicit parens. It's too much work to 
remember the rest of the precedence sheet.


Most programming languages don't differentiate in text between the
number "negative 3" and the expression "negated 3". APL does. The former
is written as "¯3" (3 preceded by the overscore character) and the
latter as "-3" (3 preceded by the minus sign).
--
http://mail.python.org/mailman/listinfo/python-list


Re: semantics of ** (unexpected/inconsistent?)

2009-11-30 Thread inhahe
one point of confusion could be the use of ** instead of superscript.
it might make things a little bit more counterintuitive-looking than
with superscripts, since the issue with
 would only apply to exponents, as

-5*4

and

a = -5
a*4

return the same answer, and superscripts make it a little easier to
associate the exponent value with the base more than with the - before
it.

On Mon, Nov 30, 2009 at 3:46 AM, Gregory Ewing
 wrote:
> Esmail wrote:
>
>> Wow .. never heard of Concatenative_languages languages before or the
>> distinction you make. Your distinction explains the behavior, but I
>> find it somewhat counter-intuitive.
>
> You shouldn't find it any more surprising than the fact that
>
>  a = 2 + 3
>  print a * 5
>
> gives a different result from
>
>  print 2 + 3 * 5
>
> --
> Greg
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: semantics of ** (unexpected/inconsistent?)

2009-11-30 Thread Gregory Ewing

Esmail wrote:


Wow .. never heard of Concatenative_languages languages before or the
distinction you make. Your distinction explains the behavior, but I
find it somewhat counter-intuitive.


You shouldn't find it any more surprising than the fact that

  a = 2 + 3
  print a * 5

gives a different result from

  print 2 + 3 * 5

--
Greg
--
http://mail.python.org/mailman/listinfo/python-list


Re: semantics of ** (unexpected/inconsistent?)

2009-11-29 Thread inhahe
On Sun, Nov 29, 2009 at 8:04 PM, Esmail  wrote:
> Chris Rebert wrote:
> Wow .. never heard of Concatenative_languages languages before or the
> distinction you make. Your distinction explains the behavior, but I
> find it somewhat counter-intuitive. (I use the Python interpreter frequently
> for small calculations - otherwise I'd never have realized this)


Well I think of it like this
-3**2, because of the order of operations as you know, groups as -(3**2)
now if you set x to -3, (-3) is now automatically, inseparably
grouped. so in a sense by setting x to that you're grouping it.
x == (-3)
x**2 == (-3)**2

if it still seems counterintuitive, what about the fact that it's
exactly the same way in Algebra?

basic algebra:
  -3(superscript)2 is -9
  but if x is -3, then x(superscript)2 is 9
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: semantics of ** (unexpected/inconsistent?)

2009-11-29 Thread Ben Finney
Lie Ryan  writes:

> I generally do not expect operator precedence to be reliable at all

Have another read of the thread. The OP's confusion was not over
operator precedence, but over how names resolve to values in
expressions.

-- 
 \  “Life does not cease to be funny when people die any more than |
  `\  it ceases to be serious when people laugh.” —George Bernard Shaw |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: semantics of ** (unexpected/inconsistent?)

2009-11-29 Thread Lie Ryan

On 11/30/2009 12:38 PM, Esmail wrote:

Thanks all!! I get it now :-)

It helped to have a number of different explanations, thanks
for taking the time to post. Much appreciated.


I generally do not expect operator precedence to be reliable at all 
except for:


+ - (binary ops, not the unary)
* /
**

for other operators I would have explicit parens. It's too much work to 
remember the rest of the precedence sheet.

--
http://mail.python.org/mailman/listinfo/python-list


Re: semantics of ** (unexpected/inconsistent?)

2009-11-29 Thread Esmail

Thanks all!! I get it now :-)

It helped to have a number of different explanations, thanks
for taking the time to post. Much appreciated.

Cheers,
Esmail

--
http://mail.python.org/mailman/listinfo/python-list


Re: semantics of ** (unexpected/inconsistent?)

2009-11-29 Thread Mel
Esmail wrote:
> Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
> [GCC 4.3.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> 
> 
>  >>> -3**2
> -9
> 
>  >>> x = -3
> 
>  >>> x**2
> 9
>  >>>
> 
> I would have expected the same result in both cases.
> 
> Initially I would have expected -3**2 to yield 9, but I can accept
> that ** binds tighter than the unary -, but shouldn't the results
> be consistent regardless if I use a literal or a variable?

When you say ** binds tighter than unary -, you're also saying that -3 isn't 
a literal: it's an expression.  Try

y=3
-y**2

Mel.


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: semantics of ** (unexpected/inconsistent?)

2009-11-29 Thread Alf P. Steinbach

* Esmail:

Ok, this is somewhat unexpected:

Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.


 >>> -3**2
-9

 >>> x = -3

 >>> x**2
9
 >>>

I would have expected the same result in both cases.

Initially I would have expected -3**2 to yield 9, but I can accept
that ** binds tighter than the unary -, but shouldn't the results
be consistent regardless if I use a literal or a variable?


It is.

>>> -3**2
-9
>>> x = 3
>>> -x**2
-9
>>>

:-)


I guess you expect your expression "x**2" to somehow be evaluated as "-3**2". 
But x doesn't contain text, it contains an integer value that presumably (I 
don't know) is represented in the binary number system, so it's evaluated as 
"(-3)**2". If x contained text and was evaluated as such, pure text replacement, 
then you should be able to write 2 x and have that evaluated as "2 -x"...



Cheers & hth.,

- Alf
--
http://mail.python.org/mailman/listinfo/python-list


Re: semantics of ** (unexpected/inconsistent?)

2009-11-29 Thread Ben Finney
Esmail  writes:

> Brian J Mingus wrote:
> >
> >
> >
> > I think you answered your own question. 3**2 comes first in the
> > order of operations, followed by the negation.
>
> No, that's not the problem, I'm ok with the operator precedence of - vs **
>
> My problem is why I don't get the same result if I use the literal -3
> or a variable that contains -3 (x in my example).

You seem to be confusing the values, which (in this case) are numbers,
with their incidental representation in Python code, which is literal
expressions in text. Perhaps the following dissection will help:

>>> -3**2

The expression ‘-3**2’ is evaluated, and a single value is the result.
In that expression, there are two operators: the unary-minus operator
and the exponential operator. There are two values as parameters in the
expression: the natural number three, and the natural number two.

>>> x = -3

The expression ‘-3’ is evaluated, and a single value is the result. That
value has an internal representation of “the integer that is three less
than zero”. It is *not* the sequence of characters that you see in the
Python code; it is a number.

The name ‘x’ is then bound to that value.

>>> x**2

The expression ‘x**2’ is evaluated, and a single value is the result. In
that expression, there is *one* operator: the exponential operator.
There is no unary-minus operator in the expression. There are two values
as parameters in the expression: the value referenced by the name ‘x’
which is the integer negative-three, and the natural number two.

I hope that explains the difference in behaviour.

-- 
 \  “I don't like country music, but I don't mean to denigrate |
  `\  those who do. And for the people who like country music, |
_o__)denigrate means ‘put down’.” —Bob Newhart |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: semantics of ** (unexpected/inconsistent?)

2009-11-29 Thread The Music Guy
It's just like in algebra. You evaluate exponents before the - which, after
all, is just another way to write -1, or times-negative-one. However, a
variable with a negative value is not the same as a value that is being
multiplied by a negative.

-3 ** 2   =   (-1)(3)^(2)  in algebraic terms. Exponents first, then
multiplication.

However,

x ** 2 = (x)^(2) = (-3)^(2)

regardless of the value of x which, in this case, is -3. When you multiply a
negative by itself, you get a positive.

In short, the ** operator appears to have a higher precedence than the -
operator, based on your results.


On Sun, Nov 29, 2009 at 6:39 PM, Esmail  wrote:

> Ok, this is somewhat unexpected:
>
> Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
> [GCC 4.3.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>
>
> >>> -3**2
> -9
>
> >>> x = -3
>
> >>> x**2
> 9
> >>>
>
> I would have expected the same result in both cases.
>
> Initially I would have expected -3**2 to yield 9, but I can accept
> that ** binds tighter than the unary -, but shouldn't the results
> be consistent regardless if I use a literal or a variable?
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: semantics of ** (unexpected/inconsistent?)

2009-11-29 Thread Martin v. Löwis
>> I think you answered your own question. 3**2 comes first in the order
>> of operations, followed by the negation.
> 
> No, that's not the problem, I'm ok with the operator precedence of - vs **
> 
> My problem is why I don't get the same result if I use the literal -3 or
> a variable that contains -3 (x in my example).

There is no literal -3 in Python, only a literal (+)3, see

http://docs.python.org/reference/lexical_analysis.html#integer-and-long-integer-literals

So -3**2 means -(3**2) == -9.

Regards,
Martin
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: semantics of ** (unexpected/inconsistent?)

2009-11-29 Thread Colin W.

On 29-Nov-09 19:50 PM, Chris Rebert wrote:

On Sun, Nov 29, 2009 at 4:39 PM, Esmail  wrote:

Ok, this is somewhat unexpected:

Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.



-3**2

-9


x = -3



x**2

9




I would have expected the same result in both cases.

Initially I would have expected -3**2 to yield 9, but I can accept
that ** binds tighter than the unary -, but shouldn't the results
be consistent regardless if I use a literal or a variable?


_No_, because using the variable evaluates "-3" as a unit separately
by itself, before the exponentiation ever occurs; it's the same as the
difference between (-3)**2 and -3**2.
Python is not a concatenative programming language[*]; you can't
directly textually replace a variable with its value and expect to get
the same result from an expression. For instance, in this case, you
need to add the parentheses.

Cheers,
Chris
--
http://blog.rebertia.com

[*] http://en.wikipedia.org/wiki/Concatenative_language

See the Operator Order of Precedence:
http://docs.python.org/reference/expressions.html#summary

Parentheses permit the user to vary the precedence.

Colin W.

--
http://mail.python.org/mailman/listinfo/python-list


Re: semantics of ** (unexpected/inconsistent?)

2009-11-29 Thread Esmail

Chris Rebert wrote:


_No_, because using the variable evaluates "-3" as a unit separately
by itself, before the exponentiation ever occurs; it's the same as the
difference between (-3)**2 and -3**2.
Python is not a concatenative programming language[*]; you can't
directly textually replace a variable with its value and expect to get
the same result from an expression. For instance, in this case, you
need to add the parentheses.

Cheers,
Chris
--
http://blog.rebertia.com

[*] http://en.wikipedia.org/wiki/Concatenative_language


Wow .. never heard of Concatenative_languages languages before or the
distinction you make. Your distinction explains the behavior, but I
find it somewhat counter-intuitive. (I use the Python interpreter frequently
for small calculations - otherwise I'd never have realized this)

Thanks,

Esmail

--
http://mail.python.org/mailman/listinfo/python-list


Re: semantics of ** (unexpected/inconsistent?)

2009-11-29 Thread Esmail

Brian J Mingus wrote:




I think you answered your own question. 3**2 comes first in the order of 
operations, followed by the negation.


No, that's not the problem, I'm ok with the operator precedence of - vs **

My problem is why I don't get the same result if I use the literal -3 or
a variable that contains -3 (x in my example).


--
http://mail.python.org/mailman/listinfo/python-list


Re: semantics of ** (unexpected/inconsistent?)

2009-11-29 Thread Chris Rebert
On Sun, Nov 29, 2009 at 4:39 PM, Esmail  wrote:
> Ok, this is somewhat unexpected:
>
> Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
> [GCC 4.3.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>
>
 -3**2
> -9
>
 x = -3
>
 x**2
> 9

>
> I would have expected the same result in both cases.
>
> Initially I would have expected -3**2 to yield 9, but I can accept
> that ** binds tighter than the unary -, but shouldn't the results
> be consistent regardless if I use a literal or a variable?

_No_, because using the variable evaluates "-3" as a unit separately
by itself, before the exponentiation ever occurs; it's the same as the
difference between (-3)**2 and -3**2.
Python is not a concatenative programming language[*]; you can't
directly textually replace a variable with its value and expect to get
the same result from an expression. For instance, in this case, you
need to add the parentheses.

Cheers,
Chris
--
http://blog.rebertia.com

[*] http://en.wikipedia.org/wiki/Concatenative_language
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: semantics of ** (unexpected/inconsistent?)

2009-11-29 Thread Brian J Mingus
On Sun, Nov 29, 2009 at 5:39 PM, Esmail  wrote:

> Ok, this is somewhat unexpected:
>
> Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
> [GCC 4.3.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>
>
> >>> -3**2
> -9
>
> >>> x = -3
>
> >>> x**2
> 9
> >>>
>
> I would have expected the same result in both cases.
>
> Initially I would have expected -3**2 to yield 9, but I can accept
> that ** binds tighter than the unary -, but shouldn't the results
> be consistent regardless if I use a literal or a variable?
>

I think you answered your own question. 3**2 comes first in the order of
operations, followed by the negation.

>>> (-3)**2
9
>>> 3**2
9
>>> -3**2
-9
-- 
http://mail.python.org/mailman/listinfo/python-list