Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #53401 > unrolled thread

Question on exponentiation

Started byRon Aaron <rambamist@gmail.com>
First post2016-12-18 07:37 +0200
Last post2016-12-18 15:48 -0500
Articles 17 — 9 participants

Back to article view | Back to comp.lang.forth


Contents

  Question on exponentiation Ron Aaron <rambamist@gmail.com> - 2016-12-18 07:37 +0200
    Re: Question on exponentiation Julian Fondren <julian.fondren@gmail.com> - 2016-12-17 21:45 -0800
      Re: Question on exponentiation Ron Aaron <rambamist@gmail.com> - 2016-12-18 07:46 +0200
    Re: Question on exponentiation albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2016-12-18 12:35 +0100
      Re: Question on exponentiation Ron Aaron <rambamist@gmail.com> - 2016-12-18 13:33 +0200
        Re: Question on exponentiation Richmond <dnomhcir@gmx.com> - 2016-12-18 18:07 +0000
          Re: Question on exponentiation albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2016-12-19 14:00 +0100
        Re: Question on exponentiation rickman <gnuarm@gmail.com> - 2016-12-18 15:45 -0500
        Re: Question on exponentiation Paul Rubin <no.email@nospam.invalid> - 2016-12-18 14:26 -0800
          Re: Question on exponentiation rickman <gnuarm@gmail.com> - 2016-12-18 17:43 -0500
            Re: Question on exponentiation Paul Rubin <no.email@nospam.invalid> - 2016-12-18 14:53 -0800
              Re: Question on exponentiation rickman <gnuarm@gmail.com> - 2016-12-18 19:12 -0500
                Re: Question on exponentiation Julian Fondren <julian.fondren@gmail.com> - 2016-12-18 16:56 -0800
            Re: Question on exponentiation Andrew Haley <andrew29@littlepinkcloud.invalid> - 2016-12-19 02:52 -0600
    Re: Question on exponentiation john <anon@example.com> - 2016-12-18 12:21 +0000
    Re: Question on exponentiation Cecil Bayona <cbayona@cbayona.com> - 2016-12-18 12:14 -0600
      Re: Question on exponentiation rickman <gnuarm@gmail.com> - 2016-12-18 15:48 -0500

#53401 — Question on exponentiation

FromRon Aaron <rambamist@gmail.com>
Date2016-12-18 07:37 +0200
SubjectQuestion on exponentiation
Message-ID<o35791$99j$1@dont-email.me>
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 ?

[toc] | [next] | [standalone]


#53402

FromJulian Fondren <julian.fondren@gmail.com>
Date2016-12-17 21:45 -0800
Message-ID<ef536e94-f7ee-4b05-83f7-bd2045609b76@googlegroups.com>
In reply to#53401
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

[toc] | [prev] | [next] | [standalone]


#53403

FromRon Aaron <rambamist@gmail.com>
Date2016-12-18 07:46 +0200
Message-ID<o357pq$a85$1@dont-email.me>
In reply to#53402

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

[toc] | [prev] | [next] | [standalone]


#53404

Fromalbert@cherry.spenarnc.xs4all.nl (Albert van der Horst)
Date2016-12-18 12:35 +0100
Message-ID<o35s9m$ssa$1@cherry.spenarnc.xs4all.nl>
In reply to#53401
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

[toc] | [prev] | [next] | [standalone]


#53405

FromRon Aaron <rambamist@gmail.com>
Date2016-12-18 13:33 +0200
Message-ID<o35s5g$vnu$1@dont-email.me>
In reply to#53404

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.

[toc] | [prev] | [next] | [standalone]


#53408

FromRichmond <dnomhcir@gmx.com>
Date2016-12-18 18:07 +0000
Message-ID<85d1gp2js3.fsf@example.com>
In reply to#53405
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)².

[toc] | [prev] | [next] | [standalone]


#53432

Fromalbert@cherry.spenarnc.xs4all.nl (Albert van der Horst)
Date2016-12-19 14:00 +0100
Message-ID<o38lkt$98t$1@cherry.spenarnc.xs4all.nl>
In reply to#53408
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

[toc] | [prev] | [next] | [standalone]


#53410

Fromrickman <gnuarm@gmail.com>
Date2016-12-18 15:45 -0500
Message-ID<o36se7$7vi$2@dont-email.me>
In reply to#53405
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

[toc] | [prev] | [next] | [standalone]


#53412

FromPaul Rubin <no.email@nospam.invalid>
Date2016-12-18 14:26 -0800
Message-ID<87oa0827si.fsf@nightsong.com>
In reply to#53405
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?

[toc] | [prev] | [next] | [standalone]


#53413

Fromrickman <gnuarm@gmail.com>
Date2016-12-18 17:43 -0500
Message-ID<o373bm$351$1@dont-email.me>
In reply to#53412
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

[toc] | [prev] | [next] | [standalone]


#53414

FromPaul Rubin <no.email@nospam.invalid>
Date2016-12-18 14:53 -0800
Message-ID<87k2aw26jm.fsf@nightsong.com>
In reply to#53413
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.

[toc] | [prev] | [next] | [standalone]


#53416

Fromrickman <gnuarm@gmail.com>
Date2016-12-18 19:12 -0500
Message-ID<o378ij$mb2$1@dont-email.me>
In reply to#53414
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

[toc] | [prev] | [next] | [standalone]


#53420

FromJulian Fondren <julian.fondren@gmail.com>
Date2016-12-18 16:56 -0800
Message-ID<18a8ca0f-4e00-4f06-8a0f-d12f8a1bd1c3@googlegroups.com>
In reply to#53416
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

[toc] | [prev] | [next] | [standalone]


#53427

FromAndrew Haley <andrew29@littlepinkcloud.invalid>
Date2016-12-19 02:52 -0600
Message-ID<R4ydnT4MhZMiAsrFnZ2dnUU78SnNnZ2d@supernews.com>
In reply to#53413
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.

[toc] | [prev] | [next] | [standalone]


#53407

Fromjohn <anon@example.com>
Date2016-12-18 12:21 +0000
Message-ID<MPG.32c08f9cb69088739896dd@news.virginmedia.com>
In reply to#53401
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/ 

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

[toc] | [prev] | [next] | [standalone]


#53409

FromCecil Bayona <cbayona@cbayona.com>
Date2016-12-18 12:14 -0600
Message-ID<o36jk6$8e6$1@dont-email.me>
In reply to#53401
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

[toc] | [prev] | [next] | [standalone]


#53411

Fromrickman <gnuarm@gmail.com>
Date2016-12-18 15:48 -0500
Message-ID<o36sja$7vi$3@dont-email.me>
In reply to#53409
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.forth


csiph-web