Re: Operator precedence

Liste des GroupesRevenir à cl awk 
Sujet : Re: Operator precedence
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.awk
Date : 23. May 2024, 17:49:15
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20240523092856.646@kylheku.com>
References : 1
User-Agent : slrn/pre1.0.4-9 (Linux)
On 2024-05-23, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
After reading an old article in the Algol bulletin about questions
and issues with operator precedences I tried this with (Gnu) Awk
>
$ awk 'BEGIN { a=5; print a^2, -a^2, 0-a^2 }'
25 -25 -25
>
and shell (Ksh)
>
$ a=5 ; print $(( a**2 )) $(( -a**2 )) $(( 0-a**2 ))
25 25 -25
>
and Algol 68
>
$ a68g -p 'INT a=5; (a^2, -a^2, 0-a^2)'
        +25        +25        -25
>
I don't want to value the different results (based on precedence),
but I'm interested in your comments/thoughts about the differences.

There is no question that we want the exponentiation operator
to have a higher precedence than plus or minus, even including
the unary forms. It's the "E" in the BEDMAS acronym that
English-speaking children learn in some places in the world: brackets,
exponentiation, division, multiplication, addition, subtracation.

If -a**2 is not parsed as -(a**2), you're messing with the BEDMAS.

Furthermore exponentation between on an intermediate precedence
level between unary minus and regular minus is simply insane.

You might think you're out of the woods with Lisp, where you don't
have precedence. But some math operations have variadic arguments,
so associativity comes into play.

Common Lisp restricts expt to exactly two arguments:

[1]> (expt 5 2)
25
[2]> (expt 5 2 3)

*** - EVAL: too many arguments given to EXPT: (EXPT 2 5 3)

I made it n-ary in TXR Lisp:

1> (expt 5 2)
25
2> (expt 5 2 3)
390625

Look, it's a right-to-left reduction, unlike addition, or multiplication:

1> (expt (expt 5 2) 3)
15625
2> (expt 5 (expt 2 3))
390625

In other words (expt a b c ...) denotes

     ...
    c
   b
  a

rather than

         b  c ...
 (.. ((a)  )     )

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Date Sujet#  Auteur
23 May 24 * Operator precedence27Janis Papanagnou
23 May 24 `* Re: Operator precedence26Kaz Kylheku
23 May 24  `* Re: Operator precedence25Axel Reichert
24 May 24   +* Re: Operator precedence21Janis Papanagnou
24 May 24   i+* Re: Operator precedence17Janis Papanagnou
24 May 24   ii`* Re: Operator precedence16Kaz Kylheku
25 May 24   ii +* Re: Operator precedence13Axel Reichert
25 May 24   ii i`* Re: Operator precedence12Janis Papanagnou
26 May 24   ii i `* Re: Operator precedence11Janis Papanagnou
26 May 24   ii i  +* Re: Operator precedence4Kaz Kylheku
30 May 24   ii i  i`* Re: Operator precedence3Janis Papanagnou
30 May 24   ii i  i `* Re: Operator precedence2Axel Reichert
31 May 24   ii i  i  `- Re: Operator precedence1Janis Papanagnou
26 May 24   ii i  +* Re: Operator precedence4Christian Weisgerber
26 May 24   ii i  i+* Re: Operator precedence2Kaz Kylheku
30 May 24   ii i  ii`- Re: Operator precedence1Janis Papanagnou
30 May 24   ii i  i`- Re: Operator precedence1Janis Papanagnou
30 May 24   ii i  `* Re: Operator precedence2Axel Reichert
31 May 24   ii i   `- Re: Operator precedence1Janis Papanagnou
25 May 24   ii `* Re: Operator precedence2Janis Papanagnou
26 May 24   ii  `- Re: Operator precedence1Kaz Kylheku
30 May 24   i`* Re: Operator precedence3Axel Reichert
31 May 24   i `* Re: Operator precedence2Janis Papanagnou
1 Jun 24   i  `- Re: Operator precedence1Axel Reichert
24 May 24   `* Re: Operator precedence3Kaz Kylheku
25 May 24    `* Re: Operator precedence2Axel Reichert
26 May 24     `- Re: Operator precedence1Kaz Kylheku

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal