Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Question on exponentiation

127 views
Skip to first unread message

Ron Aaron

unread,
Dec 18, 2016, 12:38:00 AM12/18/16
to
One of my users complained that in 8th, "-2 2 ^" gives "4" while python
(and google's calculator, etc) gives "-4".

I maintain that "-2 2 ^" is equivalent to "-2 -2 *" and therefore must
be "4". But clearly there are programs (and authors thereof) which
disagree.

Can anyone shed light on why python gives the answer it does, and
whether or not I'm just crazy to expect -2 2 ^ to give the answer 4 ?

Julian Fondren

unread,
Dec 18, 2016, 12:45:33 AM12/18/16
to
python gives the same answer -- if you ask it correctly:

# python
Python 2.7.12 (default, Jul 1 2016, 15:12:24)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> (-2)**2
4

** is evaluated before the -

Probably because the - is taken as a multiplication operation?


-- Julian

Ron Aaron

unread,
Dec 18, 2016, 12:46:56 AM12/18/16
to


On 12/18/2016 07:45, Julian Fondren wrote:

> python gives the same answer -- if you ask it correctly:
>
> # python
> Python 2.7.12 (default, Jul 1 2016, 15:12:24)
> [GCC 5.4.0 20160609] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> (-2)**2
> 4
>
> ** is evaluated before the -
>
> Probably because the - is taken as a multiplication operation?

Heh. You're right! And I just checked with "LibreOffice Calc" and it
gives the answer I expect as well.

So I'm vindicated...

Thanks

Albert van der Horst

unread,
Dec 18, 2016, 6:24:11 AM12/18/16
to
In article <o35791$99j$1...@dont-email.me>,
Python can't calculate "-2 2 ^", maybe "-2**2" . By having ** bind thighter
than -, the result ensues.

Of course your result is correct. Furthermore the Python decision is
dubious, it doesn't agree with the best thought language ever, Algol68.
There the rule is: All monadic operators bind more tightly then
dyadic operators.
They list this example as about the only somewhat unexpected one.

Algol68 has not the C-abomination of a combination of prefix and postfix
operators as in
* something ++
which could be simple have been avoided by having +- for pre
and ++ for post increment, both prefix (normal) operators

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

Ron Aaron

unread,
Dec 18, 2016, 6:34:31 AM12/18/16
to


On 12/18/2016 13:35, Albert van der Horst wrote:
> In article <o35791$99j$1...@dont-email.me>,
> Ron Aaron <ramb...@gmail.com> wrote:
>> One of my users complained that in 8th, "-2 2 ^" gives "4" while python
>> (and google's calculator, etc) gives "-4".
>>
>> I maintain that "-2 2 ^" is equivalent to "-2 -2 *" and therefore must
>> be "4". But clearly there are programs (and authors thereof) which
>> disagree.
>>
>> Can anyone shed light on why python gives the answer it does, and
>> whether or not I'm just crazy to expect -2 2 ^ to give the answer 4 ?
>
> Python can't calculate "-2 2 ^", maybe "-2**2" . By having ** bind thighter
> than -, the result ensues.

Yes, exactly. "**" binds tighter than monadic "-" in Python. You must
use parentheses to get the result I would expect naively.

> Of course your result is correct. Furthermore the Python decision is
> dubious, it doesn't agree with the best thought language ever, Algol68.
> There the rule is: All monadic operators bind more tightly then
> dyadic operators.

Interesting about Algol68; never had occasion to look at it, though I
was (, a long time ago) familiar with PL/1.

I think this simply illustrates a prime advantage of the "Forth way",
e.g. that there are no precedence rules and items are evaluated in the
order they appear.

john

unread,
Dec 18, 2016, 7:20:19 AM12/18/16
to
In article <o35791$99j$1...@dont-email.me>, ramb...@gmail.com says...
> I maintain that "-2 2 ^" is equivalent to "-2 -2 *" and therefore must
> be "4". But clearly there are programs (and authors thereof) which
> disagree.
>
>

A situation I once produced a solution for.
(The dissagreement issue not the specific equation)
I was argued down or just ignored.
Not long after an object missed a planet entirely
and went flying off into space.

The human ability to concentrate on the trivial
and ignore the vital has no bounds if it doesn't fit
with the existing world picture it seems.

--

john

=========================
http://johntech.co.uk

"Bleeding Edge Forum"
http://johntech.co.uk/forum/

=========================

Richmond

unread,
Dec 18, 2016, 1:07:26 PM12/18/16
to
I think python is correct. In mathematics -x² is not the same as (-x)².

Cecil Bayona

unread,
Dec 18, 2016, 1:14:53 PM12/18/16
to
Basic math 101 any negative number raised to an even power is positive,
raised to an odd power it is negative, that is for positive powers.

--
Cecil - k5nwa

rickman

unread,
Dec 18, 2016, 3:45:18 PM12/18/16
to
Uh, isn't that a precedence rule? In particular, the - is evaluated
first because it is part of the number, not really because it is first.

--

Rick C

rickman

unread,
Dec 18, 2016, 3:48:01 PM12/18/16
to
Yes, but as others have pointed out the issue is whether the OP's python
equation was the same as -2 squared or - (2 squared).

--

Rick C

Paul Rubin

unread,
Dec 18, 2016, 5:26:23 PM12/18/16
to
Ron Aaron <ramb...@gmail.com> writes:
> Yes, exactly. "**" binds tighter than monadic "-" in Python. You must
> use parentheses to get the result I would expect naively.

You really expect -x**2 and x**2 to be the same thing?

rickman

unread,
Dec 18, 2016, 5:43:25 PM12/18/16
to
What happens in other languages. I believe most do exactly that. In
fact, that is what the OP was confused about. Forth does what he
expected and Python did not.

--

Rick C

Paul Rubin

unread,
Dec 18, 2016, 5:53:19 PM12/18/16
to
rickman <gnu...@gmail.com> writes:
>> You really expect -x**2 and x**2 to be the same thing?
> What happens in other languages. I believe most do exactly that.

None that I can think of offhand do that. Similarly in mathematical
notation, -x**2 is -(x**2) where the **2 is written as a superscript.

rickman

unread,
Dec 18, 2016, 7:12:26 PM12/18/16
to
From wikipedia...

"There exist differing conventions concerning the unary operator −
(usually read "minus"). In written or printed mathematics, the
expression −32 is interpreted to mean 0 − (32) = −9,[1][5] but in some
applications and programming languages, notably Microsoft Office Excel
(and other spreadsheet applications) and the programming language bc,
unary operators have a higher priority than binary operators, that is,
the unary minus (negation or +/-) has higher precedence than
exponentiation, so in those languages −32 will be interpreted as (−3)2 =
9.[6]"

I noticed that C does not have an exponential operator. So they avoided
the issue altogether.

--

Rick C

Julian Fondren

unread,
Dec 18, 2016, 7:56:33 PM12/18/16
to
On Sunday, December 18, 2016 at 6:12:26 PM UTC-6, rickman wrote:
> From wikipedia...
>
> "There exist differing conventions concerning the unary operator −
> (usually read "minus").

Python has an unary operator - , as demonstrated:

$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> -4
-4
>>> --4
4
>>> ---4
-4
>>> ----4
4

Forth does not have this operator:

$ gforth -e '-4 . cr bye'
-4
$ gforth -e '--4 . cr bye' # why you confuse things?
4
$ gforth -e '---4 . cr bye'

*OS command line*:-1: Undefined word
>>>---4<<< . cr bye
Backtrace:
$7F16288A5A68 throw
$7F16288BBCE0 no.extensions
$7F16288A5D28 interpreter-notfound1


$ sf -4 . bye
-4
$ sf --4 . . bye # interpreted as a double number
-1 -4
$ sf ---4 . . bye
-1 -4

$ vfxlin -4 . bye
4
$ vfxlin --4 . bye
Err# -13 ERR: Undefined word.
Source: "/VFXBase/Vfx2.BLD" on line 0
->
^
ERROR on command line
Received SIGHUP or SIGTERM


In Forth the leading - is just part of how a number is expressed.

Some other languages use _ for Forth's purposes, and - for Python's.

O'Caml is an example of an infix language that has unary - but still
observably treats - in literals as part of the number, but then
*still* confuses things by giving unary - (and -.) higher precedence:

# let x = 2;;
val x : int = 2
# -x;; <- unary - truly exists
- : int = -2

# -2;;
- : int = -2
# --2;; <- that's not two of unary -
Error: Syntax error
# - - 2;;
- : int = 2

# -2.0**2.0;; <- shown below, this REALLY isn't unary -
- : float = 4.

# let x = 2.0;;
val x : float = 2.
# -x**2.0;;
Error: This expression has type float but an expression was expected of type
int
# -x;;
Error: This expression has type float but an expression was expected of type
int
# -.x;; <- unary -. (dot for float)
- : float = -2.
# -.x**2.0;;
- : float = 4.


-- Julian

Andrew Haley

unread,
Dec 19, 2016, 3:52:16 AM12/19/16
to
rickman <gnu...@gmail.com> wrote:
> On 12/18/2016 5:26 PM, Paul Rubin wrote:
>> Ron Aaron <ramb...@gmail.com> writes:
>>> Yes, exactly. "**" binds tighter than monadic "-" in Python. You must
>>> use parentheses to get the result I would expect naively.
>>
>> You really expect -x**2 and x**2 to be the same thing?
>
> What happens in other languages.

I don't think so: in FORTRAN and Ada and exponentiation binds tighter
than negation.

Python even gets this right:

>>> 4 ** 3 ** 2
262144

Top marks!

Andrew.

Albert van der Horst

unread,
Dec 19, 2016, 7:49:47 AM12/19/16
to
On the contrary. Python is fundamentally wrong trying to make mathematicale
expression work "as is".
Nowadays Python understands " 1<x<=10 ".
Every reasoning about how you must parse that is futile.
You must learn by rote for each expression how it works.
We're back to Windows : you must know for a couple of 100
hieroglyphs ("icons") what they mean.

OTOH Algol68 has not even half a dozen constructs/concepts.
0 new messages