f



Question on exponentiation

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 ?
0
Ron
12/18/2016 5:37:10 AM
comp.lang.forth 7147 articles. 0 followers. markrobertwills (871) is leader. Post Follow

16 Replies
917 Views

Similar Articles

[PageSpeed] 44

In article <o35791$99j$1@dont-email.me>,
Ron Aaron  <rambamist@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.

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

0
albert
12/18/2016 1:01:01 AM
On Saturday, December 17, 2016 at 11:38:00 PM UTC-6, Ron Aaron 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 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
0
Julian
12/18/2016 5:45:32 AM

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
0
Ron
12/18/2016 5:46:09 AM

On 12/18/2016 13:35, Albert van der Horst wrote:
> In article <o35791$99j$1@dont-email.me>,
> Ron Aaron  <rambamist@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.
0
Ron
12/18/2016 11:33:42 AM
In article <o35791$99j$1@dont-email.me>, rambamist@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/ 

=========================
0
john
12/18/2016 12:21:21 PM
Ron Aaron <rambamist@gmail.com> writes:

> On 12/18/2016 13:35, Albert van der Horst wrote:
>> In article <o35791$99j$1@dont-email.me>,
>> Ron Aaron  <rambamist@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.

I think python is correct. In mathematics -x² is not the same as (-x)².
0
Richmond
12/18/2016 6:07:24 PM
On 12/17/2016 11:37 PM, Ron Aaron 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 ?
>
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
0
Cecil
12/18/2016 6:14:47 PM
On 12/18/2016 6:33 AM, Ron Aaron wrote:
>
>
> On 12/18/2016 13:35, Albert van der Horst wrote:
>> In article <o35791$99j$1@dont-email.me>,
>> Ron Aaron  <rambamist@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.

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
0
rickman
12/18/2016 8:45:19 PM
On 12/18/2016 1:14 PM, Cecil Bayona wrote:
> On 12/17/2016 11:37 PM, Ron Aaron 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 ?
>>
> 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.

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
0
rickman
12/18/2016 8:48:02 PM
Ron Aaron <rambamist@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?
0
Paul
12/18/2016 10:26:21 PM
On 12/18/2016 5:26 PM, Paul Rubin wrote:
> Ron Aaron <rambamist@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 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
0
rickman
12/18/2016 10:43:26 PM
rickman <gnuarm@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.
0
Paul
12/18/2016 10:53:17 PM
On 12/18/2016 5:53 PM, Paul Rubin wrote:
> rickman <gnuarm@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.

 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
0
rickman
12/19/2016 12:12:27 AM
On Sunday, December 18, 2016 at 6:12:26 PM UTC-6, rickman wrote:
>  From wikipedia...
>=20
> "There exist differing conventions concerning the unary operator =E2=88=
=92=20
> (usually read "minus").

Python has an unary operator - , as demonstrated:

$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10)=20
[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=20
$ gforth -e '--4 . cr bye'  # why you confuse things?
4=20
$ gforth -e '---4 . cr bye'

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


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

$ vfxlin -4 . bye
4
$ vfxlin --4 . bye
Err# -13 ERR: Undefined word.
 Source: "/VFXBase/Vfx2.BLD" on line 0=20
 ->=20
   ^
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 =3D 2;;
val x : int =3D 2
# -x;;           <- unary - truly exists
- : int =3D -2

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

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

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


-- Julian
0
Julian
12/19/2016 12:56:32 AM
In article <85d1gp2js3.fsf@example.com>, Richmond  <dnomhcir@gmx.com> wrote:
>Ron Aaron <rambamist@gmail.com> writes:
>
>> On 12/18/2016 13:35, Albert van der Horst wrote:
>>> In article <o35791$99j$1@dont-email.me>,
>>> Ron Aaron  <rambamist@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.
>
>I think python is correct. In mathematics -x² is not the same as (-x)².

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.

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

0
albert
12/19/2016 1:01:01 AM
rickman <gnuarm@gmail.com> wrote:
> On 12/18/2016 5:26 PM, Paul Rubin wrote:
>> Ron Aaron <rambamist@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.
0
Andrew
12/19/2016 8:52:15 AM
Reply: