Path: ...!3.eu.feeder.erje.net!feeder.erje.net!news2.arglkargh.de!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Kaz Kylheku <643-408-1753@kylheku.com> Newsgroups: comp.lang.awk Subject: Re: Operator precedence Date: Thu, 23 May 2024 16:49:15 -0000 (UTC) Organization: A noiseless patient Spider Lines: 73 Message-ID: <20240523092856.646@kylheku.com> References: <v2nium$1pl8f$1@dont-email.me> Injection-Date: Thu, 23 May 2024 18:49:15 +0200 (CEST) Injection-Info: dont-email.me; posting-host="6aa71b7ebec593e21606490f7fbb0158"; logging-data="1949109"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19tJYguiBM1pNWQ8Dgsqxj3R+Kxcxa7yds=" User-Agent: slrn/pre1.0.4-9 (Linux) Cancel-Lock: sha1:myvXmltU/rugT9w2aqLfOBvj1qk= Bytes: 2867 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