Groups | Search | Server Info | Login | Register


Groups > comp.lang.awk > #9750

Re: Operator precedence

From Kaz Kylheku <643-408-1753@kylheku.com>
Newsgroups comp.lang.awk
Subject Re: Operator precedence
Date 2024-05-23 16:49 +0000
Organization A noiseless patient Spider
Message-ID <20240523092856.646@kylheku.com> (permalink)
References <v2nium$1pl8f$1@dont-email.me>

Show all headers | View raw


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

Back to comp.lang.awk | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Operator precedence Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-05-23 16:13 +0200
  Re: Operator precedence Kaz Kylheku <643-408-1753@kylheku.com> - 2024-05-23 16:49 +0000
    Re: Operator precedence Axel Reichert <mail@axel-reichert.de> - 2024-05-23 19:14 +0200
      Re: Operator precedence Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-05-24 01:53 +0200
        Re: Operator precedence Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-05-24 04:41 +0200
          Re: Operator precedence Kaz Kylheku <643-408-1753@kylheku.com> - 2024-05-24 04:32 +0000
            Re: Operator precedence Axel Reichert <mail@axel-reichert.de> - 2024-05-25 13:34 +0200
              Re: Operator precedence Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-05-25 23:31 +0200
                Re: Operator precedence Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-05-26 00:03 +0200
                Re: Operator precedence Kaz Kylheku <643-408-1753@kylheku.com> - 2024-05-25 22:26 +0000
                Re: Operator precedence Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-05-30 06:59 +0200
                Re: Operator precedence Axel Reichert <mail@axel-reichert.de> - 2024-05-30 10:24 +0200
                Re: Operator precedence Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-05-31 17:08 +0200
                Re: Operator precedence Christian Weisgerber <naddy@mips.inka.de> - 2024-05-26 00:37 +0000
                Re: Operator precedence Kaz Kylheku <643-408-1753@kylheku.com> - 2024-05-26 02:06 +0000
                Re: Operator precedence Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-05-30 07:26 +0200
                Re: Operator precedence Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-05-30 07:06 +0200
                Re: Operator precedence Axel Reichert <mail@axel-reichert.de> - 2024-05-30 10:22 +0200
                Re: Operator precedence Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-05-31 17:16 +0200
            Re: Operator precedence Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-05-25 23:31 +0200
              Re: Operator precedence Kaz Kylheku <643-408-1753@kylheku.com> - 2024-05-26 01:29 +0000
        Re: Operator precedence Axel Reichert <mail@axel-reichert.de> - 2024-05-30 10:17 +0200
          Re: Operator precedence Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-05-31 17:46 +0200
            Re: Operator precedence Axel Reichert <mail@axel-reichert.de> - 2024-06-01 09:11 +0200
      Re: Operator precedence Kaz Kylheku <643-408-1753@kylheku.com> - 2024-05-24 03:45 +0000
        Re: Operator precedence Axel Reichert <mail@axel-reichert.de> - 2024-05-25 13:09 +0200
          Re: Operator precedence Kaz Kylheku <643-408-1753@kylheku.com> - 2024-05-25 22:20 +0000

csiph-web