Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

The amount of weight an evangelist carries with the almighty is measured in billigrahams.


devel / comp.lang.c / Re: operator precedence

SubjectAuthor
* operator precedenceStefan Ram
+* Re: operator precedenceJames Kuyper
|+- Re: operator precedenceBen Bacarisse
|+* Re: operator precedenceSiri Cruise
||`* Re: operator precedencejames...@alumni.caltech.edu
|| `* Re: operator precedenceAndrey Tarasevich
||  +- Re: operator precedenceJames Kuyper
||  `- Re: operator precedenceJames Kuyper
|+- Re: operator precedenceScott Lurndal
|`- Re: operator precedenceBart
+- Re: operator precedenceAndrey Tarasevich
+* Re: operator precedenceStefan Ram
|`* Re: operator precedenceMike Terry
| `* Re: operator precedenceMike Terry
|  `* Re: operator precedenceStefan Ram
|   `- Re: operator precedenceStefan Ram
+* Re: operator precedenceChristian Hanné
|+* Re: operator precedenceKenny McCormack
||`* Re: operator precedenceKaz Kylheku
|| `- Shhh!!! (Was: operator precedence)Kenny McCormack
|`* Re: operator precedenceKeith Thompson
| `* Re: operator precedenceChristian Hanné
|  `* Re: operator precedencejames...@alumni.caltech.edu
|   +- Re: operator precedenceChristian Hanné
|   `* Re: operator precedenceKeith Thompson
|    `* Re: operator precedenceChristian Hanné
|     `* Re: operator precedenceKenny McCormack
|      `* Re: operator precedenceManfred
|       `- Re: operator precedenceDavid Brown
+* Re: operator precedenceGuillaume
|+* Re: operator precedenceStefan Ram
||+- Re: operator precedenceVir Campestris
||+* Re: operator precedenceStefan Ram
|||`- Re: operator precedenceDavid Brown
||+- Re: operator precedenceKenny McCormack
||`- Re: operator precedenceGuillaume
|+* Re: operator precedenceBen
||`* Re: operator precedenceGuillaume
|| `* Re: operator precedenceBen
||  +* Re: operator precedenceDavid Brown
||  |`* Re: operator precedenceBen
||  | `- Re: operator precedenceDavid Brown
||  `* Re: operator precedenceGuillaume
||   `* Re: operator precedenceBen
||    `* Re: operator precedenceDavid Brown
||     +* Re: operator precedenceKaz Kylheku
||     |`* Re: operator precedenceDavid Brown
||     | `* Re: operator precedenceKaz Kylheku
||     |  `- Re: operator precedenceDavid Brown
||     `* Re: operator precedenceJames Kuyper
||      `* Re: operator precedenceTim Rentsch
||       `- Re: operator precedencejames...@alumni.caltech.edu
|+- Re: operator precedenceBonita Montero
|`* Re: operator precedenceTim Rentsch
| +* Re: operator precedenceDavid Brown
| |`* Re: operator precedenceBen
| | `* Re: operator precedenceDavid Brown
| |  +* Re: operator precedenceBen
| |  |+* Re: operator precedenceDavid Brown
| |  ||`* Re: operator precedenceSams Lara
| |  || `* Re: operator precedenceDavid Brown
| |  ||  `* Re: operator precedenceBen
| |  ||   `* Re: operator precedenceDavid Brown
| |  ||    `* Re: operator precedenceBen
| |  ||     +* Re: operator precedenceDavid Brown
| |  ||     |+* Re: operator precedenceGiovanni
| |  ||     ||+* Re: operator precedenceDavid Brown
| |  ||     |||`- Re: operator precedenceMalcolm McLean
| |  ||     ||`- Re: operator precedenceStefan Ram
| |  ||     |`* Re: operator precedenceBen
| |  ||     | `* Re: operator precedenceDavid Brown
| |  ||     |  +* Re: operator precedenceDan Purgert
| |  ||     |  |`* Re: operator precedenceStefan Ram
| |  ||     |  | `- Re: operator precedenceDan Purgert
| |  ||     |  +- Re: operator precedenceBen
| |  ||     |  `* Re: operator precedenceRichard Harnden
| |  ||     |   `- Re: operator precedenceDavid Brown
| |  ||     `* Re: operator precedenceKaz Kylheku
| |  ||      `* Re: operator precedenceDavid Brown
| |  ||       +* Re: operator precedenceMalcolm McLean
| |  ||       |`- Re: operator precedenceDavid Brown
| |  ||       `* Re: operator precedenceJames Kuyper
| |  ||        `- Re: operator precedenceDavid Brown
| |  |`* Re: operator precedencebart c
| |  | +- Re: operator precedenceBen
| |  | `- Re: operator precedenceDavid Brown
| |  `- Re: operator precedenceÖö Tiib
| `* Re: operator precedenceScott Lurndal
|  `* Re: operator precedenceBen
|   `- Re: operator precedenceTim Rentsch
`- Re: operator precedenceTim Rentsch

Pages:1234
Re: operator precedence

<t3n6qa$fi9$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21303&group=comp.lang.c#21303

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: operator precedence
Date: Tue, 19 Apr 2022 22:41:45 +0200
Organization: A noiseless patient Spider
Lines: 181
Message-ID: <t3n6qa$fi9$1@dont-email.me>
References: <thought-20220408125437@ram.dialup.fu-berlin.de>
<t2v61c$ej9$1@gioia.aioe.org> <87bkx868cm.fsf@bsb.me.uk>
<t31tda$1ohm$1@gioia.aioe.org> <878rsb4b8g.fsf@bsb.me.uk>
<t3c9vm$d1r$1@gioia.aioe.org> <87mtgmp5kr.fsf@bsb.me.uk>
<t3jkir$9t7$1@dont-email.me> <20220418091807.644@kylheku.com>
<t3k6l3$j4d$1@dont-email.me> <20220419102826.181@kylheku.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 19 Apr 2022 20:41:46 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="cc92be7ae92ac62ec34af5ef40d31c7d";
logging-data="15945"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+zcL6nllBSXbszXTQUZBTB6MaVa9O6NvE="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:53xnyZ1gaDcnbOkoUrWlBTTmIh4=
In-Reply-To: <20220419102826.181@kylheku.com>
Content-Language: en-GB
 by: David Brown - Tue, 19 Apr 2022 20:41 UTC

On 19/04/2022 19:40, Kaz Kylheku wrote:
> On 2022-04-18, David Brown <david.brown@hesbynett.no> wrote:
>> On 18/04/2022 18:38, Kaz Kylheku wrote:
>>> On 2022-04-18, David Brown <david.brown@hesbynett.no> wrote:
>>>> On 15/04/2022 20:25, Ben wrote:
>>>>> Guillaume <message@bottle.org> writes:
>>>>>
>>>>>> Le 11/04/2022 à 22:27, Ben a écrit :
>>>>>>> I shall have to remain in the dark then about what you consider a good
>>>>>>> start because the quote "Limited dependence should be placed on operator
>>>>>>> precedence rules in expressions" I think MISRA costs money.
>>>>>>
>>>>>> If you use MISRA in a professional context, you should of course buy a
>>>>>> copy, but if you're just curious or it's for your own personal use,
>>>>>> then it's easy to get ahold of a copy.
>>>>>>
>>>>>> But I explicitely mentioned the rule number that applied here, and
>>>>>> with this number, you'll find the details online very easily without
>>>>>> needing to get the whole document.
>>>>>
>>>>> I didn't succeed. Only spent a few minutes but all I could find is
>>>>> people quoting some (or many all of) that rule in compliance statements.
>>>>>
>>>>> One says:
>>>>>
>>>>> "MISRA C:2012 Rule 12.1: The precedence of operators within
>>>>> expressions should be made explicit"
>>>>>
>>>>> but that can't be the whole rule and, as quoted, it sounds very odd.
>>>>>
>>>>
>>>> It is not the whole rule, which is fleshed out more and has a number of
>>>> examples. It is also an "advisory" rule, rather than a "required" rule,
>>>> and it contains the rationale note:
>>>>
>>>> """
>>>> It is recognized that overuse of parentheses can clutter the code and
>>>> reduce its readability. This rule aims to achieve a compromise between
>>>> code that is hard to understand because it contains either too many or
>>>> too few parentheses.
>>>> """
>>>>
>>>> Whether or not you think it succeeds in this aim is, clearly, a matter
>>>> of opinion!
>>>>
>>>> An example of a non-compliant expression is :
>>>>
>>>> x = a == b ? a : a - b;
>>>>
>>>> with the compliant version being :
>>>>
>>>> x = ( a == b ) ? a : ( a - b );
>>>
>>> This is actually a really poor example.
>>>
>>> What if I don't remember whether = has a lower precedence than ?:
>>
>> You have to require /some/ knowledge of C. Assignments are the lowest
>> precedence (other than the comma operator, which MISRA bans or at least
>> strongly discourages anyway). Similarly, MISRA expects you to
>> understand that the unary operators have higher precedence.
>>
>>>
>>> If your answer is that it obviously does, then we can easily
>>> argue that, likewise, == and - "obviously" have a higher precedence, and
>>> so nothing needs parenthesizing.
>>>
>>> Rather, if I were only allowed to insert one set of parentheses, I would
>>> prioritize the insertion of this pair:
>>>
>>> x = (a == b ? a : a - b)
>>>
>>> Then if additional parens were still needed:
>>>
>>> x = ((a == b) ? (a) : (a - b))
>>>
>>> The inner one is for consistency: a is parallel with a - b.
>>> This is analogous to bracing both branches of if/else if one
>>> branch is braced.
>>>
>>
>> To me, this is not so much a matter of remembering the precedence rules
>> as being able to tell at a glance what the expression means - and making
>> it as difficult as possible for (almost) anyone to misinterpret it when
>> reading it, even if they have different habits for spacing or
>> parenthesising.
>>
>> I believe it is highly unlikely for "x=a + b;" to be misinterpreted,
>> thus adding parenthesis is unhelpful. But "x=a == b?a:a-b;" could
>> certainly lead to confusion, or at least longer parse times - putting
>> parenthesis as suggested by MISRA would help, putting them where you
>> suggested would not.
>
> Sure, OK. But then, what is the point of the "x = " in the MISRA
> example?
>
> x = ( a == b ) ? a : ( a - b );
>
> The possibility remains that this be misread as:
>
> (x = ( a == b )) ? a : ( a - b );
>
> Why is the assignment included in this example; if its apparent
> ambiguity isn't being taken care of?

As I said - /some/ knowledge of C is taken as given. The details of the
rule specifically talk about operators of precedence higher than
assignment - the reader is expected to understand that assignment is the
lowest precedence (MISRA bans the comma operator).

(I'm not saying this is necessarily a perfect choice - merely that it is
the choice MISRA made.)

>
> What it is that the reader is supposed to be confused by if we remove
> the left pair of parentheses.
>
> x = a == b ? a : ( a - b );
>
> Are the parentheses supposed to rule out this misparse?
>
> x = a == (b ? a : ( a - b )).
>
> If so, that can be demonstrated without the x = because it's
> an internal matter within just ternary expression.
>
> Or is it this misparse:
>
> (x = a) == b ? a : ( a - b ).

I did not write MISRA - I quoted from it. There are endless ways to
misinterpret things, intentionally and unintentionally, whether it be
statements or expressions in C, or rules in a coding standard.

>
> But that is an issue between just the = and == operator that
> can be put into a simpler recommendation, like
>
> "parenthesize equality/inequality expressions that are next to
> assignment operators. Don't write:
>
> flag = foo == bar
>
> but
>
> flag = (foo == bar)
>
> or
>
> row += i % 10 == 0
>
> but rather
>
> row += (i % 10 == 0)"

I'd agree that these are good rules.

>
> The thing is that x = y ? z : w is actually a famous ambiguity,
> which revolves around the precedence of = versus ?:.
> Someone who thinks = is higher misreads it as (x = y) ? z : w.
> This can be a valid interpretation of z and w have side effects.
>

MISRA already bans the use of the result of the assignment operator.
(Some people will disagree with that one, others will agree - it is not
without reason that many programming languages have assignment as a
statement, not an expression.)

> Parenthesizing y does nothing: x = (y) ? z : w.
>
> You need: x = (y ? z : w). Aha, only y is tested, and then
> x receives either z or w.
>

I think you could reasonably argue that "x = (y ? z : w);" is clearer
than without the parentheses. But for the original expression, I'd want
parentheses as MISRA had them: "x = (a == b) ? a : (a - b);". I think
an extra set of parentheses around the whole assigned expression would
add clutter rather than clarity.

Re: operator precedence

<86o80p6uj0.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21457&group=comp.lang.c#21457

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: operator precedence
Date: Mon, 25 Apr 2022 08:40:35 -0700
Organization: A noiseless patient Spider
Lines: 24
Message-ID: <86o80p6uj0.fsf@linuxsc.com>
References: <thought-20220408125437@ram.dialup.fu-berlin.de> <t2v61c$ej9$1@gioia.aioe.org> <87bkx868cm.fsf@bsb.me.uk> <t31tda$1ohm$1@gioia.aioe.org> <878rsb4b8g.fsf@bsb.me.uk> <t3c9vm$d1r$1@gioia.aioe.org> <87mtgmp5kr.fsf@bsb.me.uk> <t3jkir$9t7$1@dont-email.me> <t3l92o$pvr$2@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="2b2bd3060d7fb14ae545fc6b32585799";
logging-data="17399"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18reHCoqVFV411C7kLjWyta73bYuWjYYSI="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:MWrpHxcmJFzIaK3zUIglIiJbZ6c=
sha1:OKZLCK++2Y4qmsWuKD24S1Nv+mI=
 by: Tim Rentsch - Mon, 25 Apr 2022 15:40 UTC

James Kuyper <jameskuyper@alumni.caltech.edu> writes:

[...]

> This reminds be a discussion [...] about
> MISRA Rule 34 (required):
> "The operands of a logical && or || shall be primary expressions
> if the operands contain binary operators."
>
> I pointed out that this rule mandates that at least one pair of
> parentheses be inserted in the following expression:
>
> (a && b) && c
>
> because '&&' is a binary operator, and a && b is the left operand
> of the second && operator.
>
> I also pointed out that, "the operands contain" could either be
> interpreted as meaning "both operands contain", which would allow
> a + b && c, or "either operand contains", which would require
> (a+b) && (c).

The stated rule -- under the "either" interpretation -- requires
(a+b) && c, not (a+b) && (c).

Re: operator precedence

<86fsm16tvi.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21460&group=comp.lang.c#21460

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: operator precedence
Date: Mon, 25 Apr 2022 08:54:41 -0700
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <86fsm16tvi.fsf@linuxsc.com>
References: <thought-20220408125437@ram.dialup.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="2b2bd3060d7fb14ae545fc6b32585799";
logging-data="17399"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/bWvjmX/2H+SgO6hbR4HtmiPRi3FIZB7U="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:D2nIF2i/usvijbV49ieD2PBXKH8=
sha1:5fP/kt4Y/6orMIGYd+1SucnFNK0=
 by: Tim Rentsch - Mon, 25 Apr 2022 15:54 UTC

ram@zedat.fu-berlin.de (Stefan Ram) writes:

> Recently, I wrote:
>
> board & 1 << row * COLS + col
>
> . Not knowing the precedence of operators by heart,
> I planned to look up the precedence rules and then insert
> any necessary parentheses. But then, I had this thought:
> "C was made for such expression; just trust the language.".

In conjunction with the other thread on operator precedence, I am
interested to ask about what people know without having to look
it up. So my question is, which operators, or precedence levels,
do you feel confident that you can say correctly how C parses
expressions that have those operators? (Feel free to indicate
partial understanding in some cases if that is applicable.)

Note that "precedence" here is meant as a stand-in for more general
parsing relationships, such as associativity, asymmetry, etc.

Re: operator precedence

<fba91f86-0f09-4444-a4e8-41ece98978a8n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21461&group=comp.lang.c#21461

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:6214:21c1:b0:450:5583:6595 with SMTP id d1-20020a05621421c100b0045055836595mr12857638qvh.130.1650908156551;
Mon, 25 Apr 2022 10:35:56 -0700 (PDT)
X-Received: by 2002:a05:622a:1818:b0:2f3:6491:c41e with SMTP id
t24-20020a05622a181800b002f36491c41emr5638355qtc.74.1650908156303; Mon, 25
Apr 2022 10:35:56 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Mon, 25 Apr 2022 10:35:56 -0700 (PDT)
In-Reply-To: <86o80p6uj0.fsf@linuxsc.com>
Injection-Info: google-groups.googlegroups.com; posting-host=108.48.119.9; posting-account=Ix1u_AoAAAAILVQeRkP2ENwli-Uv6vO8
NNTP-Posting-Host: 108.48.119.9
References: <thought-20220408125437@ram.dialup.fu-berlin.de>
<t2v61c$ej9$1@gioia.aioe.org> <87bkx868cm.fsf@bsb.me.uk> <t31tda$1ohm$1@gioia.aioe.org>
<878rsb4b8g.fsf@bsb.me.uk> <t3c9vm$d1r$1@gioia.aioe.org> <87mtgmp5kr.fsf@bsb.me.uk>
<t3jkir$9t7$1@dont-email.me> <t3l92o$pvr$2@dont-email.me> <86o80p6uj0.fsf@linuxsc.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <fba91f86-0f09-4444-a4e8-41ece98978a8n@googlegroups.com>
Subject: Re: operator precedence
From: jameskuy...@alumni.caltech.edu (james...@alumni.caltech.edu)
Injection-Date: Mon, 25 Apr 2022 17:35:56 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 25
 by: james...@alumni.calt - Mon, 25 Apr 2022 17:35 UTC

On Monday, April 25, 2022 at 11:40:47 AM UTC-4, Tim Rentsch wrote:
> James Kuyper <james...@alumni.caltech.edu> writes:
>
> [...]
>
> > This reminds be a discussion [...] about
> > MISRA Rule 34 (required):
> > "The operands of a logical && or || shall be primary expressions
> > if the operands contain binary operators."
....
> > I also pointed out that, "the operands contain" could either be
> > interpreted as meaning "both operands contain", which would allow
> > a + b && c, or "either operand contains", which would require
> > (a+b) && (c).
> The stated rule -- under the "either" interpretation -- requires
> (a+b) && c, not (a+b) && (c).

Sorry - to make my point properly, I should have used a right operand
which was not, itself, a primary-expression. Under the second
interpretation, that rule requires that
a + b && c[5]

be written as

(a+b) && (c[5])

Re: operator precedence

<86v8ux58zx.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21466&group=comp.lang.c#21466

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: operator precedence
Date: Mon, 25 Apr 2022 11:10:58 -0700
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <86v8ux58zx.fsf@linuxsc.com>
References: <thought-20220408125437@ram.dialup.fu-berlin.de> <t2v61c$ej9$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="2b2bd3060d7fb14ae545fc6b32585799";
logging-data="20117"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX195G8NijEAk0s7O7n1zhOvw0pcsB/5beBU="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:AoYT50TtlfX6GziY1+ZedD1jqt8=
sha1:1T8QC2zNYbXSS+6YT4VDA1tCKRg=
 by: Tim Rentsch - Mon, 25 Apr 2022 18:10 UTC

Guillaume <message@bottle.org> writes:

> Le 08/04/2022 at 14:00, Stefan Ram a ecrit:
>
>> Recently, I wrote:
>>
>> board & 1 << row * COLS + col
>
> If I saw this coming from someone in my team, I would probably fire them. =)
>
> I'm not even sure this is correct from what you really intended. But
> even if it is, there's a rule in programming, IMO, that's above the
> programming language's grammar: readability for us humans.
>
> Code we write is meant for humans, not machines. That's something
> people forget way too often. That's why we write using higher-level
> languages, not assembly or even machine code. Or, look at the
> obfuscated C contest and such. This is proper C from a language
> standpoint, but nothing you'd want to deal with.
>
> The readability rule that applies here is, if you need more than a few
> seconds figuring out what a given statement exactly does, and may have
> to even open the C standard to make sure, then it's badly
> written. Rewrite it.

Human readability can be improved as follows:

board & 1 << row*COLS+col

Re: operator precedence

<t46qkv$ull$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21467&group=comp.lang.c#21467

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: operator precedence
Date: Mon, 25 Apr 2022 20:52:14 +0200
Organization: A noiseless patient Spider
Lines: 54
Message-ID: <t46qkv$ull$1@dont-email.me>
References: <thought-20220408125437@ram.dialup.fu-berlin.de>
<t2v61c$ej9$1@gioia.aioe.org> <86v8ux58zx.fsf@linuxsc.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 25 Apr 2022 18:52:15 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="c3804977770cde54575c2b2ac3ea9754";
logging-data="31413"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18mlEhHeyFUQ1QWIaVOT+Mk5XOWbh8Wkw0="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:yxecqDxMgR3rOKLvtQjKOU7b2t0=
In-Reply-To: <86v8ux58zx.fsf@linuxsc.com>
Content-Language: en-GB
 by: David Brown - Mon, 25 Apr 2022 18:52 UTC

On 25/04/2022 20:10, Tim Rentsch wrote:
> Guillaume <message@bottle.org> writes:
>
>> Le 08/04/2022 at 14:00, Stefan Ram a ecrit:
>>
>>> Recently, I wrote:
>>>
>>> board & 1 << row * COLS + col
>>
>> If I saw this coming from someone in my team, I would probably fire them. =)
>>
>> I'm not even sure this is correct from what you really intended. But
>> even if it is, there's a rule in programming, IMO, that's above the
>> programming language's grammar: readability for us humans.
>>
>> Code we write is meant for humans, not machines. That's something
>> people forget way too often. That's why we write using higher-level
>> languages, not assembly or even machine code. Or, look at the
>> obfuscated C contest and such. This is proper C from a language
>> standpoint, but nothing you'd want to deal with.
>>
>> The readability rule that applies here is, if you need more than a few
>> seconds figuring out what a given statement exactly does, and may have
>> to even open the C standard to make sure, then it's badly
>> written. Rewrite it.
>
> Human readability can be improved as follows:
>
> board & 1 << row*COLS+col
>

If I saw that coming from someone in my team, I would treat it as worse
than the original. Anyone who thinks mixing inconsistent spacing into
this improves things, has completely missed the point (unless they are
using some programming language that considers the number of spaces in
an expression as significant).

For code clarity, it is important that the compiler and human readers
agree on what the code means, that the human can easily see what it
means, and easily see that the compiler will view it the same way.
Changing the spacing does not improve things if the compiler and the
human had different ideas of what the code meant - all it does is make
it marginally clearer to readers what the writer thinks it means, at the
expense of making it harder to see any possible errors.

If you think the first expression was clear enough, that's a reasonable
position - others disagree, but it can depend on the kind of programming
and the kind of programmers involved. But IMHO if the first expression
is not considered clear enough for the needs of the programming task in
hand, then the second version is worse.

(I don't expect Tim to read this, and certainly not to reply to it, as I
am on his naughty list - but others may read it.)

Re: operator precedence

<KeC9K.123254$Kdf.96421@fx96.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21468&group=comp.lang.c#21468

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!peer01.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx96.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: operator precedence
Newsgroups: comp.lang.c
References: <thought-20220408125437@ram.dialup.fu-berlin.de> <t2v61c$ej9$1@gioia.aioe.org> <86v8ux58zx.fsf@linuxsc.com>
Lines: 45
Message-ID: <KeC9K.123254$Kdf.96421@fx96.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Mon, 25 Apr 2022 19:02:02 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Mon, 25 Apr 2022 19:02:02 GMT
X-Received-Bytes: 2186
 by: Scott Lurndal - Mon, 25 Apr 2022 19:02 UTC

Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>Guillaume <message@bottle.org> writes:
>
>> Le 08/04/2022 at 14:00, Stefan Ram a ecrit:
>>
>>> Recently, I wrote:
>>>
>>> board & 1 << row * COLS + col
>>
>> If I saw this coming from someone in my team, I would probably fire them. =)
>>
>> I'm not even sure this is correct from what you really intended. But
>> even if it is, there's a rule in programming, IMO, that's above the
>> programming language's grammar: readability for us humans.
>>
>> Code we write is meant for humans, not machines. That's something
>> people forget way too often. That's why we write using higher-level
>> languages, not assembly or even machine code. Or, look at the
>> obfuscated C contest and such. This is proper C from a language
>> standpoint, but nothing you'd want to deal with.
>>
>> The readability rule that applies here is, if you need more than a few
>> seconds figuring out what a given statement exactly does, and may have
>> to even open the C standard to make sure, then it's badly
>> written. Rewrite it.
>
>Human readability can be improved as follows:
>
> board & 1 << row*COLS+col

Or,

inline _Bool
test_bit(uint64_t qword, size_t bitnum)
{ return (_Bool) (qword & (1ul << bitnum)) != 0;
}

if (test_bit(board, row*COLS+col)) {
...
}

Easily extended to support an array of uint64_t
when ROWS*COLS > sizeof(uint64_t) * BITS_PER_BYTE.

Re: operator precedence

<87ee1kudbm.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21469&group=comp.lang.c#21469

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben)
Newsgroups: comp.lang.c
Subject: Re: operator precedence
Date: Mon, 25 Apr 2022 21:18:21 +0100
Organization: A noiseless patient Spider
Lines: 59
Message-ID: <87ee1kudbm.fsf@bsb.me.uk>
References: <thought-20220408125437@ram.dialup.fu-berlin.de>
<t2v61c$ej9$1@gioia.aioe.org> <86v8ux58zx.fsf@linuxsc.com>
<t46qkv$ull$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="8cb2b4d3b883353eb1a22bfc55af833c";
logging-data="8812"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18wgK27FvVnE2Qa23rDDEr1JFxPqMyXSps="
Cancel-Lock: sha1:j9LktMpXOipesNzwUGrxV74rJbI=
sha1:U7DA/MeObcGAkgd+izyLqmtEuUs=
X-BSB-Auth: 1.46beac8ee95bf58c42b6.20220425211821BST.87ee1kudbm.fsf@bsb.me.uk
 by: Ben - Mon, 25 Apr 2022 20:18 UTC

David Brown <david.brown@hesbynett.no> writes:

> On 25/04/2022 20:10, Tim Rentsch wrote:
>> Guillaume <message@bottle.org> writes:
>>
>>> Le 08/04/2022 at 14:00, Stefan Ram a ecrit:
>>>
>>>> Recently, I wrote:
>>>>
>>>> board & 1 << row * COLS + col
>>>
>>> If I saw this coming from someone in my team, I would probably fire them. =)
>>>
>>> I'm not even sure this is correct from what you really intended. But
>>> even if it is, there's a rule in programming, IMO, that's above the
>>> programming language's grammar: readability for us humans.
>>>
>>> Code we write is meant for humans, not machines. That's something
>>> people forget way too often. That's why we write using higher-level
>>> languages, not assembly or even machine code. Or, look at the
>>> obfuscated C contest and such. This is proper C from a language
>>> standpoint, but nothing you'd want to deal with.
>>>
>>> The readability rule that applies here is, if you need more than a few
>>> seconds figuring out what a given statement exactly does, and may have
>>> to even open the C standard to make sure, then it's badly
>>> written. Rewrite it.
>>
>> Human readability can be improved as follows:
>>
>> board & 1 << row*COLS+col
>
> If I saw that coming from someone in my team, I would treat it as worse
> than the original. Anyone who thinks mixing inconsistent spacing into
> this improves things, has completely missed the point (unless they are
> using some programming language that considers the number of spaces in
> an expression as significant).

You presumably think that there is only one notion of consistent
spacing, but spacing to show operator binding is consistent
with... well, with doing so. I've seen it quite a lot. I do it myself
even when it hardly matters:

int mid = (low + high)/2;

looks better, to me, with the /2 glued right up to the ()s.

> For code clarity, it is important that the compiler and human readers
> agree on what the code means, that the human can easily see what it
> means, and easily see that the compiler will view it the same way.

The trouble with these sort of overly general platitudes is they don't
help people like me (now thankfully out of the game) to know what you
think needs to be clarified. How would you parenthesise the above, and
why?

<cut>
--
Ben.

Re: operator precedence

<878rrsucd9.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21470&group=comp.lang.c#21470

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben)
Newsgroups: comp.lang.c
Subject: Re: operator precedence
Date: Mon, 25 Apr 2022 21:38:58 +0100
Organization: A noiseless patient Spider
Lines: 68
Message-ID: <878rrsucd9.fsf@bsb.me.uk>
References: <thought-20220408125437@ram.dialup.fu-berlin.de>
<t2v61c$ej9$1@gioia.aioe.org> <86v8ux58zx.fsf@linuxsc.com>
<KeC9K.123254$Kdf.96421@fx96.iad>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="8cb2b4d3b883353eb1a22bfc55af833c";
logging-data="18702"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18tf97IS8jpUGZhu99CyKCTWD6SM7IoFGo="
Cancel-Lock: sha1:f/q9/0D7qyqdt+jhutf39nre5Og=
sha1:2Vh9L4vEFm57dAwZ90LchHm/GOQ=
X-BSB-Auth: 1.c3a2f88c65aed2a8bd92.20220425213858BST.878rrsucd9.fsf@bsb.me.uk
 by: Ben - Mon, 25 Apr 2022 20:38 UTC

scott@slp53.sl.home (Scott Lurndal) writes:

> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>>Guillaume <message@bottle.org> writes:
>>
>>> Le 08/04/2022 at 14:00, Stefan Ram a ecrit:
>>>
>>>> Recently, I wrote:
>>>>
>>>> board & 1 << row * COLS + col
>>>
>>> If I saw this coming from someone in my team, I would probably fire them. =)
>>>
>>> I'm not even sure this is correct from what you really intended. But
>>> even if it is, there's a rule in programming, IMO, that's above the
>>> programming language's grammar: readability for us humans.
>>>
>>> Code we write is meant for humans, not machines. That's something
>>> people forget way too often. That's why we write using higher-level
>>> languages, not assembly or even machine code. Or, look at the
>>> obfuscated C contest and such. This is proper C from a language
>>> standpoint, but nothing you'd want to deal with.
>>>
>>> The readability rule that applies here is, if you need more than a few
>>> seconds figuring out what a given statement exactly does, and may have
>>> to even open the C standard to make sure, then it's badly
>>> written. Rewrite it.
>>
>>Human readability can be improved as follows:
>>
>> board & 1 << row*COLS+col
>
>
> Or,
>
> inline _Bool
> test_bit(uint64_t qword, size_t bitnum)
> {
> return (_Bool) (qword & (1ul << bitnum)) != 0;
> }

But this makes me wonder why uint64_t and not uint_least64_t. Do you
really need no more than 64 bits? And why 1ul rather than UINT64_C(1)
(or (uint64_t)1)?

And what is going on with the cast, the != and the return value
conversion? Why does an integer value have to explicitly cast to _Bool
before testing for non-zero, but the integer result (albeit now 0 or 1)
can be implicitly converted to the _Bool return type? This would make
me cautious about other code, especially if I were porting to some new C
compiler. It looks like code that's been copied and edited.

> if (test_bit(board, row*COLS+col)) {

I'd rather bit_is_set(...) since test_bit is agnostic about which way
the test goes. In the case of bits I know one is almost always testing
for set and not clear, but I like to stick to this habit.

> ...
> }
>
> Easily extended to support an array of uint64_t
> when ROWS*COLS > sizeof(uint64_t) * BITS_PER_BYTE.

s/BITS_PER_BYTE/CHAR_BIT/?

--
Ben.

Re: operator precedence

<86czh45sh3.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21474&group=comp.lang.c#21474

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: operator precedence
Date: Mon, 25 Apr 2022 22:22:32 -0700
Organization: A noiseless patient Spider
Lines: 74
Message-ID: <86czh45sh3.fsf@linuxsc.com>
References: <thought-20220408125437@ram.dialup.fu-berlin.de> <t2v61c$ej9$1@gioia.aioe.org> <86v8ux58zx.fsf@linuxsc.com> <KeC9K.123254$Kdf.96421@fx96.iad> <878rrsucd9.fsf@bsb.me.uk>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="e7f354bf3c65e91fece722955444236f";
logging-data="24974"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/26P0MUwr2da9nNivNQt9sVweN4CaCPNk="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:hu7Qfr1x0Hfi4ggdsoWMX8VGHJw=
sha1:zi1Hz5b1BFgF140DJzQvd8itcH8=
 by: Tim Rentsch - Tue, 26 Apr 2022 05:22 UTC

Ben <ben.usenet@bsb.me.uk> writes:

> scott@slp53.sl.home (Scott Lurndal) writes:
>
>> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>>
>>> Guillaume <message@bottle.org> writes:
>>>
>>>> Le 08/04/2022 at 14:00, Stefan Ram a ecrit:
>>>>
>>>>> Recently, I wrote:
>>>>>
>>>>> board & 1 << row * COLS + col
>>>>
>>>> If I saw this coming from someone in my team, I would probably
>>>> fire them. =)
>>>>
>>>> I'm not even sure this is correct from what you really intended.
>>>> But even if it is, there's a rule in programming, IMO, that's
>>>> above the programming language's grammar: readability for us
>>>> humans.
>>>>
>>>> Code we write is meant for humans, not machines. That's something
>>>> people forget way too often. That's why we write using higher-level
>>>> languages, not assembly or even machine code. Or, look at the
>>>> obfuscated C contest and such. This is proper C from a language
>>>> standpoint, but nothing you'd want to deal with.
>>>>
>>>> The readability rule that applies here is, if you need more than
>>>> a few seconds figuring out what a given statement exactly does,
>>>> and may have to even open the C standard to make sure, then it's
>>>> badly written. Rewrite it.
>>>
>>> Human readability can be improved as follows:
>>>
>>> board & 1 << row*COLS+col
>>
>> Or,
>>
>> inline _Bool
>> test_bit(uint64_t qword, size_t bitnum)
>> {
>> return (_Bool) (qword & (1ul << bitnum)) != 0;
>> }
>
> But this makes me wonder why uint64_t and not uint_least64_t. Do
> you really need no more than 64 bits? And why 1ul rather than
> UINT64_C(1) (or (uint64_t)1)?
>
> And what is going on with the cast, the != and the return value
> conversion? Why does an integer value have to explicitly cast to
> _Bool before testing for non-zero, but the integer result (albeit
> now 0 or 1) can be implicitly converted to the _Bool return type?
> This would make me cautious about other code, especially if I were
> porting to some new C compiler. It looks like code that's been
> copied and edited.
>
>> if (test_bit(board, row*COLS+col)) {
>
> I'd rather bit_is_set(...) since test_bit is agnostic about which
> way the test goes. In the case of bits I know one is almost always
> testing for set and not clear, but I like to stick to this habit.

Your comments cover most of what I was going to say. To add one
more thought, the function definition can be simplified:

inline _Bool
bit_is_set( uintmax_t bits, unsigned n ){
return bits>>n & 1;
}

This definition is for me easier to take in and understand than
how test_bit() is defined. Of course other people may have other
impressions.

Re: operator precedence

<t48s6g$4ir$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21479&group=comp.lang.c#21479

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: operator precedence
Date: Tue, 26 Apr 2022 15:30:56 +0200
Organization: A noiseless patient Spider
Lines: 103
Message-ID: <t48s6g$4ir$1@dont-email.me>
References: <thought-20220408125437@ram.dialup.fu-berlin.de>
<t2v61c$ej9$1@gioia.aioe.org> <86v8ux58zx.fsf@linuxsc.com>
<t46qkv$ull$1@dont-email.me> <87ee1kudbm.fsf@bsb.me.uk>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 26 Apr 2022 13:30:56 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="8d5377b2357f4bccec14b90f247d7811";
logging-data="4699"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/MGlrtwTmZHPo92XRUhHjcnQxcT+WAuz4="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.7.0
Cancel-Lock: sha1:vQEofqr0IzZs4zNaPx0o89ReT9M=
In-Reply-To: <87ee1kudbm.fsf@bsb.me.uk>
Content-Language: en-GB
 by: David Brown - Tue, 26 Apr 2022 13:30 UTC

On 25/04/2022 22:18, Ben wrote:
> David Brown <david.brown@hesbynett.no> writes:
>
>> On 25/04/2022 20:10, Tim Rentsch wrote:
>>> Guillaume <message@bottle.org> writes:
>>>
>>>> Le 08/04/2022 at 14:00, Stefan Ram a ecrit:
>>>>
>>>>> Recently, I wrote:
>>>>>
>>>>> board & 1 << row * COLS + col
>>>>
>>>> If I saw this coming from someone in my team, I would probably fire them. =)
>>>>
>>>> I'm not even sure this is correct from what you really intended. But
>>>> even if it is, there's a rule in programming, IMO, that's above the
>>>> programming language's grammar: readability for us humans.
>>>>
>>>> Code we write is meant for humans, not machines. That's something
>>>> people forget way too often. That's why we write using higher-level
>>>> languages, not assembly or even machine code. Or, look at the
>>>> obfuscated C contest and such. This is proper C from a language
>>>> standpoint, but nothing you'd want to deal with.
>>>>
>>>> The readability rule that applies here is, if you need more than a few
>>>> seconds figuring out what a given statement exactly does, and may have
>>>> to even open the C standard to make sure, then it's badly
>>>> written. Rewrite it.
>>>
>>> Human readability can be improved as follows:
>>>
>>> board & 1 << row*COLS+col
>>
>> If I saw that coming from someone in my team, I would treat it as worse
>> than the original. Anyone who thinks mixing inconsistent spacing into
>> this improves things, has completely missed the point (unless they are
>> using some programming language that considers the number of spaces in
>> an expression as significant).
>
> You presumably think that there is only one notion of consistent
> spacing, but spacing to show operator binding is consistent
> with... well, with doing so. I've seen it quite a lot. I do it myself
> even when it hardly matters:
>
> int mid = (low + high)/2;
>
> looks better, to me, with the /2 glued right up to the ()s.

I tend to stick to consistent spacing, but I am always open to bending
such rules if it results in clearer code. (And I also understand that
different people have different spacing rules that they apply at least
reasonably consistently.)

But I think it is rare that unusual, inconsistent or extra spaces are
going to make an expression significantly clearer or easier to parse
(for the human reader). You might feel that skipping the spaces on your
"/2" example makes the line look nicer - and maybe it does, perhaps
depending on fonts and other visual details. However, I don't think
you'd claim that it makes the functional effect of the statement more
immediately obvious to the reader, or reduces the risk of it being
misinterpreted.

>
>> For code clarity, it is important that the compiler and human readers
>> agree on what the code means, that the human can easily see what it
>> means, and easily see that the compiler will view it the same way.
>
> The trouble with these sort of overly general platitudes is they don't
> help people like me (now thankfully out of the game) to know what you
> think needs to be clarified. How would you parenthesise the above, and
> why?
>

I have intentionally been a bit vague on the details, because I don't
think there is one correct set of rules that apply to all people, all
projects, all types of coding task.

I would prefer :

board & 1 << (rows * COLS + col)

I am not sure I would go as far as MISRA would like :

board & (1 << (rows * COLS + col))

There's no single one "correct" or "best" way to write this or any other
code. Maybe it would be better to have new "index" or "mask" variables
for part of the expression, if these were re-used in other code. Maybe
it should be refactored into a function.

Specific rule sets can be useful for specific purposes. MISRA was
developed for safety-critical automotive software. It would not be
suitable for Linux kernel programming. The Linux kernel coding standard
would not be useful for game programming. And so on.

But I think it /is/ possible to form general rules that apply across a
range of uses, and even a range of languages. That includes the
importance of coding for human readability, or avoiding undefined
behaviour (and making reliance on uncommon implementation-dependent
behaviour clear in the code - preferably via static assertions or
pre-processor checks).

Re: operator precedence

<87v8uvq4bv.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21481&group=comp.lang.c#21481

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben)
Newsgroups: comp.lang.c
Subject: Re: operator precedence
Date: Tue, 26 Apr 2022 15:58:28 +0100
Organization: A noiseless patient Spider
Lines: 109
Message-ID: <87v8uvq4bv.fsf@bsb.me.uk>
References: <thought-20220408125437@ram.dialup.fu-berlin.de>
<t2v61c$ej9$1@gioia.aioe.org> <86v8ux58zx.fsf@linuxsc.com>
<t46qkv$ull$1@dont-email.me> <87ee1kudbm.fsf@bsb.me.uk>
<t48s6g$4ir$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="85664e1ccf3261638e2e61e9936c7a03";
logging-data="8646"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/uYwHpxfOi5wSgd+516lsUG3hTjJUpzUc="
Cancel-Lock: sha1:Uv/jBACoN2jfmmB++R4yypAiPTE=
sha1:VV78UsSTsunpQlXud280vOOQJAA=
X-BSB-Auth: 1.4988c6534146d6e6a1db.20220426155828BST.87v8uvq4bv.fsf@bsb.me.uk
 by: Ben - Tue, 26 Apr 2022 14:58 UTC

David Brown <david.brown@hesbynett.no> writes:

> On 25/04/2022 22:18, Ben wrote:
>> David Brown <david.brown@hesbynett.no> writes:
>>
>>> On 25/04/2022 20:10, Tim Rentsch wrote:
>>>> Guillaume <message@bottle.org> writes:
>>>>
>>>>> Le 08/04/2022 at 14:00, Stefan Ram a ecrit:
>>>>>
>>>>>> Recently, I wrote:
>>>>>>
>>>>>> board & 1 << row * COLS + col
>>>>>
>>>>> If I saw this coming from someone in my team, I would probably fire them. =)
>>>>>
>>>>> I'm not even sure this is correct from what you really intended. But
>>>>> even if it is, there's a rule in programming, IMO, that's above the
>>>>> programming language's grammar: readability for us humans.
>>>>>
>>>>> Code we write is meant for humans, not machines. That's something
>>>>> people forget way too often. That's why we write using higher-level
>>>>> languages, not assembly or even machine code. Or, look at the
>>>>> obfuscated C contest and such. This is proper C from a language
>>>>> standpoint, but nothing you'd want to deal with.
>>>>>
>>>>> The readability rule that applies here is, if you need more than a few
>>>>> seconds figuring out what a given statement exactly does, and may have
>>>>> to even open the C standard to make sure, then it's badly
>>>>> written. Rewrite it.
>>>>
>>>> Human readability can be improved as follows:
>>>>
>>>> board & 1 << row*COLS+col
>>>
>>> If I saw that coming from someone in my team, I would treat it as worse
>>> than the original. Anyone who thinks mixing inconsistent spacing into
>>> this improves things, has completely missed the point (unless they are
>>> using some programming language that considers the number of spaces in
>>> an expression as significant).
>> You presumably think that there is only one notion of consistent
>> spacing, but spacing to show operator binding is consistent
>> with... well, with doing so. I've seen it quite a lot. I do it myself
>> even when it hardly matters:
>> int mid = (low + high)/2;
>> looks better, to me, with the /2 glued right up to the ()s.
>
> I tend to stick to consistent spacing, but I am always open to bending
> such rules if it results in clearer code. (And I also understand that
> different people have different spacing rules that they apply at least
> reasonably consistently.)

I think mean you prefer the a fixed number of spaces (one) around each
binary operator and, guessing, none between unary operators and
operands.

> But I think it is rare that unusual, inconsistent or extra spaces are
> going to make an expression significantly clearer or easier to parse
> (for the human reader). You might feel that skipping the spaces on
> your "/2" example makes the line look nicer - and maybe it does,
> perhaps depending on fonts and other visual details. However, I don't
> think you'd claim that it makes the functional effect of the statement
> more immediately obvious to the reader, or reduces the risk of it
> being misinterpreted.

Indeed I would not. As you know, I am not that interested in surface
syntax. I have preferences, but can't get as het up about it as some
people seem to.

>>> For code clarity, it is important that the compiler and human readers
>>> agree on what the code means, that the human can easily see what it
>>> means, and easily see that the compiler will view it the same way.
>> The trouble with these sort of overly general platitudes is they don't
>> help people like me (now thankfully out of the game) to know what you
>> think needs to be clarified. How would you parenthesise the above, and
>> why?
>
> I have intentionally been a bit vague on the details, because I don't
> think there is one correct set of rules that apply to all people, all
> projects, all types of coding task.
>
> I would prefer :
>
> board & 1 << (rows * COLS + col)

Ta. That tells me what priorities you think people may confuse. If I
had to add a pair, I'd have gone with

board & (1 << rows * COLS + col)

since I would assume that a lot of C programmers know some C++ and
therefore know that one never has to parenthesise basic arithmetic in
C++ output expressions.

> I am not sure I would go as far as MISRA would like :
>
> board & (1 << (rows * COLS + col))

They leave it vague. Applying the rule strictly, it would require

board & (1 << ((rows * COLS) + col))

> There's no single one "correct" or "best" way to write this or any
> other code.

I know. I just like to see what other people prefer.

--
Ben.

Re: operator precedence

<t49gk8$lkd$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21492&group=comp.lang.c#21492

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: operator precedence
Date: Tue, 26 Apr 2022 21:19:35 +0200
Organization: A noiseless patient Spider
Lines: 133
Message-ID: <t49gk8$lkd$1@dont-email.me>
References: <thought-20220408125437@ram.dialup.fu-berlin.de>
<t2v61c$ej9$1@gioia.aioe.org> <86v8ux58zx.fsf@linuxsc.com>
<t46qkv$ull$1@dont-email.me> <87ee1kudbm.fsf@bsb.me.uk>
<t48s6g$4ir$1@dont-email.me> <87v8uvq4bv.fsf@bsb.me.uk>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 26 Apr 2022 19:19:36 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="dbbd19897be28b963d9e4f5c7bfe451a";
logging-data="22157"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18/gZ+lblfzU+z/VCnQmsoz9KjOs1sx6T8="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:Ojhoo26p1tKT5ZSwp6njIzCJ3eY=
In-Reply-To: <87v8uvq4bv.fsf@bsb.me.uk>
Content-Language: en-GB
 by: David Brown - Tue, 26 Apr 2022 19:19 UTC

On 26/04/2022 16:58, Ben wrote:
> David Brown <david.brown@hesbynett.no> writes:
>
>> On 25/04/2022 22:18, Ben wrote:
>>> David Brown <david.brown@hesbynett.no> writes:
>>>
>>>> On 25/04/2022 20:10, Tim Rentsch wrote:
>>>>> Guillaume <message@bottle.org> writes:
>>>>>
>>>>>> Le 08/04/2022 at 14:00, Stefan Ram a ecrit:
>>>>>>
>>>>>>> Recently, I wrote:
>>>>>>>
>>>>>>> board & 1 << row * COLS + col
>>>>>>
>>>>>> If I saw this coming from someone in my team, I would probably fire them. =)
>>>>>>
>>>>>> I'm not even sure this is correct from what you really intended. But
>>>>>> even if it is, there's a rule in programming, IMO, that's above the
>>>>>> programming language's grammar: readability for us humans.
>>>>>>
>>>>>> Code we write is meant for humans, not machines. That's something
>>>>>> people forget way too often. That's why we write using higher-level
>>>>>> languages, not assembly or even machine code. Or, look at the
>>>>>> obfuscated C contest and such. This is proper C from a language
>>>>>> standpoint, but nothing you'd want to deal with.
>>>>>>
>>>>>> The readability rule that applies here is, if you need more than a few
>>>>>> seconds figuring out what a given statement exactly does, and may have
>>>>>> to even open the C standard to make sure, then it's badly
>>>>>> written. Rewrite it.
>>>>>
>>>>> Human readability can be improved as follows:
>>>>>
>>>>> board & 1 << row*COLS+col
>>>>
>>>> If I saw that coming from someone in my team, I would treat it as worse
>>>> than the original. Anyone who thinks mixing inconsistent spacing into
>>>> this improves things, has completely missed the point (unless they are
>>>> using some programming language that considers the number of spaces in
>>>> an expression as significant).
>>> You presumably think that there is only one notion of consistent
>>> spacing, but spacing to show operator binding is consistent
>>> with... well, with doing so. I've seen it quite a lot. I do it myself
>>> even when it hardly matters:
>>> int mid = (low + high)/2;
>>> looks better, to me, with the /2 glued right up to the ()s.
>>
>> I tend to stick to consistent spacing, but I am always open to bending
>> such rules if it results in clearer code. (And I also understand that
>> different people have different spacing rules that they apply at least
>> reasonably consistently.)
>
> I think mean you prefer the a fixed number of spaces (one) around each
> binary operator and, guessing, none between unary operators and
> operands.
>

Yes, on the whole.

>> But I think it is rare that unusual, inconsistent or extra spaces are
>> going to make an expression significantly clearer or easier to parse
>> (for the human reader). You might feel that skipping the spaces on
>> your "/2" example makes the line look nicer - and maybe it does,
>> perhaps depending on fonts and other visual details. However, I don't
>> think you'd claim that it makes the functional effect of the statement
>> more immediately obvious to the reader, or reduces the risk of it
>> being misinterpreted.
>
> Indeed I would not. As you know, I am not that interested in surface
> syntax. I have preferences, but can't get as het up about it as some
> people seem to.

I try not to get worked up about it, but I don't think I am as patient
and understanding as you!

>
>>>> For code clarity, it is important that the compiler and human readers
>>>> agree on what the code means, that the human can easily see what it
>>>> means, and easily see that the compiler will view it the same way.
>>> The trouble with these sort of overly general platitudes is they don't
>>> help people like me (now thankfully out of the game) to know what you
>>> think needs to be clarified. How would you parenthesise the above, and
>>> why?
>>
>> I have intentionally been a bit vague on the details, because I don't
>> think there is one correct set of rules that apply to all people, all
>> projects, all types of coding task.
>>
>> I would prefer :
>>
>> board & 1 << (rows * COLS + col)
>
> Ta. That tells me what priorities you think people may confuse. If I
> had to add a pair, I'd have gone with
>
> board & (1 << rows * COLS + col)
>
> since I would assume that a lot of C programmers know some C++ and
> therefore know that one never has to parenthesise basic arithmetic in
> C++ output expressions.

That's a reasonable point. A lot of people in my line of programming
come from the other direction - electronics, then assembly, then C, then
perhaps C++ for the "advanced" programmers. (I personally have jumped
around a great deal, and have not followed any logical progression.) It
is probably more common than it used to be for people to have worked
with C++ before moving into C programming on microcontrollers, though
often now they stick with C++ even on small devices.

>
>> I am not sure I would go as far as MISRA would like :
>>
>> board & (1 << (rows * COLS + col))
>
> They leave it vague. Applying the rule strictly, it would require
>
> board & (1 << ((rows * COLS) + col))

Yes, that would be the most strict application of the rules - but it is
common practice to accept that multiplication binds more tightly than
addition. (People vary in their programming backgrounds and
experiences, but we've all covered basic arithmetic conventions at school.)

>
>> There's no single one "correct" or "best" way to write this or any
>> other code.
>
> I know. I just like to see what other people prefer.
>

Yes. And I thought it was interesting to see your alternative
parenthesising.

Re: operator precedence

<ee03352d-a8a4-4ac3-8d86-84f3d5b82c76n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21496&group=comp.lang.c#21496

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:6214:dcc:b0:446:4e6e:e919 with SMTP id 12-20020a0562140dcc00b004464e6ee919mr17921241qvt.24.1651008616568;
Tue, 26 Apr 2022 14:30:16 -0700 (PDT)
X-Received: by 2002:a37:a716:0:b0:69f:7e9b:9762 with SMTP id
q22-20020a37a716000000b0069f7e9b9762mr2657388qke.33.1651008616434; Tue, 26
Apr 2022 14:30:16 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Tue, 26 Apr 2022 14:30:13 -0700 (PDT)
In-Reply-To: <87v8uvq4bv.fsf@bsb.me.uk>
Injection-Info: google-groups.googlegroups.com; posting-host=80.192.51.129; posting-account=rqC7UgoAAACeVvrGykivrxfPIl3bA_1y
NNTP-Posting-Host: 80.192.51.129
References: <thought-20220408125437@ram.dialup.fu-berlin.de>
<t2v61c$ej9$1@gioia.aioe.org> <86v8ux58zx.fsf@linuxsc.com>
<t46qkv$ull$1@dont-email.me> <87ee1kudbm.fsf@bsb.me.uk> <t48s6g$4ir$1@dont-email.me>
<87v8uvq4bv.fsf@bsb.me.uk>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <ee03352d-a8a4-4ac3-8d86-84f3d5b82c76n@googlegroups.com>
Subject: Re: operator precedence
From: bart4...@gmail.com (bart c)
Injection-Date: Tue, 26 Apr 2022 21:30:16 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 25
 by: bart c - Tue, 26 Apr 2022 21:30 UTC

On Tuesday, 26 April 2022 at 15:58:41 UTC+1, Ben wrote:
> David Brown writes:

> > board & 1 << (rows * COLS + col)
> Ta. That tells me what priorities you think people may confuse. If I
> had to add a pair, I'd have gone with
>
> board & (1 << rows * COLS + col)
>
> since I would assume that a lot of C programmers know some C++ and
> therefore know that one never has to parenthesise basic arithmetic in
> C++ output expressions.

<< isn't basic arithmetic.

> > I am not sure I would go as far as MISRA would like :
> >
> > board & (1 << (rows * COLS + col))
> They leave it vague. Applying the rule strictly, it would require
>
> board & (1 << ((rows * COLS) + col))

The relative precedences of * and + are fairly universal so this is going too far.

But I'd have to go and look up those for & and <<. Parentheses would be a good idea especially for fragments of code that might end up being ported across languages, or posted as 'pseudo-code'.

Re: operator precedence

<87pml3pi0u.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21498&group=comp.lang.c#21498

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben)
Newsgroups: comp.lang.c
Subject: Re: operator precedence
Date: Wed, 27 Apr 2022 00:00:17 +0100
Organization: A noiseless patient Spider
Lines: 21
Message-ID: <87pml3pi0u.fsf@bsb.me.uk>
References: <thought-20220408125437@ram.dialup.fu-berlin.de>
<t2v61c$ej9$1@gioia.aioe.org> <86v8ux58zx.fsf@linuxsc.com>
<t46qkv$ull$1@dont-email.me> <87ee1kudbm.fsf@bsb.me.uk>
<t48s6g$4ir$1@dont-email.me> <87v8uvq4bv.fsf@bsb.me.uk>
<ee03352d-a8a4-4ac3-8d86-84f3d5b82c76n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="ea2fbcde32935fc0fbc4de3f4e5a010f";
logging-data="21357"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19v5Spoos1lZS1brQ3uLekEC5RjGgw4QcE="
Cancel-Lock: sha1:7DcQLJ3oY/K6TyaJtG/SVdBfzXo=
sha1:W4vQnEBUcVd49ZD300yd1KGvKBc=
X-BSB-Auth: 1.d9485cf1a033c59268de.20220427000017BST.87pml3pi0u.fsf@bsb.me.uk
 by: Ben - Tue, 26 Apr 2022 23:00 UTC

bart c <bart4858@gmail.com> writes:

> On Tuesday, 26 April 2022 at 15:58:41 UTC+1, Ben wrote:
>> David Brown writes:
>
>> > board & 1 << (rows * COLS + col)
>> Ta. That tells me what priorities you think people may confuse. If I
>> had to add a pair, I'd have gone with
>>
>> board & (1 << rows * COLS + col)
>>
>> since I would assume that a lot of C programmers know some C++ and
>> therefore know that one never has to parenthesise basic arithmetic in
>> C++ output expressions.
>
> << isn't basic arithmetic.

Yes, that's was my point.

--
Ben.

Re: operator precedence

<t4a5ko$1n6o4$1@news.mixmin.net>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21499&group=comp.lang.c#21499

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.mixmin.net!.POSTED!not-for-mail
From: samlara...@gmail.com (Sams Lara)
Newsgroups: comp.lang.c
Subject: Re: operator precedence
Date: Wed, 27 Apr 2022 02:15:32 +0100
Organization: Microsoft Unofficial Representative on Newsgroups
Message-ID: <t4a5ko$1n6o4$1@news.mixmin.net>
References: <thought-20220408125437@ram.dialup.fu-berlin.de>
<t2v61c$ej9$1@gioia.aioe.org> <86v8ux58zx.fsf@linuxsc.com>
<t46qkv$ull$1@dont-email.me> <87ee1kudbm.fsf@bsb.me.uk>
<t48s6g$4ir$1@dont-email.me> <87v8uvq4bv.fsf@bsb.me.uk>
<t49gk8$lkd$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 27 Apr 2022 01:18:16 -0000 (UTC)
Injection-Info: news.mixmin.net; posting-host="48d11a3b2fa45924111005cb39b9d63a5dfc88a3";
logging-data="1809156"; mail-complaints-to="abuse@mixmin.net"
In-Reply-To: <t49gk8$lkd$1@dont-email.me>
Content-Language: en-us
 by: Sams Lara - Wed, 27 Apr 2022 01:15 UTC

On 26/04/2022 20:19, David Brown wrote:
> Yes, that would be the most strict application of the rules - but it is
> common practice to accept that multiplication binds more tightly than
> addition. (People vary in their programming backgrounds and
> experiences, but we've all covered basic arithmetic conventions at school.)

Is this basic arithmetic:

int result = 8 / 2 * (2 + 2);

You'll be amazed to find that even top notch mathematicians will tell
you that the result of this statement is ambiguous. Order of evaluation
is very important here.

Re: operator precedence

<58d89f9d-b7b3-4841-97ad-a27dfeeaf3e6n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21501&group=comp.lang.c#21501

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ad4:594d:0:b0:456:2e2c:957e with SMTP id eo13-20020ad4594d000000b004562e2c957emr13559521qvb.39.1651043459823;
Wed, 27 Apr 2022 00:10:59 -0700 (PDT)
X-Received: by 2002:a05:622a:91:b0:2f1:f9ce:405a with SMTP id
o17-20020a05622a009100b002f1f9ce405amr17920936qtw.194.1651043459692; Wed, 27
Apr 2022 00:10:59 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Wed, 27 Apr 2022 00:10:59 -0700 (PDT)
In-Reply-To: <t48s6g$4ir$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=84.50.190.130; posting-account=pysjKgkAAACLegAdYDFznkqjgx_7vlUK
NNTP-Posting-Host: 84.50.190.130
References: <thought-20220408125437@ram.dialup.fu-berlin.de>
<t2v61c$ej9$1@gioia.aioe.org> <86v8ux58zx.fsf@linuxsc.com>
<t46qkv$ull$1@dont-email.me> <87ee1kudbm.fsf@bsb.me.uk> <t48s6g$4ir$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <58d89f9d-b7b3-4841-97ad-a27dfeeaf3e6n@googlegroups.com>
Subject: Re: operator precedence
From: oot...@hot.ee (Öö Tiib)
Injection-Date: Wed, 27 Apr 2022 07:10:59 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 55
 by: Öö Tiib - Wed, 27 Apr 2022 07:10 UTC

On Tuesday, 26 April 2022 at 16:31:11 UTC+3, David Brown wrote:
> On 25/04/2022 22:18, Ben wrote:
> > David Brown <david...@hesbynett.no> writes:
> >
> >
> >> For code clarity, it is important that the compiler and human readers
> >> agree on what the code means, that the human can easily see what it
> >> means, and easily see that the compiler will view it the same way.
> >
> > The trouble with these sort of overly general platitudes is they don't
> > help people like me (now thankfully out of the game) to know what you
> > think needs to be clarified. How would you parenthesise the above, and
> > why?
> >
> I have intentionally been a bit vague on the details, because I don't
> think there is one correct set of rules that apply to all people, all
> projects, all types of coding task.
>
> I would prefer :
>
> board & 1 << (rows * COLS + col)
>
> I am not sure I would go as far as MISRA would like :
>
> board & (1 << (rows * COLS + col))
>
> There's no single one "correct" or "best" way to write this or any other
> code. Maybe it would be better to have new "index" or "mask" variables
> for part of the expression, if these were re-used in other code. Maybe
> it should be refactored into a function.
>
> Specific rule sets can be useful for specific purposes. MISRA was
> developed for safety-critical automotive software. It would not be
> suitable for Linux kernel programming. The Linux kernel coding standard
> would not be useful for game programming. And so on.
>
> But I think it /is/ possible to form general rules that apply across a
> range of uses, and even a range of languages. That includes the
> importance of coding for human readability, or avoiding undefined
> behaviour (and making reliance on uncommon implementation-dependent
> behaviour clear in the code - preferably via static assertions or
> pre-processor checks).

I have used simple rule that whatever static style analysis / code
formatting tool settings are accepted by team have to be followed.
Best is to integrate auto-formatting tool (like Clang-Format) into
build system and done.

If formatting is a build step and they still somehow manage
to push different parentheses, brackets, indentation or whitespace
into repo then that indicates that they did not even build what they
wrote.

But if no one can find a tool that enforces it then it is probably too
complicated style rule for to explain to humans as well and will
cause time wasted into bike-shedding if it was violated or wasn't.

Re: operator precedence

<t4aqeh$4d6$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21502&group=comp.lang.c#21502

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: operator precedence
Date: Wed, 27 Apr 2022 09:13:21 +0200
Organization: A noiseless patient Spider
Lines: 38
Message-ID: <t4aqeh$4d6$1@dont-email.me>
References: <thought-20220408125437@ram.dialup.fu-berlin.de>
<t2v61c$ej9$1@gioia.aioe.org> <86v8ux58zx.fsf@linuxsc.com>
<t46qkv$ull$1@dont-email.me> <87ee1kudbm.fsf@bsb.me.uk>
<t48s6g$4ir$1@dont-email.me> <87v8uvq4bv.fsf@bsb.me.uk>
<t49gk8$lkd$1@dont-email.me> <t4a5ko$1n6o4$1@news.mixmin.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 27 Apr 2022 07:13:21 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="6d62c71627a4c5a5c3e5a43f2401c350";
logging-data="4518"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/C/YoiJlucKr+j9Hj4KpcLZ6q95pEO3+U="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.7.0
Cancel-Lock: sha1:1B9Hx6QPU0IXGOWvDF9m74bqO5E=
In-Reply-To: <t4a5ko$1n6o4$1@news.mixmin.net>
Content-Language: en-GB
 by: David Brown - Wed, 27 Apr 2022 07:13 UTC

On 27/04/2022 03:15, Sams Lara wrote:
> On 26/04/2022 20:19, David Brown wrote:
>> Yes, that would be the most strict application of the rules - but it is
>> common practice to accept that multiplication binds more tightly than
>> addition.  (People vary in their programming backgrounds and
>> experiences, but we've all covered basic arithmetic conventions at
>> school.)
>
> Is this basic arithmetic:
>
> int result = 8 / 2 * (2 + 2);
>
> You'll be amazed to find that even top notch mathematicians will tell
> you that the result of this statement is ambiguous. Order of evaluation
> is very important here.
>

If it were written as mathematics, exactly like that, it /would/ be
ambiguous. Writing mathematics you have a lot more freedom to make the
meaning clear (especially when using pencil and paper), and you are
writing for human readers rather than dumb computers. So you'd write:

8/2 × (2+2)

or

8
---------
2 × (2+2)

You don't need parenthesis because in written mathematics, size of
spacing /is/ significant.

Written as C code, the statement is not ambiguous - but it /is/ unclear
and can easily be misinterpreted when reading. I think pretty much any
C programmer, when thinking about it, will get it right - but the more
you have to think, the more likely you are to get it wrong.

Re: operator precedence

<t4asd3$gku$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21503&group=comp.lang.c#21503

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: operator precedence
Date: Wed, 27 Apr 2022 09:46:43 +0200
Organization: A noiseless patient Spider
Lines: 83
Message-ID: <t4asd3$gku$1@dont-email.me>
References: <thought-20220408125437@ram.dialup.fu-berlin.de>
<t2v61c$ej9$1@gioia.aioe.org> <86v8ux58zx.fsf@linuxsc.com>
<t46qkv$ull$1@dont-email.me> <87ee1kudbm.fsf@bsb.me.uk>
<t48s6g$4ir$1@dont-email.me> <87v8uvq4bv.fsf@bsb.me.uk>
<ee03352d-a8a4-4ac3-8d86-84f3d5b82c76n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 27 Apr 2022 07:46:43 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="6d62c71627a4c5a5c3e5a43f2401c350";
logging-data="17054"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19ZlGwpH30FUxPKHce5l/JEjxrZNSzH7xw="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.7.0
Cancel-Lock: sha1:DD4UiRKqsqJY7wwIBngLwIv2ArE=
In-Reply-To: <ee03352d-a8a4-4ac3-8d86-84f3d5b82c76n@googlegroups.com>
Content-Language: en-GB
 by: David Brown - Wed, 27 Apr 2022 07:46 UTC

On 26/04/2022 23:30, bart c wrote:
> On Tuesday, 26 April 2022 at 15:58:41 UTC+1, Ben wrote:
>> David Brown writes:
>
>>> board & 1 << (rows * COLS + col)
>> Ta. That tells me what priorities you think people may confuse. If I
>> had to add a pair, I'd have gone with
>>
>> board & (1 << rows * COLS + col)
>>
>> since I would assume that a lot of C programmers know some C++ and
>> therefore know that one never has to parenthesise basic arithmetic in
>> C++ output expressions.
>
> << isn't basic arithmetic.

C++ programmers are used to seeing statements like :

cout << stuff << more stuff << even more stuff;

Ben's thought here is that they are used to this pattern, and will
automatically think of the "basic arithmetic" parts (which does not
include the "<<" symbol) as being handled first, with the << symbols
separating them much like the commas in function call syntax. Thus
there is no need to parenthesise "rows * COLS + col" within the shift
expression because people familiar with C++ will already see << as low
precedence.

I have at least two objections to this concept, however. First, in some
types of coding - such as the small-systems embedded world for which
MISRA is relevant - you very rarely see iostreams for output in C++
programming. Standard iostreams are just too much overhead - even
standard "printf" is often too big and too slow.

Secondly, it is (IMHO) a mistake to think << has very low precedence, or
to get used to using it without parentheses. "<<" may have been the
best operator available when iostreams were developed, but it is not
perfect. Consider:

cout << "Equality test: " << x == y << endl;

or

cout << "Masked result: " << x & 0xf0 << endl;

The compiler is going to complain about most such mistakes, fortunately.
However, if you've learned that the "<<" symbols work a bit like
parentheses and you have skipped them for printing "x + y", suddenly you
need them for "x < y" and there's an inconsistency. It is not
necessarily a particularly bad inconsistency, but it is worth considering.

>
>>> I am not sure I would go as far as MISRA would like :
>>>
>>> board & (1 << (rows * COLS + col))
>> They leave it vague. Applying the rule strictly, it would require
>>
>> board & (1 << ((rows * COLS) + col))
>
> The relative precedences of * and + are fairly universal so this is going too far.
>
> But I'd have to go and look up those for & and <<. Parentheses would be a good idea especially for fragments of code that might end up being ported across languages, or posted as 'pseudo-code'.
>

While I /do/ know the relative precedences of these operators, I still
think it is clearer to have some parentheses. You give good
justification - code moves around, between people and even between
languages, and long experience at C programming is no guarantee that a
reader will know the precedences. Even for those that /do/ know, it can
take more mental effort to read the expressions and that's wasted mental
effort.

Apparently this first appeared in comp.lang.c++ in a discussion about
the use of the comma operator, but has since become a classic:

"Always code as if the guy who ends up maintaining your code will be a
violent psychopath who knows where you live."

And as we all know, psychopaths don't like learning operator precedence
tables :-)

Re: operator precedence

<87y1zqmkol.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21515&group=comp.lang.c#21515

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben)
Newsgroups: comp.lang.c
Subject: Re: operator precedence
Date: Wed, 27 Apr 2022 19:43:22 +0100
Organization: A noiseless patient Spider
Lines: 9
Message-ID: <87y1zqmkol.fsf@bsb.me.uk>
References: <thought-20220408125437@ram.dialup.fu-berlin.de>
<t2v61c$ej9$1@gioia.aioe.org> <86v8ux58zx.fsf@linuxsc.com>
<t46qkv$ull$1@dont-email.me> <87ee1kudbm.fsf@bsb.me.uk>
<t48s6g$4ir$1@dont-email.me> <87v8uvq4bv.fsf@bsb.me.uk>
<t49gk8$lkd$1@dont-email.me> <t4a5ko$1n6o4$1@news.mixmin.net>
<t4aqeh$4d6$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="ea2fbcde32935fc0fbc4de3f4e5a010f";
logging-data="7539"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19SCzt7IeeZmOkXS/eicY2YG0oNSyO6Xo4="
Cancel-Lock: sha1:yX9lmCO6fkGfUfT7gWA/ow4MWdw=
sha1:NTN4ynSzU4pKbvpaYt8xF53wsjo=
X-BSB-Auth: 1.49d4c22781fa134774cc.20220427194322BST.87y1zqmkol.fsf@bsb.me.uk
 by: Ben - Wed, 27 Apr 2022 18:43 UTC

David Brown <david.brown@hesbynett.no> writes:

> You don't need parenthesis because in written mathematics, size of
> spacing /is/ significant.

Is it? Where can I find out about this?

--
Ben.

Re: operator precedence

<t4di33$48o$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21526&group=comp.lang.c#21526

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: operator precedence
Date: Thu, 28 Apr 2022 10:09:06 +0200
Organization: A noiseless patient Spider
Lines: 45
Message-ID: <t4di33$48o$1@dont-email.me>
References: <thought-20220408125437@ram.dialup.fu-berlin.de>
<t2v61c$ej9$1@gioia.aioe.org> <86v8ux58zx.fsf@linuxsc.com>
<t46qkv$ull$1@dont-email.me> <87ee1kudbm.fsf@bsb.me.uk>
<t48s6g$4ir$1@dont-email.me> <87v8uvq4bv.fsf@bsb.me.uk>
<t49gk8$lkd$1@dont-email.me> <t4a5ko$1n6o4$1@news.mixmin.net>
<t4aqeh$4d6$1@dont-email.me> <87y1zqmkol.fsf@bsb.me.uk>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 28 Apr 2022 08:09:07 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="8d8f24825e5730f4bfa59a4b1fa7d8e1";
logging-data="4376"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ajI9K8EqAoU0R+32tPW+fptDJ+hy+7cQ="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.7.0
Cancel-Lock: sha1:K1TT3GdApuBtsLNXInfbS/IKSPY=
In-Reply-To: <87y1zqmkol.fsf@bsb.me.uk>
Content-Language: en-GB
 by: David Brown - Thu, 28 Apr 2022 08:09 UTC

On 27/04/2022 20:43, Ben wrote:
> David Brown <david.brown@hesbynett.no> writes:
>
>> You don't need parenthesis because in written mathematics, size of
>> spacing /is/ significant.
>
> Is it? Where can I find out about this?
>

I mean the way you space things in how you write mathematics gives
additional information, in a way that cannot be done in a programming
language.

If you see mathematics written :

10 × 2+3

you'll interpret it differently from :

10×2 + 3

The second one you'll see as 23, and be happy with it. The first one
you might assume the writer meant 50. You might also interpret it as
meaning the writer is sloppy or doesn't really understand what he or she
is writing - that's additional information you gain from the spacing.

If you see :

a - b

you'll treat it as one expression.

If you see :

a -b

you will likely consider it as two independent expressions (all
depending on the context, of course).

I am certainly not advocating using spacing instead of parenthesis in
written mathematics - ambiguious and unclear writing is no better in
written mathematics than in programming or anything else (except perhaps
politics or diplomacy!). But you get extra information from it, and it
is therefore significant.

Re: operator precedence

<871qxhmtjo.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21527&group=comp.lang.c#21527

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben)
Newsgroups: comp.lang.c
Subject: Re: operator precedence
Date: Thu, 28 Apr 2022 10:44:11 +0100
Organization: A noiseless patient Spider
Lines: 27
Message-ID: <871qxhmtjo.fsf@bsb.me.uk>
References: <thought-20220408125437@ram.dialup.fu-berlin.de>
<t2v61c$ej9$1@gioia.aioe.org> <86v8ux58zx.fsf@linuxsc.com>
<t46qkv$ull$1@dont-email.me> <87ee1kudbm.fsf@bsb.me.uk>
<t48s6g$4ir$1@dont-email.me> <87v8uvq4bv.fsf@bsb.me.uk>
<t49gk8$lkd$1@dont-email.me> <t4a5ko$1n6o4$1@news.mixmin.net>
<t4aqeh$4d6$1@dont-email.me> <87y1zqmkol.fsf@bsb.me.uk>
<t4di33$48o$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
Injection-Info: reader02.eternal-september.org; posting-host="78d270a39e8aac3e13f55e3de1875d63";
logging-data="14978"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/BXgsVbrf4hHUTuQdIDt1AhAzvjC/xltU="
Cancel-Lock: sha1:V1mOUPbu90213ap5uayZkoVu3yc=
sha1:J4mD7w/Y5UGRBSTWnUcBwsb8Rg0=
X-BSB-Auth: 1.0301a05350948271c5fb.20220428104411BST.871qxhmtjo.fsf@bsb.me.uk
 by: Ben - Thu, 28 Apr 2022 09:44 UTC

David Brown <david.brown@hesbynett.no> writes:

> On 27/04/2022 20:43, Ben wrote:
>> David Brown <david.brown@hesbynett.no> writes:
>>
>>> You don't need parenthesis because in written mathematics, size of
>>> spacing /is/ significant.
>> Is it? Where can I find out about this?
>>
>
> I mean the way you space things in how you write mathematics gives
> additional information, in a way that cannot be done in a programming
> language.
>
> If you see mathematics written :
>
> 10 × 2+3
>
> you'll interpret it differently from :
>
> 10×2 + 3

Where does this come from? It's news to me. Where did you find out
about it?

--
Ben.

Re: operator precedence

<t4dohv$mmk$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21528&group=comp.lang.c#21528

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: operator precedence
Date: Thu, 28 Apr 2022 11:59:26 +0200
Organization: A noiseless patient Spider
Lines: 35
Message-ID: <t4dohv$mmk$1@dont-email.me>
References: <thought-20220408125437@ram.dialup.fu-berlin.de>
<t2v61c$ej9$1@gioia.aioe.org> <86v8ux58zx.fsf@linuxsc.com>
<t46qkv$ull$1@dont-email.me> <87ee1kudbm.fsf@bsb.me.uk>
<t48s6g$4ir$1@dont-email.me> <87v8uvq4bv.fsf@bsb.me.uk>
<t49gk8$lkd$1@dont-email.me> <t4a5ko$1n6o4$1@news.mixmin.net>
<t4aqeh$4d6$1@dont-email.me> <87y1zqmkol.fsf@bsb.me.uk>
<t4di33$48o$1@dont-email.me> <871qxhmtjo.fsf@bsb.me.uk>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 28 Apr 2022 09:59:27 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="8d8f24825e5730f4bfa59a4b1fa7d8e1";
logging-data="23252"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/l3YNCS0LP+uAn+BrBYnHjJ4lkuig5iRk="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.7.0
Cancel-Lock: sha1:GewUWmhG6gxXfKR4EDOpD6Q6gXI=
In-Reply-To: <871qxhmtjo.fsf@bsb.me.uk>
Content-Language: en-GB
 by: David Brown - Thu, 28 Apr 2022 09:59 UTC

On 28/04/2022 11:44, Ben wrote:
> David Brown <david.brown@hesbynett.no> writes:
>
>> On 27/04/2022 20:43, Ben wrote:
>>> David Brown <david.brown@hesbynett.no> writes:
>>>
>>>> You don't need parenthesis because in written mathematics, size of
>>>> spacing /is/ significant.
>>> Is it? Where can I find out about this?
>>>
>>
>> I mean the way you space things in how you write mathematics gives
>> additional information, in a way that cannot be done in a programming
>> language.
>>
>> If you see mathematics written :
>>
>> 10 × 2+3
>>
>> you'll interpret it differently from :
>>
>> 10×2 + 3
>
> Where does this come from? It's news to me. Where did you find out
> about it?
>

I think you are reading too much into this. To my knowledge, there is
no ISO Mathematics Standard document we can refer to. Written
mathematics is written by people, and read by people, as a method of
communication between humans. The way it is laid out, including the
spacing, is part of that. Spacing can make it clear what the writer
/intended/ even if it is not what what was actually /written/. Humans
can make interpretations like that - a compiler cannot.

Re: operator precedence

<t4dqav$sjd$1@milena.home.net.it>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21530&group=comp.lang.c#21530

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!news.uzoreto.com!dotsrc.org!filter.dotsrc.org!news.dotsrc.org!not-for-mail
From: lsod...@home.net.it (Giovanni)
Newsgroups: comp.lang.c
Subject: Re: operator precedence
Date: Thu, 28 Apr 2022 12:29:51 +0200
Organization: G. Falzoni Home Network
Message-ID: <t4dqav$sjd$1@milena.home.net.it>
References: <thought-20220408125437@ram.dialup.fu-berlin.de>
<t2v61c$ej9$1@gioia.aioe.org> <86v8ux58zx.fsf@linuxsc.com>
<t46qkv$ull$1@dont-email.me> <87ee1kudbm.fsf@bsb.me.uk>
<t48s6g$4ir$1@dont-email.me> <87v8uvq4bv.fsf@bsb.me.uk>
<t49gk8$lkd$1@dont-email.me> <t4a5ko$1n6o4$1@news.mixmin.net>
<t4aqeh$4d6$1@dont-email.me> <87y1zqmkol.fsf@bsb.me.uk>
<t4di33$48o$1@dont-email.me> <871qxhmtjo.fsf@bsb.me.uk>
<t4dohv$mmk$1@dont-email.me>
Reply-To: gfalzoni@inwind.it
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Lines: 14
NNTP-Posting-Host: 11578051.news.sunsite.dk
X-Trace: 1651141794 news.sunsite.dk 698 gfalzoni@inwind.it/151.21.53.24:43088
X-Complaints-To: staff@sunsite.dk
 by: Giovanni - Thu, 28 Apr 2022 10:29 UTC

On 4/28/22 11:59, David Brown wrote:

> Humans can make interpretations like that - a compiler cannot.

Humans _can_ but not all humans do. I would interpret
10 × 2+3 as (10 x 2)+3
This was the basic rule I learned at the times of primary school.

Ciao
Giovanni
--
A computer is like an air conditioner,
it stops working when you open Windows.
< http://giovanni.homelinux.net/ >

Re: operator precedence

<t4drth$mng$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21533&group=comp.lang.c#21533

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: operator precedence
Date: Thu, 28 Apr 2022 12:56:48 +0200
Organization: A noiseless patient Spider
Lines: 19
Message-ID: <t4drth$mng$1@dont-email.me>
References: <thought-20220408125437@ram.dialup.fu-berlin.de>
<t2v61c$ej9$1@gioia.aioe.org> <86v8ux58zx.fsf@linuxsc.com>
<t46qkv$ull$1@dont-email.me> <87ee1kudbm.fsf@bsb.me.uk>
<t48s6g$4ir$1@dont-email.me> <87v8uvq4bv.fsf@bsb.me.uk>
<t49gk8$lkd$1@dont-email.me> <t4a5ko$1n6o4$1@news.mixmin.net>
<t4aqeh$4d6$1@dont-email.me> <87y1zqmkol.fsf@bsb.me.uk>
<t4di33$48o$1@dont-email.me> <871qxhmtjo.fsf@bsb.me.uk>
<t4dohv$mmk$1@dont-email.me> <t4dqav$sjd$1@milena.home.net.it>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 28 Apr 2022 10:56:49 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="8d8f24825e5730f4bfa59a4b1fa7d8e1";
logging-data="23280"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18kFU87a6eFXqSoY40L11R6bTcB52gg1nA="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.7.0
Cancel-Lock: sha1:Trx0QmBc5GPiHV/Thu1o9jyEMtg=
In-Reply-To: <t4dqav$sjd$1@milena.home.net.it>
Content-Language: en-GB
 by: David Brown - Thu, 28 Apr 2022 10:56 UTC

On 28/04/2022 12:29, Giovanni wrote:
> On 4/28/22 11:59, David Brown wrote:
>
>> Humans can make interpretations like that - a compiler cannot.
>
> Humans _can_ but not all humans do.  I would interpret
>     10  ×  2+3  as (10  x  2)+3
> This was the basic rule I learned at the times of primary school.
>

That is the rule, yes. But if someone else wrote it with such spacing,
would you not think that perhaps they meant it to be interpreted as
though they had written "(10 × 2) + 3" ? When faced with something that
has a conflict between the layout and the standard rules for
mathematics, you know something is not right - but as a human reader you
need to make a judgement as to what you think is wrong, and correct for
that. And you do this in a different way than a computer or compiler does.

Pages:1234
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor