Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

"Jesus may love you, but I think you're garbage wrapped in skin." -- Michael O'Donohugh


devel / comp.lang.c / Re: How to avoid an overflow during multiplication?

SubjectAuthor
* How to avoid an overflow during multiplication?Mateusz Viste
+* Re: How to avoid an overflow during multiplication?Stefan Ram
|`- Re: How to avoid an overflow during multiplication?Ben Bacarisse
+* Re: How to avoid an overflow during multiplication?Ben Bacarisse
|`* Re: How to avoid an overflow during multiplication?Mateusz Viste
| +* Re: How to avoid an overflow during multiplication?Stefan Ram
| |+* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||`* Re: How to avoid an overflow during multiplication?Keith Thompson
| || `* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  +* Re: How to avoid an overflow during multiplication?James Kuyper
| ||  |`* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  | `* Re: How to avoid an overflow during multiplication?Tim Rentsch
| ||  |  `* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |   +* Re: How to avoid an overflow during multiplication?David Brown
| ||  |   |`* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |   | +- Re: How to avoid an overflow during multiplication?James Kuyper
| ||  |   | `* Re: How to avoid an overflow during multiplication?David Brown
| ||  |   |  `* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |   |   `* Re: How to avoid an overflow during multiplication?David Brown
| ||  |   |    `* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |   |     `* Re: How to avoid an overflow during multiplication?David Brown
| ||  |   |      `* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |   |       `* Re: How to avoid an overflow during multiplication?Richard Damon
| ||  |   |        +* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |   |        |`* Re: How to avoid an overflow during multiplication?David Brown
| ||  |   |        | `* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |   |        |  `- Re: How to avoid an overflow during multiplication?David Brown
| ||  |   |        `* Re: How to avoid an overflow during multiplication?Tim Rentsch
| ||  |   |         `* Re: How to avoid an overflow during multiplication?David Brown
| ||  |   |          `* Re: How to avoid an overflow during multiplication?Bart
| ||  |   |           `* Re: How to avoid an overflow during multiplication?Öö Tiib
| ||  |   |            +- Re: How to avoid an overflow during multiplication?Bart
| ||  |   |            `- Re: How to avoid an overflow during multiplication?Tim Rentsch
| ||  |   +* Re: How to avoid an overflow during multiplication?James Kuyper
| ||  |   |`* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |   | `- Re: How to avoid an overflow during multiplication?James Kuyper
| ||  |   +* Re: How to avoid an overflow during multiplication?Scott Lurndal
| ||  |   |`* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |   | `* Re: How to avoid an overflow during multiplication?Scott Lurndal
| ||  |   |  `* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |   |   +* Re: How to avoid an overflow during multiplication?Scott Lurndal
| ||  |   |   |`* Re: How to avoid an overflow during multiplication?Tim Rentsch
| ||  |   |   | `* Re: How to avoid an overflow during multiplication?Scott Lurndal
| ||  |   |   |  `* Re: How to avoid an overflow during multiplication?Tim Rentsch
| ||  |   |   |   `* Re: How to avoid an overflow during multiplication?Scott Lurndal
| ||  |   |   |    +* Re: How to avoid an overflow during multiplication?Scott Lurndal
| ||  |   |   |    |`* Re: How to avoid an overflow during multiplication?Tim Rentsch
| ||  |   |   |    | `* Re: How to avoid an overflow during multiplication?Scott Lurndal
| ||  |   |   |    |  +* Re: How to avoid an overflow during multiplication?Keith Thompson
| ||  |   |   |    |  |`* Re: How to avoid an overflow during multiplication?Scott Lurndal
| ||  |   |   |    |  | +* Re: How to avoid an overflow during multiplication?Ben Bacarisse
| ||  |   |   |    |  | |`- Re: How to avoid an overflow during multiplication?Scott Lurndal
| ||  |   |   |    |  | `* Re: How to avoid an overflow during multiplication?Keith Thompson
| ||  |   |   |    |  |  `- Re: How to avoid an overflow during multiplication?Scott Lurndal
| ||  |   |   |    |  `- Re: How to avoid an overflow during multiplication?Tim Rentsch
| ||  |   |   |    `* Re: How to avoid an overflow during multiplication?James Kuyper
| ||  |   |   |     `* Re: How to avoid an overflow during multiplication?Manfred
| ||  |   |   |      `* Re: How to avoid an overflow during multiplication?Scott Lurndal
| ||  |   |   |       `- Re: How to avoid an overflow during multiplication?Tim Rentsch
| ||  |   |   `- Re: How to avoid an overflow during multiplication?Ben Bacarisse
| ||  |   +* Re: How to avoid an overflow during multiplication?Tim Rentsch
| ||  |   |`* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |   | +- Re: How to avoid an overflow during multiplication?Tim Rentsch
| ||  |   | `* Re: How to avoid an overflow during multiplication?James Kuyper
| ||  |   |  `- Re: How to avoid an overflow during multiplication?Tim Rentsch
| ||  |   +- Re: How to avoid an overflow during multiplication?Tim Rentsch
| ||  |   `* Re: How to avoid an overflow during multiplication?Tim Rentsch
| ||  |    `* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |     `* Re: How to avoid an overflow during multiplication?Tim Rentsch
| ||  |      `* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |       +* Re: How to avoid an overflow during multiplication?Keith Thompson
| ||  |       |`- Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |       +* Re: How to avoid an overflow during multiplication?Vir Campestris
| ||  |       |`* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |       | +* Re: How to avoid an overflow during multiplication?James Kuyper
| ||  |       | |+* Re: How to avoid an overflow during multiplication?Öö Tiib
| ||  |       | ||`* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |       | || +- Re: How to avoid an overflow during multiplication?Öö Tiib
| ||  |       | || `* Re: How to avoid an overflow during multiplication?Tim Rentsch
| ||  |       | ||  `* Re: How to avoid an overflow during multiplication?Bart
| ||  |       | ||   +* Re: How to avoid an overflow during multiplication?Ben Bacarisse
| ||  |       | ||   |`- Re: How to avoid an overflow during multiplication?Bart
| ||  |       | ||   `- Re: How to avoid an overflow during multiplication?Tim Rentsch
| ||  |       | |`* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |       | | +* Re: How to avoid an overflow during multiplication?David Brown
| ||  |       | | |+* Re: How to avoid an overflow during multiplication?Malcolm McLean
| ||  |       | | ||`* Re: How to avoid an overflow during multiplication?David Brown
| ||  |       | | || `* Re: How to avoid an overflow during multiplication?Mateusz Viste
| ||  |       | | ||  `* Re: How to avoid an overflow during multiplication?David Brown
| ||  |       | | ||   `- Re: How to avoid an overflow during multiplication?Malcolm McLean
| ||  |       | | |`* Re: How to avoid an overflow during multiplication?Bart
| ||  |       | | | +* Re: How to avoid an overflow during multiplication?David Brown
| ||  |       | | | |`* Re: How to avoid an overflow during multiplication?Bart
| ||  |       | | | | +* Re: How to avoid an overflow during multiplication?David Brown
| ||  |       | | | | |+* Re: How to avoid an overflow during multiplication?Bart
| ||  |       | | | | ||`* Re: How to avoid an overflow during multiplication?David Brown
| ||  |       | | | | || `- Re: How to avoid an overflow during multiplication?Bart
| ||  |       | | | | |`* Re: How to avoid an overflow during multiplication?Manfred
| ||  |       | | | | | +* Re: How to avoid an overflow during multiplication?James Kuyper
| ||  |       | | | | | |`- Re: How to avoid an overflow during multiplication?Manfred
| ||  |       | | | | | `- Re: How to avoid an overflow during multiplication?David Brown
| ||  |       | | | | `* Re: How to avoid an overflow during multiplication?james...@alumni.caltech.edu
| ||  |       | | | `* Re: How to avoid an overflow during multiplication?james...@alumni.caltech.edu
| ||  |       | | `* Re: How to avoid an overflow during multiplication?James Kuyper
| ||  |       | +- Re: How to avoid an overflow during multiplication?Scott Lurndal
| ||  |       | `* Re: How to avoid an overflow during multiplication?Vir Campestris
| ||  |       +- Re: How to avoid an overflow during multiplication?James Kuyper
| ||  |       `* Re: How to avoid an overflow during multiplication?Tim Rentsch
| ||  `- Re: How to avoid an overflow during multiplication?Keith Thompson
| |`- Re: How to avoid an overflow during multiplication?Guillaume
| `* Re: How to avoid an overflow during multiplication?David Brown
+* Re: How to avoid an overflow during multiplication?Bonita Montero
+* Re: How to avoid an overflow during multiplication?Michael S
+* Re: How to avoid an overflow during multiplication?Kaz Kylheku
+* Re: How to avoid an overflow during multiplication?Tim Rentsch
`* Re: How to avoid an overflow during multiplication?Stefan Ram

Pages:12345678
Re: How to avoid an overflow during multiplication?

<sthe7t$1cph$1@gioia.aioe.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!299gYy2nqWB43X4cCBV6zg.user.46.165.242.75.POSTED!not-for-mail
From: mate...@xyz.invalid (Mateusz Viste)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: Thu, 3 Feb 2022 21:34:05 +0100
Organization: . . .
Message-ID: <sthe7t$1cph$1@gioia.aioe.org>
References: <sqmkg9$157v$1@gioia.aioe.org>
<8735m954yz.fsf@bsb.me.uk>
<sqn0aq$1e8b$1@gioia.aioe.org>
<unsigned-20211231144041@ram.dialup.fu-berlin.de>
<sqn2dj$14ld$2@gioia.aioe.org>
<877dbk36qu.fsf@nosuchdomain.example.com>
<sqnmsm$1ff8$1@gioia.aioe.org>
<sqo47m$n9p$1@dont-email.me>
<sqq0lb$1g77$1@gioia.aioe.org>
<86v8ydp57h.fsf@linuxsc.com>
<ssehod$hfk$1@gioia.aioe.org>
<86pmojo7th.fsf@linuxsc.com>
<sslpp3$61s$2@gioia.aioe.org>
<86sfszlsfd.fsf@linuxsc.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="45873"; posting-host="299gYy2nqWB43X4cCBV6zg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Mateusz Viste - Thu, 3 Feb 2022 20:34 UTC

2022-02-03 at 10:24 -0800, Tim Rentsch wrote:
> > Now, about features that I don't like in C99:
> > - VLAs (temptation argument again, that may lead to stack
> > exhaustion)
> > - bool (don't see what added value this brings)
> > - complex & imaginary numbers (I admit those may be useful in some
> > niche usage, but *I* never had such need)
>
> With all due respect, these sound more like rationalizations than
> reasons.

These are my reasons, I'm sorry to disappoint.

> If you don't give C99 an earnest try of non-trivial duration,
> you'll never know.

It's not like I reject C99 completely - I do use it in a few projects,
mainly for bad reasons (to name two: extensive use of VLAs, looked nice
and fun when I started using it, and by the time I realized that's a
trap it was too late - the cost of redoing things the proper way is too
high now so I'm living with it now ; second is 3rd-party code that
insists on using [0]-sized arrays and mixes variable declaration with
code).

Anyway - I do use C99 from time to time on some oddball projects, but
for my daily stuff I simply don't feel it brings anything on top of
what I have already with gnu89. But maybe I missed some key things, so
let me ask: what C99 feature do you use, that you wouldn't want to
loose?

Mateusz

Re: How to avoid an overflow during multiplication?

<877dabljnf.fsf@nosuchdomain.example.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: Thu, 03 Feb 2022 13:33:56 -0800
Organization: None to speak of
Lines: 18
Message-ID: <877dabljnf.fsf@nosuchdomain.example.com>
References: <sqmkg9$157v$1@gioia.aioe.org> <8735m954yz.fsf@bsb.me.uk>
<sqn0aq$1e8b$1@gioia.aioe.org>
<unsigned-20211231144041@ram.dialup.fu-berlin.de>
<sqn2dj$14ld$2@gioia.aioe.org>
<877dbk36qu.fsf@nosuchdomain.example.com>
<sqnmsm$1ff8$1@gioia.aioe.org> <sqo47m$n9p$1@dont-email.me>
<sqq0lb$1g77$1@gioia.aioe.org> <86v8ydp57h.fsf@linuxsc.com>
<ssehod$hfk$1@gioia.aioe.org> <86pmojo7th.fsf@linuxsc.com>
<sslpp3$61s$2@gioia.aioe.org> <86sfszlsfd.fsf@linuxsc.com>
<sthe7t$1cph$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="833c2fa4bd384948b97d604ba3ea646b";
logging-data="6447"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+JT2v/3IlpcGiCkfSK72my"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:AoYKAGXCBUFONgj7E9gquyttfLM=
sha1:m4BumiKsRZhcZ5y1DcZsOe3KZQQ=
 by: Keith Thompson - Thu, 3 Feb 2022 21:33 UTC

Mateusz Viste <mateusz@xyz.invalid> writes:
[...]
> It's not like I reject C99 completely - I do use it in a few projects,
> mainly for bad reasons (to name two: extensive use of VLAs, looked nice
> and fun when I started using it, and by the time I realized that's a
> trap it was too late - the cost of redoing things the proper way is too
> high now so I'm living with it now ; second is 3rd-party code that
> insists on using [0]-sized arrays and mixes variable declaration with
> code).

C99 doesn't support zero-sized arrays. Attempting to define a
zero-sized ordinary array is a constraint violation. Creating a
zero-sized VLA has undefined behavior.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

Re: How to avoid an overflow during multiplication?

<sthj43$124e$1@gioia.aioe.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!299gYy2nqWB43X4cCBV6zg.user.46.165.242.75.POSTED!not-for-mail
From: mate...@xyz.invalid (Mateusz Viste)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: Thu, 3 Feb 2022 22:57:23 +0100
Organization: . . .
Message-ID: <sthj43$124e$1@gioia.aioe.org>
References: <sqmkg9$157v$1@gioia.aioe.org>
<8735m954yz.fsf@bsb.me.uk>
<sqn0aq$1e8b$1@gioia.aioe.org>
<unsigned-20211231144041@ram.dialup.fu-berlin.de>
<sqn2dj$14ld$2@gioia.aioe.org>
<877dbk36qu.fsf@nosuchdomain.example.com>
<sqnmsm$1ff8$1@gioia.aioe.org>
<sqo47m$n9p$1@dont-email.me>
<sqq0lb$1g77$1@gioia.aioe.org>
<86v8ydp57h.fsf@linuxsc.com>
<ssehod$hfk$1@gioia.aioe.org>
<86pmojo7th.fsf@linuxsc.com>
<sslpp3$61s$2@gioia.aioe.org>
<86sfszlsfd.fsf@linuxsc.com>
<sthe7t$1cph$1@gioia.aioe.org>
<877dabljnf.fsf@nosuchdomain.example.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="34958"; posting-host="299gYy2nqWB43X4cCBV6zg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Mateusz Viste - Thu, 3 Feb 2022 21:57 UTC

2022-02-03 at 13:33 -0800, Keith Thompson wrote:
> C99 doesn't support zero-sized arrays.

You are right indeed, 0-sized arrays are a GNU extension. Seems I
misremembered this one.

Mateusz

Re: How to avoid an overflow during multiplication?

<sthkum$5nu$2@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: vir.camp...@invalid.invalid (Vir Campestris)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: Thu, 3 Feb 2022 22:28:38 +0000
Organization: A noiseless patient Spider
Lines: 12
Message-ID: <sthkum$5nu$2@dont-email.me>
References: <sqmkg9$157v$1@gioia.aioe.org> <8735m954yz.fsf@bsb.me.uk>
<sqn0aq$1e8b$1@gioia.aioe.org>
<unsigned-20211231144041@ram.dialup.fu-berlin.de>
<sqn2dj$14ld$2@gioia.aioe.org> <877dbk36qu.fsf@nosuchdomain.example.com>
<sqnmsm$1ff8$1@gioia.aioe.org> <sqo47m$n9p$1@dont-email.me>
<sqq0lb$1g77$1@gioia.aioe.org> <86v8ydp57h.fsf@linuxsc.com>
<ssehod$hfk$1@gioia.aioe.org> <86pmojo7th.fsf@linuxsc.com>
<sslpp3$61s$2@gioia.aioe.org> <86sfszlsfd.fsf@linuxsc.com>
<sthe7t$1cph$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 3 Feb 2022 22:28:38 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="73daac4c140791fc7c9bed28c84dab6e";
logging-data="5886"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX181Xb37pRYZtlYcgrcRIhEzs7VH+6rYrcA="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.5.0
Cancel-Lock: sha1:zXwK3DT++o2QaF4Gx0qKv/j234Q=
In-Reply-To: <sthe7t$1cph$1@gioia.aioe.org>
Content-Language: en-GB
 by: Vir Campestris - Thu, 3 Feb 2022 22:28 UTC

On 03/02/2022 20:34, Mateusz Viste wrote:
> what C99 feature do you use, that you wouldn't want to
> loose?

I mostly use C++.

I use bool all the time; why would you use anything else? It's a type
that holds 2 values, and the compiler can pick what to use for your
processor. It might be a bit mask, or a 64 bit word. It might even vary
with your optimisation settings.

Andy

Re: How to avoid an overflow during multiplication?

<sthm4i$d2b$1@gioia.aioe.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!299gYy2nqWB43X4cCBV6zg.user.46.165.242.75.POSTED!not-for-mail
From: mate...@xyz.invalid (Mateusz Viste)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: Thu, 3 Feb 2022 23:48:50 +0100
Organization: . . .
Message-ID: <sthm4i$d2b$1@gioia.aioe.org>
References: <sqmkg9$157v$1@gioia.aioe.org>
<8735m954yz.fsf@bsb.me.uk>
<sqn0aq$1e8b$1@gioia.aioe.org>
<unsigned-20211231144041@ram.dialup.fu-berlin.de>
<sqn2dj$14ld$2@gioia.aioe.org>
<877dbk36qu.fsf@nosuchdomain.example.com>
<sqnmsm$1ff8$1@gioia.aioe.org>
<sqo47m$n9p$1@dont-email.me>
<sqq0lb$1g77$1@gioia.aioe.org>
<86v8ydp57h.fsf@linuxsc.com>
<ssehod$hfk$1@gioia.aioe.org>
<86pmojo7th.fsf@linuxsc.com>
<sslpp3$61s$2@gioia.aioe.org>
<86sfszlsfd.fsf@linuxsc.com>
<sthe7t$1cph$1@gioia.aioe.org>
<sthkum$5nu$2@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="13387"; posting-host="299gYy2nqWB43X4cCBV6zg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Mateusz Viste - Thu, 3 Feb 2022 22:48 UTC

2022-02-03 at 22:28 +0000, Vir Campestris wrote:
> I use bool all the time; why would you use anything else? It's a type
> that holds 2 values, and the compiler can pick what to use for your
> processor. It might be a bit mask, or a 64 bit word. It might even
> vary with your optimisation settings.

Can you tell what advantage it has over using, say, int_fast8_t?

Mateusz

Re: How to avoid an overflow during multiplication?

<sti5s0$dtj$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jameskuy...@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: Thu, 3 Feb 2022 22:17:18 -0500
Organization: A noiseless patient Spider
Lines: 13
Message-ID: <sti5s0$dtj$1@dont-email.me>
References: <sqmkg9$157v$1@gioia.aioe.org> <8735m954yz.fsf@bsb.me.uk>
<sqn0aq$1e8b$1@gioia.aioe.org>
<unsigned-20211231144041@ram.dialup.fu-berlin.de>
<sqn2dj$14ld$2@gioia.aioe.org> <877dbk36qu.fsf@nosuchdomain.example.com>
<sqnmsm$1ff8$1@gioia.aioe.org> <sqo47m$n9p$1@dont-email.me>
<sqq0lb$1g77$1@gioia.aioe.org> <86v8ydp57h.fsf@linuxsc.com>
<ssehod$hfk$1@gioia.aioe.org> <86pmojo7th.fsf@linuxsc.com>
<sslpp3$61s$2@gioia.aioe.org> <86sfszlsfd.fsf@linuxsc.com>
<sthe7t$1cph$1@gioia.aioe.org> <sthkum$5nu$2@dont-email.me>
<sthm4i$d2b$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 4 Feb 2022 03:17:20 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="b01fab8648ecf27ecc3a29ac06987053";
logging-data="14259"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+XIGq21o2xxYzSjW5dfUvr0VIHRijVMW4="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.5.0
Cancel-Lock: sha1:LHQLRA89t8sPIC0Ze+MB2nwy7w8=
In-Reply-To: <sthm4i$d2b$1@gioia.aioe.org>
Content-Language: en-US
 by: James Kuyper - Fri, 4 Feb 2022 03:17 UTC

On 2/3/22 17:48, Mateusz Viste wrote:
> 2022-02-03 at 22:28 +0000, Vir Campestris wrote:
>> I use bool all the time; why would you use anything else? It's a type
>> that holds 2 values, and the compiler can pick what to use for your
>> processor. It might be a bit mask, or a 64 bit word. It might even
>> vary with your optimisation settings.
>
> Can you tell what advantage it has over using, say, int_fast8_t?

Conversions to C's _Bool (C standard 6.3.1.2), or C++'s bool (C++
standard 7.3.14), type always generate only one of two possible values,
which is what you need for true boolean semantics. That if very
definitely not the case for int_fast8_t.

Re: How to avoid an overflow during multiplication?

<sti6t7$vis$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jameskuy...@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: Thu, 3 Feb 2022 22:35:03 -0500
Organization: A noiseless patient Spider
Lines: 45
Message-ID: <sti6t7$vis$1@dont-email.me>
References: <sqmkg9$157v$1@gioia.aioe.org> <8735m954yz.fsf@bsb.me.uk>
<sqn0aq$1e8b$1@gioia.aioe.org>
<unsigned-20211231144041@ram.dialup.fu-berlin.de>
<sqn2dj$14ld$2@gioia.aioe.org> <877dbk36qu.fsf@nosuchdomain.example.com>
<sqnmsm$1ff8$1@gioia.aioe.org> <sqo47m$n9p$1@dont-email.me>
<sqq0lb$1g77$1@gioia.aioe.org> <86v8ydp57h.fsf@linuxsc.com>
<ssehod$hfk$1@gioia.aioe.org> <86pmojo7th.fsf@linuxsc.com>
<sslpp3$61s$2@gioia.aioe.org> <86sfszlsfd.fsf@linuxsc.com>
<sthe7t$1cph$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 4 Feb 2022 03:35:03 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="b01fab8648ecf27ecc3a29ac06987053";
logging-data="32348"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18SHLOObcCgRUgGv/khTRKvr/tCcGqCVys="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.5.0
Cancel-Lock: sha1:VC26hyHNjjNrLGUhiz2aWQ8CT/k=
In-Reply-To: <sthe7t$1cph$1@gioia.aioe.org>
Content-Language: en-US
 by: James Kuyper - Fri, 4 Feb 2022 03:35 UTC

On 2/3/22 15:34, Mateusz Viste wrote:
....
> let me ask: what C99 feature do you use, that you wouldn't want to
> loose?

Many of the new features that I especially like are related to floating
point operations, which is of special interest to me because I've
specialized in scientific computer programming. I've marked those with
asterisks below. But the rest would be of interest to me pretty much
regardless of what kind of programming I was doing. Paragraph 5 of the
forward to C99 summarizes the changes from C90. These are the ones I
particularly like:

— more precise aliasing rules via effective type
— restricted pointers
— variable length arrays
— flexible array members
— static and type qualifiers in parameter array declarators
— complex (and imaginary) support in <complex.h>*
— remove implicit int
— reliable integer division
— hexadecimal floating-point constants and %a and %A printf/scanf
conversion specifiers*
— compound literals
— designated initializers
— // comments
— extended integer types and library functions in <inttypes.h> and
<stdint.h>
— remove implicit function declaration
— mixed declarations and code
— new block scopes for selection and iteration statements
— additional math library functions in <math.h> *
— treatment of error conditions by math library functions
(math_errhandling )*
— floating-point environment access in <fenv.h>*
— IEC 60559 (also known as IEC 559 or IEEE arithmetic) support*
— the snprintf family of functions in <stdio.h>
— standard pragmas*
— _ _func_ _ predefined identifier
— additional strftime conversion specifiers
— relaxed constraints on aggregate and union initialization
— return without expression not permitted in function that returns a
value (and vice versa)

Re: How to avoid an overflow during multiplication?

<6f966456-55d7-40f4-9815-25096cd2c709n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:622a:192:: with SMTP id s18mr1246043qtw.43.1643963795294;
Fri, 04 Feb 2022 00:36:35 -0800 (PST)
X-Received: by 2002:a05:6214:2aad:: with SMTP id js13mr1161257qvb.82.1643963795173;
Fri, 04 Feb 2022 00:36:35 -0800 (PST)
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: Fri, 4 Feb 2022 00:36:35 -0800 (PST)
In-Reply-To: <sti5s0$dtj$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: <sqmkg9$157v$1@gioia.aioe.org> <8735m954yz.fsf@bsb.me.uk>
<sqn0aq$1e8b$1@gioia.aioe.org> <unsigned-20211231144041@ram.dialup.fu-berlin.de>
<sqn2dj$14ld$2@gioia.aioe.org> <877dbk36qu.fsf@nosuchdomain.example.com>
<sqnmsm$1ff8$1@gioia.aioe.org> <sqo47m$n9p$1@dont-email.me>
<sqq0lb$1g77$1@gioia.aioe.org> <86v8ydp57h.fsf@linuxsc.com>
<ssehod$hfk$1@gioia.aioe.org> <86pmojo7th.fsf@linuxsc.com>
<sslpp3$61s$2@gioia.aioe.org> <86sfszlsfd.fsf@linuxsc.com>
<sthe7t$1cph$1@gioia.aioe.org> <sthkum$5nu$2@dont-email.me>
<sthm4i$d2b$1@gioia.aioe.org> <sti5s0$dtj$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <6f966456-55d7-40f4-9815-25096cd2c709n@googlegroups.com>
Subject: Re: How to avoid an overflow during multiplication?
From: oot...@hot.ee (Öö Tiib)
Injection-Date: Fri, 04 Feb 2022 08:36:35 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 19
 by: Öö Tiib - Fri, 4 Feb 2022 08:36 UTC

On Friday, 4 February 2022 at 05:17:33 UTC+2, james...@alumni.caltech.edu wrote:
> On 2/3/22 17:48, Mateusz Viste wrote:
> > 2022-02-03 at 22:28 +0000, Vir Campestris wrote:
> >> I use bool all the time; why would you use anything else? It's a type
> >> that holds 2 values, and the compiler can pick what to use for your
> >> processor. It might be a bit mask, or a 64 bit word. It might even
> >> vary with your optimisation settings.
> >
> > Can you tell what advantage it has over using, say, int_fast8_t?
> Conversions to C's _Bool (C standard 6.3.1.2), or C++'s bool (C++
> standard 7.3.14), type always generate only one of two possible values,
> which is what you need for true boolean semantics. That if very
> definitely not the case for int_fast8_t.

That bool has only two values was already said above. It feels
tautological that more constrained type brings less opportunities of
misuse and so less cases to check or to unit test. I do not know
why it is not perceived as an advantage by some people or why
they get offended like if they were accused or suspected of misuse
by providing such benefit.

Re: How to avoid an overflow during multiplication?

<stip0g$1aii$1@gioia.aioe.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!299gYy2nqWB43X4cCBV6zg.user.46.165.242.75.POSTED!not-for-mail
From: mate...@xyz.invalid (Mateusz Viste)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: Fri, 4 Feb 2022 09:44:00 +0100
Organization: . . .
Message-ID: <stip0g$1aii$1@gioia.aioe.org>
References: <sqmkg9$157v$1@gioia.aioe.org>
<8735m954yz.fsf@bsb.me.uk>
<sqn0aq$1e8b$1@gioia.aioe.org>
<unsigned-20211231144041@ram.dialup.fu-berlin.de>
<sqn2dj$14ld$2@gioia.aioe.org>
<877dbk36qu.fsf@nosuchdomain.example.com>
<sqnmsm$1ff8$1@gioia.aioe.org>
<sqo47m$n9p$1@dont-email.me>
<sqq0lb$1g77$1@gioia.aioe.org>
<86v8ydp57h.fsf@linuxsc.com>
<ssehod$hfk$1@gioia.aioe.org>
<86pmojo7th.fsf@linuxsc.com>
<sslpp3$61s$2@gioia.aioe.org>
<86sfszlsfd.fsf@linuxsc.com>
<sthe7t$1cph$1@gioia.aioe.org>
<sthkum$5nu$2@dont-email.me>
<sthm4i$d2b$1@gioia.aioe.org>
<sti5s0$dtj$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="43602"; posting-host="299gYy2nqWB43X4cCBV6zg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Mateusz Viste - Fri, 4 Feb 2022 08:44 UTC

2022-02-03 at 22:17 -0500, James Kuyper wrote:
> Conversions to C's _Bool (C standard 6.3.1.2), or C++'s bool (C++
> standard 7.3.14), type always generate only one of two possible
> values, which is what you need for true boolean semantics. That if
> very definitely not the case for int_fast8_t.

int_fast8_t (just like any other integer type) can be either zero or
non-zero. Why does the exact value of "not zero" matter to you? I mean,
what kind of practical problems can be solved with _Bool?

Mateusz

Re: How to avoid an overflow during multiplication?

<stisri$195u$1@gioia.aioe.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!299gYy2nqWB43X4cCBV6zg.user.46.165.242.75.POSTED!not-for-mail
From: mate...@xyz.invalid (Mateusz Viste)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: Fri, 4 Feb 2022 10:49:38 +0100
Organization: . . .
Message-ID: <stisri$195u$1@gioia.aioe.org>
References: <sqmkg9$157v$1@gioia.aioe.org>
<8735m954yz.fsf@bsb.me.uk>
<sqn0aq$1e8b$1@gioia.aioe.org>
<unsigned-20211231144041@ram.dialup.fu-berlin.de>
<sqn2dj$14ld$2@gioia.aioe.org>
<877dbk36qu.fsf@nosuchdomain.example.com>
<sqnmsm$1ff8$1@gioia.aioe.org>
<sqo47m$n9p$1@dont-email.me>
<sqq0lb$1g77$1@gioia.aioe.org>
<86v8ydp57h.fsf@linuxsc.com>
<ssehod$hfk$1@gioia.aioe.org>
<86pmojo7th.fsf@linuxsc.com>
<sslpp3$61s$2@gioia.aioe.org>
<86sfszlsfd.fsf@linuxsc.com>
<sthe7t$1cph$1@gioia.aioe.org>
<sthkum$5nu$2@dont-email.me>
<sthm4i$d2b$1@gioia.aioe.org>
<sti5s0$dtj$1@dont-email.me>
<6f966456-55d7-40f4-9815-25096cd2c709n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Injection-Info: gioia.aioe.org; logging-data="42174"; posting-host="299gYy2nqWB43X4cCBV6zg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Mateusz Viste - Fri, 4 Feb 2022 09:49 UTC

2022-02-04 at 00:36 -0800, Öö Tiib wrote:
> That bool has only two values was already said above. It feels
> tautological that more constrained type brings less opportunities of
> misuse and so less cases to check or to unit test. I do not know
> why it is not perceived as an advantage by some people or why
> they get offended like if they were accused or suspected of misuse
> by providing such benefit.

You are right of course. Having an emulated "1-bit" type is, in theory,
providing a nice way to handle values that are expected to be binary in
nature.

What I am wondering about is the practical use of it - ie. in what kind
of situation such _Bool type could save the day. I am certainly biased,
since I always used normal integer to keep flags, so I am genuinely
interested to see how other people do.

My usual need for "booleans" is storing flags. I do this then:

int flag_zorgl;
const char *s = read_string_from_space();

flag_zorgl = is_zorgl_present(s);
(...)
if (flag_zorgl) process_str_with_a_zorgl(s);
(...)
if (!flag_zorgl) no_zorgl_data_processing(s);

The above could very well use a _Bool. But would it be significantly
faster? Shorter? Safer? What I am looking for is a tangible example of
_Bool's added value in a practical situation.

It's worth noting that in the above code is_zorgl_present() could be
replaced by count_zorgls(), which would provide more information to the
program, without affecting the "boolean-like" operations. This is, of
course, an advantage only if the program requires such extra
information.

One scenario I can think of where _Bool would work better, is if
someone does this kind of things:

/* do something that require zorgl or other but not both */
if (flag_zorgl ^ flag_other) process(s);

This is an example I came up with only in a effort of trying to find a
rationalization for _Bool, it's not something I recall ever needing. If
I'd had needed it, I'd have to put some safeguards around values in
flags, or write a much longer form:

if (((flag_zorgl) && (!flag_other)) || ((!flag_zorgl) && (flag_other)))
{ process(s);
}

....or alternatively I could use an enum of two values to simulate a
bool. Either way it would be more hassle than _Bool. But again - it is
not something I recall ever needing. Are there other examples that
would be less hypothetical?

Mateusz

Re: How to avoid an overflow during multiplication?

<stitqj$23u$1@dont-email.me>

  copy mid

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

  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: How to avoid an overflow during multiplication?
Date: Fri, 4 Feb 2022 11:06:10 +0100
Organization: A noiseless patient Spider
Lines: 70
Message-ID: <stitqj$23u$1@dont-email.me>
References: <sqmkg9$157v$1@gioia.aioe.org> <8735m954yz.fsf@bsb.me.uk>
<sqn0aq$1e8b$1@gioia.aioe.org>
<unsigned-20211231144041@ram.dialup.fu-berlin.de>
<sqn2dj$14ld$2@gioia.aioe.org> <877dbk36qu.fsf@nosuchdomain.example.com>
<sqnmsm$1ff8$1@gioia.aioe.org> <sqo47m$n9p$1@dont-email.me>
<sqq0lb$1g77$1@gioia.aioe.org> <86v8ydp57h.fsf@linuxsc.com>
<ssehod$hfk$1@gioia.aioe.org> <86pmojo7th.fsf@linuxsc.com>
<sslpp3$61s$2@gioia.aioe.org> <86sfszlsfd.fsf@linuxsc.com>
<sthe7t$1cph$1@gioia.aioe.org> <sthkum$5nu$2@dont-email.me>
<sthm4i$d2b$1@gioia.aioe.org> <sti5s0$dtj$1@dont-email.me>
<stip0g$1aii$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 4 Feb 2022 10:06:11 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="889365d4f9fff30a572327e8f34289de";
logging-data="2174"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+0eL9hoUUWjJ2h155KOsVcZWUUz9TyQzs="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:Lud44YDKbdZmGc0IrCZYhaKYj3M=
In-Reply-To: <stip0g$1aii$1@gioia.aioe.org>
Content-Language: en-GB
 by: David Brown - Fri, 4 Feb 2022 10:06 UTC

On 04/02/2022 09:44, Mateusz Viste wrote:
> 2022-02-03 at 22:17 -0500, James Kuyper wrote:
>> Conversions to C's _Bool (C standard 6.3.1.2), or C++'s bool (C++
>> standard 7.3.14), type always generate only one of two possible
>> values, which is what you need for true boolean semantics. That if
>> very definitely not the case for int_fast8_t.
>
> int_fast8_t (just like any other integer type) can be either zero or
> non-zero. Why does the exact value of "not zero" matter to you? I mean,
> what kind of practical problems can be solved with _Bool?
>

There are, I think, two main differences here.

First is the logical one. A "bool" is a type that can be either "true"
or "false". This is clearer, simpler, and more closely models the
programmer's intent than storing a 0 or a 1 in a small integer type. If
I read :

bool isThingValid(const thing * p)

I know the result will be either "true" or "false".

If I read:

int_fast8_t isThingValid(const thing * p)

I am left wondering if the thing might be partially valid, with a scale.
I wonder /why/ the programmer wanted an arithmetic integer type aimed
at high-speed small integer ranges. Will the function always return 0
or 1? Does it consider non-zero as "true" ? Is it going to give a
negative value if there is an error? Did the programmer really want to
use an enumerated type but is converting to int_fast8_t to save space
somehow?

Prior to C99, it was extremely common in C programming to define your
own Boolean type. Mostly this was done by something like:

typedef int bool;
#define true 1
#define false 0

But sometimes signed or unsigned char was used, sometimes an enumerated
type, sometimes slightly different names or capitalisations were used.
A few would give different values for "true" and "false" (I've never
really understood why). Some would define "true" as "(0 == 0)" or
similar, in the mistaken belief that the result might vary by compiler.

It is hugely simpler, clearer and more portable to stick to the one
standard type.

Secondly, there are technical differences. The conversion of an integer
"x" to a _Bool "b" is effectively "b = !!x;". You are guaranteed that
"b" is either 0 or 1 - and the compiler can use that knowledge for
optimisation (as can the programmer). This can mean some operations
(such as conversion from an integer) may be less efficient, but other
operations (such as logical operations) are more efficient. Overall, it
is usually a win.

Of course you can write your C code without ever needing the type _Bool.
But you can write /better/ code, in a clearer, easier and more
efficient manner if you use it when appropriate.

(In C++, "bool" and its distinction from other integer types is hugely
more important.)

Re: How to avoid an overflow during multiplication?

<46fd23b2-d691-4742-b387-f66d071463a7n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:6214:e63:: with SMTP id jz3mr1959662qvb.24.1643982801516;
Fri, 04 Feb 2022 05:53:21 -0800 (PST)
X-Received: by 2002:a05:6214:21ab:: with SMTP id t11mr1887829qvc.74.1643982801339;
Fri, 04 Feb 2022 05:53:21 -0800 (PST)
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: Fri, 4 Feb 2022 05:53:21 -0800 (PST)
In-Reply-To: <stitqj$23u$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2a00:23a8:400a:5601:7db9:c679:bb69:690c;
posting-account=Dz2zqgkAAADlK5MFu78bw3ab-BRFV4Qn
NNTP-Posting-Host: 2a00:23a8:400a:5601:7db9:c679:bb69:690c
References: <sqmkg9$157v$1@gioia.aioe.org> <8735m954yz.fsf@bsb.me.uk>
<sqn0aq$1e8b$1@gioia.aioe.org> <unsigned-20211231144041@ram.dialup.fu-berlin.de>
<sqn2dj$14ld$2@gioia.aioe.org> <877dbk36qu.fsf@nosuchdomain.example.com>
<sqnmsm$1ff8$1@gioia.aioe.org> <sqo47m$n9p$1@dont-email.me>
<sqq0lb$1g77$1@gioia.aioe.org> <86v8ydp57h.fsf@linuxsc.com>
<ssehod$hfk$1@gioia.aioe.org> <86pmojo7th.fsf@linuxsc.com>
<sslpp3$61s$2@gioia.aioe.org> <86sfszlsfd.fsf@linuxsc.com>
<sthe7t$1cph$1@gioia.aioe.org> <sthkum$5nu$2@dont-email.me>
<sthm4i$d2b$1@gioia.aioe.org> <sti5s0$dtj$1@dont-email.me>
<stip0g$1aii$1@gioia.aioe.org> <stitqj$23u$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <46fd23b2-d691-4742-b387-f66d071463a7n@googlegroups.com>
Subject: Re: How to avoid an overflow during multiplication?
From: malcolm....@gmail.com (Malcolm McLean)
Injection-Date: Fri, 04 Feb 2022 13:53:21 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 54
 by: Malcolm McLean - Fri, 4 Feb 2022 13:53 UTC

On Friday, 4 February 2022 at 10:06:42 UTC, David Brown wrote:
> On 04/02/2022 09:44, Mateusz Viste wrote:
> > 2022-02-03 at 22:17 -0500, James Kuyper wrote:
> >> Conversions to C's _Bool (C standard 6.3.1.2), or C++'s bool (C++
> >> standard 7.3.14), type always generate only one of two possible
> >> values, which is what you need for true boolean semantics. That if
> >> very definitely not the case for int_fast8_t.
> >
> > int_fast8_t (just like any other integer type) can be either zero or
> > non-zero. Why does the exact value of "not zero" matter to you? I mean,
> > what kind of practical problems can be solved with _Bool?
> >
> There are, I think, two main differences here.
>
> First is the logical one. A "bool" is a type that can be either "true"
> or "false". This is clearer, simpler, and more closely models the
> programmer's intent than storing a 0 or a 1 in a small integer type. If
> I read :
>
> bool isThingValid(const thing * p)
>
> I know the result will be either "true" or "false".
>
>
> If I read:
>
> int_fast8_t isThingValid(const thing * p)
>
> I am left wondering if the thing might be partially valid, with a scale.
> I wonder /why/ the programmer wanted an arithmetic integer type
>
Yes. If a function returns values that are logically either true/false, then the
best way to document this is to return a boolean.

However if it takes values which are either true.false, then you have this
problem

void SetSpinEdit(SpinEdit *control, double value, bool clamp, bool roundtoprecison, bool propagatemessage);

All looks reasonable. A SpinEdit is obviously some GUI widget that displays a real value. The second parameter
is the value it is set to. "clamp" must mean that the control has "maximum" and "minimum" values and
we want to honour those in case our value is out of range. "roundtoprecision" probably means that
the control show, for example, two places after the decimal point, and if we pass 1.234 we expect "1.23"
to be displayed and the internal value to be as close to 1.23 as floating point will allow. "propagatemessage"
probaby means that the control has a callback associated with it when the user enters a value, and we want
that callback to be called.

However when you see this.

double height = fabs(maximumvalue(x, N));
// Maintainer's comment. Bug in this test here ?
if (height != GetSpinEdit(height_spn))
SetSpnEdit(height_spn, height, true, false, false);

It's far less obvious what is going on.

Re: How to avoid an overflow during multiplication?

<stjd4t$722$1@dont-email.me>

  copy mid

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

  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: How to avoid an overflow during multiplication?
Date: Fri, 4 Feb 2022 15:27:40 +0100
Organization: A noiseless patient Spider
Lines: 74
Message-ID: <stjd4t$722$1@dont-email.me>
References: <sqmkg9$157v$1@gioia.aioe.org> <8735m954yz.fsf@bsb.me.uk>
<sqn0aq$1e8b$1@gioia.aioe.org>
<unsigned-20211231144041@ram.dialup.fu-berlin.de>
<sqn2dj$14ld$2@gioia.aioe.org> <877dbk36qu.fsf@nosuchdomain.example.com>
<sqnmsm$1ff8$1@gioia.aioe.org> <sqo47m$n9p$1@dont-email.me>
<sqq0lb$1g77$1@gioia.aioe.org> <86v8ydp57h.fsf@linuxsc.com>
<ssehod$hfk$1@gioia.aioe.org> <86pmojo7th.fsf@linuxsc.com>
<sslpp3$61s$2@gioia.aioe.org> <86sfszlsfd.fsf@linuxsc.com>
<sthe7t$1cph$1@gioia.aioe.org> <sthkum$5nu$2@dont-email.me>
<sthm4i$d2b$1@gioia.aioe.org> <sti5s0$dtj$1@dont-email.me>
<stip0g$1aii$1@gioia.aioe.org> <stitqj$23u$1@dont-email.me>
<46fd23b2-d691-4742-b387-f66d071463a7n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 4 Feb 2022 14:27:41 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="889365d4f9fff30a572327e8f34289de";
logging-data="7234"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+zzi2fJv/FukBVGDWkXwa+bI4Z3g1AB2E="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:BKya8/E1y04iL/sLzLngpxvuhx4=
In-Reply-To: <46fd23b2-d691-4742-b387-f66d071463a7n@googlegroups.com>
Content-Language: en-GB
 by: David Brown - Fri, 4 Feb 2022 14:27 UTC

On 04/02/2022 14:53, Malcolm McLean wrote:
> On Friday, 4 February 2022 at 10:06:42 UTC, David Brown wrote:
>> On 04/02/2022 09:44, Mateusz Viste wrote:
>>> 2022-02-03 at 22:17 -0500, James Kuyper wrote:
>>>> Conversions to C's _Bool (C standard 6.3.1.2), or C++'s bool (C++
>>>> standard 7.3.14), type always generate only one of two possible
>>>> values, which is what you need for true boolean semantics. That if
>>>> very definitely not the case for int_fast8_t.
>>>
>>> int_fast8_t (just like any other integer type) can be either zero or
>>> non-zero. Why does the exact value of "not zero" matter to you? I mean,
>>> what kind of practical problems can be solved with _Bool?
>>>
>> There are, I think, two main differences here.
>>
>> First is the logical one. A "bool" is a type that can be either "true"
>> or "false". This is clearer, simpler, and more closely models the
>> programmer's intent than storing a 0 or a 1 in a small integer type. If
>> I read :
>>
>> bool isThingValid(const thing * p)
>>
>> I know the result will be either "true" or "false".
>>
>>
>> If I read:
>>
>> int_fast8_t isThingValid(const thing * p)
>>
>> I am left wondering if the thing might be partially valid, with a scale.
>> I wonder /why/ the programmer wanted an arithmetic integer type
>>
> Yes. If a function returns values that are logically either true/false, then the
> best way to document this is to return a boolean.
>
> However if it takes values which are either true.false, then you have this
> problem
>
> void SetSpinEdit(SpinEdit *control, double value, bool clamp, bool roundtoprecison, bool propagatemessage);
>
> All looks reasonable. A SpinEdit is obviously some GUI widget that displays a real value. The second parameter
> is the value it is set to. "clamp" must mean that the control has "maximum" and "minimum" values and
> we want to honour those in case our value is out of range. "roundtoprecision" probably means that
> the control show, for example, two places after the decimal point, and if we pass 1.234 we expect "1.23"
> to be displayed and the internal value to be as close to 1.23 as floating point will allow. "propagatemessage"
> probaby means that the control has a callback associated with it when the user enters a value, and we want
> that callback to be called.
>
> However when you see this.
>
> double height = fabs(maximumvalue(x, N));
> // Maintainer's comment. Bug in this test here ?
> if (height != GetSpinEdit(height_spn))
> SetSpnEdit(height_spn, height, true, false, false);
>
> It's far less obvious what is going on.
>

That is a completely separate issue, and nothing to do with the
question, which was "what advantages are there to using _Bool over an
plain integer type?".

Yes, it's hard to see what

SetSpnEdit(height_spn, height, true, false, false);

is doing. But you don't make anything better by writing

SetSpnEdit(height_spn, height, 1, 0, 0);

We could discuss ways to handle such paramaters in a clearer or less
error-prone way, but that would be a completely different discussion and
there is little in the improvements of C99 over C90 that affect it.

Re: How to avoid an overflow during multiplication?

<stjele$1hsd$1@gioia.aioe.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!299gYy2nqWB43X4cCBV6zg.user.46.165.242.75.POSTED!not-for-mail
From: mate...@xyz.invalid (Mateusz Viste)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: Fri, 4 Feb 2022 15:53:34 +0100
Organization: . . .
Message-ID: <stjele$1hsd$1@gioia.aioe.org>
References: <sqmkg9$157v$1@gioia.aioe.org>
<8735m954yz.fsf@bsb.me.uk>
<sqn0aq$1e8b$1@gioia.aioe.org>
<unsigned-20211231144041@ram.dialup.fu-berlin.de>
<sqn2dj$14ld$2@gioia.aioe.org>
<877dbk36qu.fsf@nosuchdomain.example.com>
<sqnmsm$1ff8$1@gioia.aioe.org>
<sqo47m$n9p$1@dont-email.me>
<sqq0lb$1g77$1@gioia.aioe.org>
<86v8ydp57h.fsf@linuxsc.com>
<ssehod$hfk$1@gioia.aioe.org>
<86pmojo7th.fsf@linuxsc.com>
<sslpp3$61s$2@gioia.aioe.org>
<86sfszlsfd.fsf@linuxsc.com>
<sthe7t$1cph$1@gioia.aioe.org>
<sthkum$5nu$2@dont-email.me>
<sthm4i$d2b$1@gioia.aioe.org>
<sti5s0$dtj$1@dont-email.me>
<stip0g$1aii$1@gioia.aioe.org>
<stitqj$23u$1@dont-email.me>
<46fd23b2-d691-4742-b387-f66d071463a7n@googlegroups.com>
<stjd4t$722$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="51085"; posting-host="299gYy2nqWB43X4cCBV6zg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Mateusz Viste - Fri, 4 Feb 2022 14:53 UTC

2022-02-04 at 15:27 +0100, David Brown wrote:
> That is a completely separate issue, and nothing to do with the
> question, which was "what advantages are there to using _Bool over an
> plain integer type?".

I think that in a vicious way, it is not separate.

> Yes, it's hard to see what
> SetSpnEdit(height_spn, height, true, false, false);
>
> is doing. But you don't make anything better by writing
> SetSpnEdit(height_spn, height, 1, 0, 0);

If I would be a user of _Bool, I'd be very tempted to use the first form
(and end up with a hardly readable line). Since I do not use _Bool,
I prefer doing this:

#define FLAG_SETSPN_CLAMP 0x01
#define FLAG_SETSPN_ROUNDTOPREC 0x02
#define FLAG_SETSPN_PROPAGMSG 0x04

SetSpnEdit(height_spn, height, FLAG_SETSPN_CLAMP);

You will say that the same can be done with having _Bool available (and
not using it, in this specific case), and you will be obviously right.
I guess it's a vicious side effect of the human mind - "if I have a
hammer, then everything looks like nails". :-)

Mateusz

Re: How to avoid an overflow during multiplication?

<stjiuc$bgr$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jameskuy...@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: Fri, 4 Feb 2022 11:06:36 -0500
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <stjiuc$bgr$1@dont-email.me>
References: <sqmkg9$157v$1@gioia.aioe.org> <8735m954yz.fsf@bsb.me.uk>
<sqn0aq$1e8b$1@gioia.aioe.org>
<unsigned-20211231144041@ram.dialup.fu-berlin.de>
<sqn2dj$14ld$2@gioia.aioe.org> <877dbk36qu.fsf@nosuchdomain.example.com>
<sqnmsm$1ff8$1@gioia.aioe.org> <sqo47m$n9p$1@dont-email.me>
<sqq0lb$1g77$1@gioia.aioe.org> <86v8ydp57h.fsf@linuxsc.com>
<ssehod$hfk$1@gioia.aioe.org> <86pmojo7th.fsf@linuxsc.com>
<sslpp3$61s$2@gioia.aioe.org> <86sfszlsfd.fsf@linuxsc.com>
<sthe7t$1cph$1@gioia.aioe.org> <sthkum$5nu$2@dont-email.me>
<sthm4i$d2b$1@gioia.aioe.org> <sti5s0$dtj$1@dont-email.me>
<stip0g$1aii$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 4 Feb 2022 16:06:36 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="b01fab8648ecf27ecc3a29ac06987053";
logging-data="11803"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/zVOkFb891Lo0P/rkCvtVbX0SqLKzxwPs="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.5.0
Cancel-Lock: sha1:MK4QfGXStfRAt78/SjoNGSlHE7A=
In-Reply-To: <stip0g$1aii$1@gioia.aioe.org>
Content-Language: en-US
 by: James Kuyper - Fri, 4 Feb 2022 16:06 UTC

On 2/4/22 03:44, Mateusz Viste wrote:
> 2022-02-03 at 22:17 -0500, James Kuyper wrote:
>> Conversions to C's _Bool (C standard 6.3.1.2), or C++'s bool (C++
>> standard 7.3.14), type always generate only one of two possible
>> values, which is what you need for true boolean semantics. That if
>> very definitely not the case for int_fast8_t.
>
> int_fast8_t (just like any other integer type) can be either zero or
> non-zero. Why does the exact value of "not zero" matter to you? I mean,
> what kind of practical problems can be solved with _Bool?

I'm far more interested in there being only one representation of
"true", than I am in how it's represented. If you misuse an ordinary
integer type as a substitute for a true boolean type, then equality and
inequality comparisons for that type aren't guaranteed to behave
properly - and that can be a problem. A simple comparison b1==b2, should
return a result of "true" if, and only if, both b1 and b2 are true, but
that would not be guaranteed if you misuse an ordinary integer type as
if it were a boolean type, because it might be the case that b1==3 and
b2==5.

Less importantly, in almost all cases _Bool values get implicitly
promoted to 'int'. You can take advantage of the fact that the resulting
conversion is guaranteed to produce a value of 1 if the value is true,
to write arithmetic expressions involving boolean values that could
break if you're misusing an ordinary integer type as if it were boolean.
5*b1 + 3*b2 should have a value of either 0, 3, 5, or 8, but that
wouldn't be guaranteed with an ordinary integer type.

Re: How to avoid an overflow during multiplication?

<nEcLJ.2426$3AH9.679@fx15.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!peer03.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.ams4!peer.am4.highwinds-media.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx15.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: How to avoid an overflow during multiplication?
Newsgroups: comp.lang.c
References: <sqmkg9$157v$1@gioia.aioe.org> <unsigned-20211231144041@ram.dialup.fu-berlin.de> <sqn2dj$14ld$2@gioia.aioe.org> <877dbk36qu.fsf@nosuchdomain.example.com> <sqnmsm$1ff8$1@gioia.aioe.org> <sqo47m$n9p$1@dont-email.me> <sqq0lb$1g77$1@gioia.aioe.org> <86v8ydp57h.fsf@linuxsc.com> <ssehod$hfk$1@gioia.aioe.org> <86pmojo7th.fsf@linuxsc.com> <sslpp3$61s$2@gioia.aioe.org> <86sfszlsfd.fsf@linuxsc.com> <sthe7t$1cph$1@gioia.aioe.org> <sthkum$5nu$2@dont-email.me> <sthm4i$d2b$1@gioia.aioe.org>
Lines: 11
Message-ID: <nEcLJ.2426$3AH9.679@fx15.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Fri, 04 Feb 2022 16:38:43 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Fri, 04 Feb 2022 16:38:43 GMT
X-Received-Bytes: 1660
 by: Scott Lurndal - Fri, 4 Feb 2022 16:38 UTC

Mateusz Viste <mateusz@xyz.invalid> writes:
>2022-02-03 at 22:28 +0000, Vir Campestris wrote:
>> I use bool all the time; why would you use anything else? It's a type
>> that holds 2 values, and the compiler can pick what to use for your
>> processor. It might be a bit mask, or a 64 bit word. It might even
>> vary with your optimisation settings.
>
>Can you tell what advantage it has over using, say, int_fast8_t?

half as many characters to type, and more precisely identifies function.

Re: How to avoid an overflow during multiplication?

<5eb48fca-6c40-402c-98a3-8187ed652499n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:6214:194a:: with SMTP id q10mr324965qvk.82.1643994127473;
Fri, 04 Feb 2022 09:02:07 -0800 (PST)
X-Received: by 2002:ac8:5716:: with SMTP id 22mr2678934qtw.322.1643994126673;
Fri, 04 Feb 2022 09:02:06 -0800 (PST)
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: Fri, 4 Feb 2022 09:02:06 -0800 (PST)
In-Reply-To: <stjiuc$bgr$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=81.143.231.9; posting-account=Dz2zqgkAAADlK5MFu78bw3ab-BRFV4Qn
NNTP-Posting-Host: 81.143.231.9
References: <sqmkg9$157v$1@gioia.aioe.org> <8735m954yz.fsf@bsb.me.uk>
<sqn0aq$1e8b$1@gioia.aioe.org> <unsigned-20211231144041@ram.dialup.fu-berlin.de>
<sqn2dj$14ld$2@gioia.aioe.org> <877dbk36qu.fsf@nosuchdomain.example.com>
<sqnmsm$1ff8$1@gioia.aioe.org> <sqo47m$n9p$1@dont-email.me>
<sqq0lb$1g77$1@gioia.aioe.org> <86v8ydp57h.fsf@linuxsc.com>
<ssehod$hfk$1@gioia.aioe.org> <86pmojo7th.fsf@linuxsc.com>
<sslpp3$61s$2@gioia.aioe.org> <86sfszlsfd.fsf@linuxsc.com>
<sthe7t$1cph$1@gioia.aioe.org> <sthkum$5nu$2@dont-email.me>
<sthm4i$d2b$1@gioia.aioe.org> <sti5s0$dtj$1@dont-email.me>
<stip0g$1aii$1@gioia.aioe.org> <stjiuc$bgr$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <5eb48fca-6c40-402c-98a3-8187ed652499n@googlegroups.com>
Subject: Re: How to avoid an overflow during multiplication?
From: malcolm....@gmail.com (Malcolm McLean)
Injection-Date: Fri, 04 Feb 2022 17:02:07 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 21
 by: Malcolm McLean - Fri, 4 Feb 2022 17:02 UTC

On Friday, 4 February 2022 at 16:07:12 UTC, james...@alumni.caltech.edu wrote:
> On 2/4/22 03:44, Mateusz Viste wrote:
> > 2022-02-03 at 22:17 -0500, James Kuyper wrote:
> >> Conversions to C's _Bool (C standard 6.3.1.2), or C++'s bool (C++
> >> standard 7.3.14), type always generate only one of two possible
> >> values, which is what you need for true boolean semantics. That if
> >> very definitely not the case for int_fast8_t.
> >
> > int_fast8_t (just like any other integer type) can be either zero or
> > non-zero. Why does the exact value of "not zero" matter to you? I mean,
> > what kind of practical problems can be solved with _Bool?
> I'm far more interested in there being only one representation of
> "true", than I am in how it's represented.
>
In C, true also means "exists". This is very idiomatic:
void whizzystringfunction(char *str)
{ if (str)
{
// we've been passed a string, do our whizzy things with it
}
}

Re: How to avoid an overflow during multiplication?

<00f67fc8-5b55-4fb8-9efb-427ad3abbd5dn@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:2405:: with SMTP id d5mr24349qkn.534.1643994200942;
Fri, 04 Feb 2022 09:03:20 -0800 (PST)
X-Received: by 2002:ac8:5c86:: with SMTP id r6mr2718168qta.20.1643994200783;
Fri, 04 Feb 2022 09:03:20 -0800 (PST)
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: Fri, 4 Feb 2022 09:03:20 -0800 (PST)
In-Reply-To: <stisri$195u$1@gioia.aioe.org>
Injection-Info: google-groups.googlegroups.com; posting-host=84.50.190.130; posting-account=pysjKgkAAACLegAdYDFznkqjgx_7vlUK
NNTP-Posting-Host: 84.50.190.130
References: <sqmkg9$157v$1@gioia.aioe.org> <8735m954yz.fsf@bsb.me.uk>
<sqn0aq$1e8b$1@gioia.aioe.org> <unsigned-20211231144041@ram.dialup.fu-berlin.de>
<sqn2dj$14ld$2@gioia.aioe.org> <877dbk36qu.fsf@nosuchdomain.example.com>
<sqnmsm$1ff8$1@gioia.aioe.org> <sqo47m$n9p$1@dont-email.me>
<sqq0lb$1g77$1@gioia.aioe.org> <86v8ydp57h.fsf@linuxsc.com>
<ssehod$hfk$1@gioia.aioe.org> <86pmojo7th.fsf@linuxsc.com>
<sslpp3$61s$2@gioia.aioe.org> <86sfszlsfd.fsf@linuxsc.com>
<sthe7t$1cph$1@gioia.aioe.org> <sthkum$5nu$2@dont-email.me>
<sthm4i$d2b$1@gioia.aioe.org> <sti5s0$dtj$1@dont-email.me>
<6f966456-55d7-40f4-9815-25096cd2c709n@googlegroups.com> <stisri$195u$1@gioia.aioe.org>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <00f67fc8-5b55-4fb8-9efb-427ad3abbd5dn@googlegroups.com>
Subject: Re: How to avoid an overflow during multiplication?
From: oot...@hot.ee (Öö Tiib)
Injection-Date: Fri, 04 Feb 2022 17:03:20 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 99
 by: Öö Tiib - Fri, 4 Feb 2022 17:03 UTC

On Friday, 4 February 2022 at 11:50:11 UTC+2, Mateusz Viste wrote:
> 2022-02-04 at 00:36 -0800, Öö Tiib wrote:
> > That bool has only two values was already said above. It feels
> > tautological that more constrained type brings less opportunities of
> > misuse and so less cases to check or to unit test. I do not know
> > why it is not perceived as an advantage by some people or why
> > they get offended like if they were accused or suspected of misuse
> > by providing such benefit.
>
> You are right of course. Having an emulated "1-bit" type is, in theory,
> providing a nice way to handle values that are expected to be binary in
> nature.
>
> What I am wondering about is the practical use of it - ie. in what kind
> of situation such _Bool type could save the day. I am certainly biased,
> since I always used normal integer to keep flags, so I am genuinely
> interested to see how other people do.

It is typically used to store, to return and to pass separate truth
values. I suspect you know that:

device->allows_cellular_connection = true; // store
if (is_authenticated(user)) { /*... */ } // return
set_visibility(separator, false); // pass

> My usual need for "booleans" is storing flags. I do this then:
>
> int flag_zorgl;
> const char *s = read_string_from_space();
>
> flag_zorgl = is_zorgl_present(s);
> (...)
> if (flag_zorgl) process_str_with_a_zorgl(s);
> (...)
> if (!flag_zorgl) no_zorgl_data_processing(s);

Yes that was what I meant. We can easily use int or char or enum type
for that flag_zorgl but then we just have doubt if it is worth to put a
debug check here or there or if it is worth to write unit tests for cases
when it has some other value than 0 or 1.

assert(flag_zorgl == 0 || flag_zorgl == 1);

>
> The above could very well use a _Bool. But would it be significantly
> faster? Shorter? Safer? What I am looking for is a tangible example of
> _Bool's added value in a practical situation.
>
> It's worth noting that in the above code is_zorgl_present() could be
> replaced by count_zorgls(), which would provide more information to the
> program, without affecting the "boolean-like" operations. This is, of
> course, an advantage only if the program requires such extra
> information.

My review would require rename ... flag_zorgl containing count of those
would be confusing. Also instead of if(count_zorgls(s)) I would
prefer if (count_zorgls(s)>0) or if (count_zorgls(s)!=0) as those
are more logical to read and each of us has huge displays
and can type 2-3 characters in blink of eye.

> One scenario I can think of where _Bool would work better, is if
> someone does this kind of things:
>
> /* do something that require zorgl or other but not both */
> if (flag_zorgl ^ flag_other) process(s);
>
> This is an example I came up with only in a effort of trying to find a
> rationalization for _Bool, it's not something I recall ever needing. If
> I'd had needed it, I'd have to put some safeguards around values in
> flags, or write a much longer form:
>
> if (((flag_zorgl) && (!flag_other)) || ((!flag_zorgl) && (flag_other)))
> {
> process(s);
> }

Huh? C does have logical and && and logical or || but logical
xor ^^ seems missing. Why? Because != can be used as logical xor.
But with ints where 42 means also true it looks bit weird:

if ( !flag_zorgl != !!flag_other ) process(s);

>
> ...or alternatively I could use an enum of two values to simulate a
> bool. Either way it would be more hassle than _Bool. But again - it is
> not something I recall ever needing. Are there other examples that
> would be less hypothetical?

No. For me the clear reasons are already sufficient:
1) the type conveys to reader of code that it has only two values
2) no one raises questions about need of checking if it is really so
3) compilers and programmers can find opportunities to optimise
thanks to that fact.

Re: How to avoid an overflow during multiplication?

<87v8xujviv.fsf@nosuchdomain.example.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: Fri, 04 Feb 2022 11:12:40 -0800
Organization: None to speak of
Lines: 46
Message-ID: <87v8xujviv.fsf@nosuchdomain.example.com>
References: <sqmkg9$157v$1@gioia.aioe.org> <8735m954yz.fsf@bsb.me.uk>
<sqn0aq$1e8b$1@gioia.aioe.org>
<unsigned-20211231144041@ram.dialup.fu-berlin.de>
<sqn2dj$14ld$2@gioia.aioe.org>
<877dbk36qu.fsf@nosuchdomain.example.com>
<sqnmsm$1ff8$1@gioia.aioe.org> <sqo47m$n9p$1@dont-email.me>
<sqq0lb$1g77$1@gioia.aioe.org> <86v8ydp57h.fsf@linuxsc.com>
<ssehod$hfk$1@gioia.aioe.org> <86pmojo7th.fsf@linuxsc.com>
<sslpp3$61s$2@gioia.aioe.org> <86sfszlsfd.fsf@linuxsc.com>
<sthe7t$1cph$1@gioia.aioe.org> <sthkum$5nu$2@dont-email.me>
<sthm4i$d2b$1@gioia.aioe.org> <sti5s0$dtj$1@dont-email.me>
<stip0g$1aii$1@gioia.aioe.org> <stjiuc$bgr$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="7f1d5cf8385e80db09fcf2c43731a30e";
logging-data="26791"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19YuYr7coaGNHY3knAtOvfJ"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:awP6N2AjdB6Dvh3sqofYW57l3l4=
sha1:4Dw9AcNDhKk3Ja26NAJhyC4c7FY=
 by: Keith Thompson - Fri, 4 Feb 2022 19:12 UTC

James Kuyper <jameskuyper@alumni.caltech.edu> writes:
> On 2/4/22 03:44, Mateusz Viste wrote:
>> 2022-02-03 at 22:17 -0500, James Kuyper wrote:
>>> Conversions to C's _Bool (C standard 6.3.1.2), or C++'s bool (C++
>>> standard 7.3.14), type always generate only one of two possible
>>> values, which is what you need for true boolean semantics. That if
>>> very definitely not the case for int_fast8_t.
>>
>> int_fast8_t (just like any other integer type) can be either zero or
>> non-zero. Why does the exact value of "not zero" matter to you? I mean,
>> what kind of practical problems can be solved with _Bool?
>
> I'm far more interested in there being only one representation of
> "true", than I am in how it's represented. If you misuse an ordinary
> integer type as a substitute for a true boolean type, then equality and
> inequality comparisons for that type aren't guaranteed to behave
> properly - and that can be a problem. A simple comparison b1==b2, should
> return a result of "true" if, and only if, both b1 and b2 are true, but
> that would not be guaranteed if you misuse an ordinary integer type as
> if it were a boolean type, because it might be the case that b1==3 and
> b2==5.

Quibble: b1==b2 is true iff b1 and b2 are both true *or* they're both
false.

[...]

> Less importantly, in almost all cases _Bool values get implicitly
> promoted to 'int'. You can take advantage of the fact that the resulting
> conversion is guaranteed to produce a value of 1 if the value is true,
> to write arithmetic expressions involving boolean values that could
> break if you're misusing an ordinary integer type as if it were boolean.
> 5*b1 + 3*b2 should have a value of either 0, 3, 5, or 8, but that
> wouldn't be guaranteed with an ordinary integer type.

A perhaps more plausible example of that:

int widget_count = 0;
widget_count += has_foo_widget;
widget_count += has_bar_widget;
...

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

Re: How to avoid an overflow during multiplication?

<stjvbo$dmc$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: Fri, 4 Feb 2022 19:38:33 +0000
Organization: A noiseless patient Spider
Lines: 105
Message-ID: <stjvbo$dmc$1@dont-email.me>
References: <sqmkg9$157v$1@gioia.aioe.org> <8735m954yz.fsf@bsb.me.uk>
<sqn0aq$1e8b$1@gioia.aioe.org>
<unsigned-20211231144041@ram.dialup.fu-berlin.de>
<sqn2dj$14ld$2@gioia.aioe.org> <877dbk36qu.fsf@nosuchdomain.example.com>
<sqnmsm$1ff8$1@gioia.aioe.org> <sqo47m$n9p$1@dont-email.me>
<sqq0lb$1g77$1@gioia.aioe.org> <86v8ydp57h.fsf@linuxsc.com>
<ssehod$hfk$1@gioia.aioe.org> <86pmojo7th.fsf@linuxsc.com>
<sslpp3$61s$2@gioia.aioe.org> <86sfszlsfd.fsf@linuxsc.com>
<sthe7t$1cph$1@gioia.aioe.org> <sthkum$5nu$2@dont-email.me>
<sthm4i$d2b$1@gioia.aioe.org> <sti5s0$dtj$1@dont-email.me>
<stip0g$1aii$1@gioia.aioe.org> <stitqj$23u$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 4 Feb 2022 19:38:32 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="f58f9091b31d5a6aeec1c2f159b34b93";
logging-data="14028"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19kuZkoM2yHmGAR5klbUthqUZtt8mYKBBQ="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.5.0
Cancel-Lock: sha1:yHEFAbaP399Ci4i/Mk+yC/InWuA=
In-Reply-To: <stitqj$23u$1@dont-email.me>
 by: Bart - Fri, 4 Feb 2022 19:38 UTC

On 04/02/2022 10:06, David Brown wrote:
> On 04/02/2022 09:44, Mateusz Viste wrote:
>> 2022-02-03 at 22:17 -0500, James Kuyper wrote:
>>> Conversions to C's _Bool (C standard 6.3.1.2), or C++'s bool (C++
>>> standard 7.3.14), type always generate only one of two possible
>>> values, which is what you need for true boolean semantics. That if
>>> very definitely not the case for int_fast8_t.
>>
>> int_fast8_t (just like any other integer type) can be either zero or
>> non-zero. Why does the exact value of "not zero" matter to you? I mean,
>> what kind of practical problems can be solved with _Bool?
>>
>
> There are, I think, two main differences here.
>
> First is the logical one. A "bool" is a type that can be either "true"
> or "false". This is clearer, simpler, and more closely models the
> programmer's intent than storing a 0 or a 1 in a small integer type. If
> I read :
>
> bool isThingValid(const thing * p)
>
> I know the result will be either "true" or "false".
>
>
> If I read:
>
> int_fast8_t isThingValid(const thing * p)
>
> I am left wondering if the thing might be partially valid, with a scale.
> I wonder /why/ the programmer wanted an arithmetic integer type aimed
> at high-speed small integer ranges. Will the function always return 0
> or 1? Does it consider non-zero as "true" ? Is it going to give a
> negative value if there is an error? Did the programmer really want to
> use an enumerated type but is converting to int_fast8_t to save space
> somehow?
>
> Prior to C99, it was extremely common in C programming to define your
> own Boolean type. Mostly this was done by something like:
>
> typedef int bool;
> #define true 1
> #define false 0
>
> But sometimes signed or unsigned char was used, sometimes an enumerated
> type, sometimes slightly different names or capitalisations were used.
> A few would give different values for "true" and "false" (I've never
> really understood why). Some would define "true" as "(0 == 0)" or
> similar, in the mistaken belief that the result might vary by compiler.
>
> It is hugely simpler, clearer and more portable to stick to the one
> standard type.
>
>
> Secondly, there are technical differences. The conversion of an integer
> "x" to a _Bool "b" is effectively "b = !!x;". You are guaranteed that
> "b" is either 0 or 1 - and the compiler can use that knowledge for
> optimisation (as can the programmer). This can mean some operations
> (such as conversion from an integer) may be less efficient, but other
> operations (such as logical operations) are more efficient. Overall, it
> is usually a win.

You're never really quite sure what's happening with _Bool.

There's little control over its size. And in A program like this:

union {
_Bool a;
unsigned char b;
} x;

x.b=0x55;
printf("A= %X\n", x.a);
printf("B= %X\n", x.b);

if (x.a == true) puts("A is True");
if (x.a == false) puts("A is False");
if (x.a != true && x.a != false) puts("A is neither True nor False");

different compilers show different results, including this from gcc -O0:

A= 55
B= 55
A is True
A is False
A is neither True nor False

MSVC shows this:

A= 55
B= 55
A is neither True nor False

while Clang, and gcc -O3, displays:

A= 1
B= 55
A is True

You can probably claim this is due to UB or whatever, but the fact
remains that using regular int types, the results will be consistent and
predictable across all compilers. It /is/ possible for the byte value
that usually represents a bool type to be set something that is neither
true (1) or false (0).

Re: How to avoid an overflow during multiplication?

<stkgq0$tms$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jameskuy...@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: Fri, 4 Feb 2022 19:36:15 -0500
Organization: A noiseless patient Spider
Lines: 37
Message-ID: <stkgq0$tms$1@dont-email.me>
References: <sqmkg9$157v$1@gioia.aioe.org> <8735m954yz.fsf@bsb.me.uk>
<sqn0aq$1e8b$1@gioia.aioe.org>
<unsigned-20211231144041@ram.dialup.fu-berlin.de>
<sqn2dj$14ld$2@gioia.aioe.org> <877dbk36qu.fsf@nosuchdomain.example.com>
<sqnmsm$1ff8$1@gioia.aioe.org> <sqo47m$n9p$1@dont-email.me>
<sqq0lb$1g77$1@gioia.aioe.org> <86v8ydp57h.fsf@linuxsc.com>
<ssehod$hfk$1@gioia.aioe.org> <86pmojo7th.fsf@linuxsc.com>
<sslpp3$61s$2@gioia.aioe.org> <86sfszlsfd.fsf@linuxsc.com>
<sthe7t$1cph$1@gioia.aioe.org> <sthkum$5nu$2@dont-email.me>
<sthm4i$d2b$1@gioia.aioe.org> <sti5s0$dtj$1@dont-email.me>
<stip0g$1aii$1@gioia.aioe.org> <stjiuc$bgr$1@dont-email.me>
<87v8xujviv.fsf@nosuchdomain.example.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 5 Feb 2022 00:36:17 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="3d2b33619245bac830ff75ea2c47e9cd";
logging-data="30428"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18CzETzIq+npQXFpnr/8oXngODFUcLAa3k="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.5.0
Cancel-Lock: sha1:zZ9Ji0MCLyOz0sf8pCrWpGcj/Y4=
In-Reply-To: <87v8xujviv.fsf@nosuchdomain.example.com>
Content-Language: en-US
 by: James Kuyper - Sat, 5 Feb 2022 00:36 UTC

On 2/4/22 14:12, Keith Thompson wrote:
> James Kuyper <jameskuyper@alumni.caltech.edu> writes:
....
>> I'm far more interested in there being only one representation of
>> "true", than I am in how it's represented. If you misuse an ordinary
>> integer type as a substitute for a true boolean type, then equality and
>> inequality comparisons for that type aren't guaranteed to behave
>> properly - and that can be a problem. A simple comparison b1==b2, should
>> return a result of "true" if, and only if, both b1 and b2 are true, but
>> that would not be guaranteed if you misuse an ordinary integer type as
>> if it were a boolean type, because it might be the case that b1==3 and
>> b2==5.
>
> Quibble: b1==b2 is true iff b1 and b2 are both true *or* they're both
> false.

Correct. There's only one false value, either way, so I was thinking
mainly about the true values, and as a result worded my statement
incorrectly.

>> Less importantly, in almost all cases _Bool values get implicitly
>> promoted to 'int'. You can take advantage of the fact that the resulting
>> conversion is guaranteed to produce a value of 1 if the value is true,
>> to write arithmetic expressions involving boolean values that could
>> break if you're misusing an ordinary integer type as if it were boolean.
>> 5*b1 + 3*b2 should have a value of either 0, 3, 5, or 8, but that
>> wouldn't be guaranteed with an ordinary integer type.
>
> A perhaps more plausible example of that:
>
> int widget_count = 0;
> widget_count += has_foo_widget;
> widget_count += has_bar_widget;

Agreed - that is a more plausible thing to do.

Re: How to avoid an overflow during multiplication?

<86ee4hlwrw.fsf@linuxsc.com>

  copy mid

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

  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: How to avoid an overflow during multiplication?
Date: Fri, 04 Feb 2022 21:14:59 -0800
Organization: A noiseless patient Spider
Lines: 146
Message-ID: <86ee4hlwrw.fsf@linuxsc.com>
References: <sqmkg9$157v$1@gioia.aioe.org> <8735m954yz.fsf@bsb.me.uk> <sqn0aq$1e8b$1@gioia.aioe.org> <unsigned-20211231144041@ram.dialup.fu-berlin.de> <sqn2dj$14ld$2@gioia.aioe.org> <877dbk36qu.fsf@nosuchdomain.example.com> <sqnmsm$1ff8$1@gioia.aioe.org> <sqo47m$n9p$1@dont-email.me> <sqq0lb$1g77$1@gioia.aioe.org> <86v8ydp57h.fsf@linuxsc.com> <ssehod$hfk$1@gioia.aioe.org> <86pmojo7th.fsf@linuxsc.com> <sslpp3$61s$2@gioia.aioe.org> <86sfszlsfd.fsf@linuxsc.com> <sthe7t$1cph$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="fd296145b453fec571e4c44d6840aea3";
logging-data="12237"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+uL1J7tWiz0NP1X18IpcW/6+duUKlZws0="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:7aCyQNTy+7CovgOO+lT4EU96J00=
sha1:Gsapd11d92iWrvX1bIsex+5IQKU=
 by: Tim Rentsch - Sat, 5 Feb 2022 05:14 UTC

Mateusz Viste <mateusz@xyz.invalid> writes:

> 2022-02-03 at 10:24 -0800, Tim Rentsch wrote:

[...]

>> If you don't give C99 an earnest try of non-trivial duration,
>> you'll never know.
>
> [...] I do use C99 from time to time on some oddball projects, but
> for my daily stuff I simply don't feel it brings anything on top of
> what I have already with gnu89. But maybe I missed some key things,
> so let me ask: what C99 feature do you use, that you wouldn't want
> to loose?

I see this as two or three related questions, which I will try to
answer in turn. The third of these relates to the question of
using option -std=gnuXX versus option -std=cXX, and is addressed
in the last section.

First there are several constructs that C89/C90 permits but are
disallowed in C99:

* calls to functions not previously declared are implicitly
declared (with nothing known about the parameter types, and
as returning type 'int')

* declarations (especially functions) are allowed not to give
a type specifier, in which case the type defaults to 'int'

* 'return' statements need not give an expression in a
function that returns a non-void type, and conversely

I understand why K&R C (and later C89/C90) allowed these things.
But they are all dangerous, especially implicit declaration of
functions, and it is good that C99 requires them to be flagged.

Second, there are lots of C99 additions (my list has at least 20)
that I find useful. These span a range of utility but in each
case either they offer something that is at best very inconvenient
to do in C89/C90 or that is only a minor convenience but one I
have gotten used to and would not want to give up. Here is a
selected subset, in rough order of increasing significance:

Number 10: 'enum' definitions allow a trailing comma. Only a
convenience feature, but a useful one, and it makes the language
more regular by aligning with the analogous rule for array
initializers.

Number 9: array parameters may have 'static' and 'const' etc.
Normally I use array notation (eg, 'int x[]') for parameters
that are treated as arrays rather than as pointers to single
objects. This feature allows length and qualifier information to
be added to the pointer parameter while still retaining array
notation in the parameter declaration.

Number 8: 'va_copy' macro. I found 'va_copy' indispensable
while implementing an extension to printf().

Number 7: 'long long' type (and library functions). It's nice to
have an integer type that is guaranteed to be at least 64 bits,
which C89/C90 doesn't have.

Number 6: snprintf and friends. Needed for safe formatting.

Number 5: restricted pointers. Especially when a function has
parameters that are pointers to characters, 'restrict' can help a
lot with quality of generated code (because of aliasing concerns).

Number 4: variadic macros. Compare this fragment

case 1: case 2: case 3: case 4:
case 5: case 6: case 7: case 8: case 9:

with this fragment

cases(1,2,3,4,5,6,7,8,9):

Variadic macros make writing a 'cases()' macro possible (and of
course there are other applications as well). I /write/ variadic
macros not very often, but I often see situations where /applying/
a variadic macro can make the code cleaner and neater.

Number 3: variably modified types. Variable length arrays are
sometimes problematic because of the danger of stack overflow.
But variably modified types, which are akin to VLAs but not the
same, provide similar benefits without the associated risk of
blowing the stack.

Number 2: non-constant block-level initializers for structs and
arrays. In C89/C90 initializers for structs and array must have
constant values for each element, including variables that are
function locals. In C99 this limitation is removed, avoiding the
need to do element-by-element assignment for what is logically
just an initialization.

Number 1: compound literals and designated initializers. I put
these two features together because they are often used together
and because there is some synergy between them. Individually, a
compound literal is often a safer alternative to casting, and
designated initializers provide an easy way of initializing a
sparse array (provided "sparse" means most of the elements are
zero). There are of course other applications but the last
sentence gives a couple of common examples.

I find myself using most of the features in the above list (not
all certainly, but more than half) in almost every C program I
work on.

Third area: on the matter of -std=gnuXX versus -std=cXX.

Many years ago I didn't pay much attention to differences between
gnu C and ISO standard C. Little by little I became aware of the
distinction and what some implications are of using one versus
using the other.

One key difference is that when using standard C you know what
you're getting. To say that another way, standard C can be
counted on to be the same across different platforms. Conversely
it is frustrating to take a program that compiles under gnu C
on one platform but doesn't on a different platform. I have seen
this result a lot more often than I would like.

A second key difference is that what "gnu C" means can (and
unfortunately sometimes does) change over time. Standard C
doesn't have that property (assuming of course we are sticking to
one version of the ISO C standard, such as C99).

I understand that some programs need access to functionality that
simply is not available if compiling as strictly standard C. In
such cases the use of non-standard options cannot be avoided, but
it can be localized. If and when problems come up, they will be
easier to deal with by virtue of being confined to a relatively
small and well-defined part of the program.

In times past I had a more laissez faire attitude toward allowing
non-standard compiler options. What changed my mind was being
bitten when something would work in one place or time and then
not work in another place or time. Using strictly standard C is
reliable and dependable, and that is worth a lot. Sometimes it
is necessary to make a (local) pragmatic exception to that rule,
and that's fine, but it's better for program structure and
reliability that such cases be exceptions rather than the usual
choice.

Re: How to avoid an overflow during multiplication?

<stlk7a$1gp0$1@gioia.aioe.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!299gYy2nqWB43X4cCBV6zg.user.46.165.242.75.POSTED!not-for-mail
From: mate...@xyz.invalid (Mateusz Viste)
Newsgroups: comp.lang.c
Subject: Re: How to avoid an overflow during multiplication?
Date: Sat, 5 Feb 2022 11:40:42 +0100
Organization: . . .
Message-ID: <stlk7a$1gp0$1@gioia.aioe.org>
References: <sqmkg9$157v$1@gioia.aioe.org>
<8735m954yz.fsf@bsb.me.uk>
<sqn0aq$1e8b$1@gioia.aioe.org>
<unsigned-20211231144041@ram.dialup.fu-berlin.de>
<sqn2dj$14ld$2@gioia.aioe.org>
<877dbk36qu.fsf@nosuchdomain.example.com>
<sqnmsm$1ff8$1@gioia.aioe.org>
<sqo47m$n9p$1@dont-email.me>
<sqq0lb$1g77$1@gioia.aioe.org>
<86v8ydp57h.fsf@linuxsc.com>
<ssehod$hfk$1@gioia.aioe.org>
<86pmojo7th.fsf@linuxsc.com>
<sslpp3$61s$2@gioia.aioe.org>
<86sfszlsfd.fsf@linuxsc.com>
<sthe7t$1cph$1@gioia.aioe.org>
<86ee4hlwrw.fsf@linuxsc.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Injection-Info: gioia.aioe.org; logging-data="49952"; posting-host="299gYy2nqWB43X4cCBV6zg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Mateusz Viste - Sat, 5 Feb 2022 10:40 UTC

2022-02-04 at 21:14 -0800, Tim Rentsch wrote:
> I see this as two or three related questions, which I will try to
> answer in turn.

Thanks for the time you took to answer this in such a well-structured
and concise way. Much appreciated.

> First there are several constructs that C89/C90 permits but are
> disallowed in C99:
> * calls to functions not previously declared are implicitly
> declared (with nothing known about the parameter types, and
> as returning type 'int')

That's a problem only in theory, because in practice compilers warn
about it anyway. At least these that I use do (gcc and clang, with -Wall
-pedantic). Such program:

int main(void) { return(poo(1)); }
int poo(kupa) { return(kupa); }

Triggers a warning with both gcc and clang, in both std=c89 and std=c99
(in all cases I use the -Wall flag, as I always do with all my code):

poo.c:1:25: warning: implicit declaration of function ‘poo’

Hence, while I agree with you on the theoretical annoyance of implicit
function declaration, it does not appear to be a practical concern.

> * declarations (especially functions) are allowed not to give
> a type specifier, in which case the type defaults to 'int'

Same story as above. gcc and clang both emit this, in both c89 and c99
modes with -pedantic:

for unspecified function type:
poo.c:2:1: warning: return type defaults to ‘int’

for unspecified parameter type:
poo.c:2:9: warning: parameter 'kupa' was not declared, defaulting to
type 'int'

> * 'return' statements need not give an expression in a
> function that returns a non-void type, and conversely

I have to admit I didn't know about that. But again, compilers
warn about this in both c89 and c99 modes:

clang (warns even without any extra warning flags):
poo.c:2:18: warning: non-void function does not return a value

gcc (requires -Wall):
poo.c:2:1: warning: control reaches end of non-void function

The function I tested was simply this:
int poo(kupa) { }

> Number 10: 'enum' definitions allow a trailing comma. Only a
> convenience feature, but a useful one

I think it is nice only because it limits the amount of changed lines
when adding a new value at the end of the list, so an svn diff is one
line shorter. But then I always wonder: did the programmer forgot about
some terminating entry, or did he leave this useless comma there on
purpose?

> Number 9: array parameters may have 'static' and 'const' etc.
> Normally I use array notation (eg, 'int x[]') for parameters
> that are treated as arrays rather than as pointers to single
> objects. This feature allows length and qualifier information to
> be added to the pointer parameter while still retaining array
> notation in the parameter declaration.

I am not sure I understand this one (and I also never use array
notation in function parameters). Are you referring to such construct?

void pupa(const char s[]) {
printf("%c", s[0]);
}

Apparently this passes without warnings in both c89 and c99 modes of
clang and gcc. But I probably misunderstood you.

> Number 8: 'va_copy' macro. I found 'va_copy' indispensable
> while implementing an extension to printf().

I never needed this, but this certainly depends on the kind of programs
one writes. Anyway, va_copy() is part of gnu89.

> Number 7: 'long long' type (and library functions). It's nice to
> have an integer type that is guaranteed to be at least 64 bits,
> which C89/C90 doesn't have.

I agree, and I routinely use int64_t types in c89 mode (requires
including stdint.h).

> Number 6: snprintf and friends. Needed for safe formatting.

Agreed, I use them very often. They are part of gnu89.

> Number 5: restricted pointers. Especially when a function has
> parameters that are pointers to characters, 'restrict' can help a
> lot with quality of generated code (because of aliasing concerns).

I find it's an optimization feature that is a burden to the programmer,
and that can lead to new bugs due to opening extra UB possibilities.
But I'd be curious to see benchmarks that demonstrate the practical
performance gain that may be achieved. Perhaps it's worth it in some
limited, performance-critical parts of code.

> Number 4: variadic macros.

In over 20 years of C I think that I used a variadic macro maybe two
times. :) And IIRC this was the reason I switched to C99 for these
projects, at least temporarily (because each time the macro was some
dirty hack that wasn't meant to stay in production code).

> Number 3: variably modified types. Variable length arrays are
> sometimes problematic because of the danger of stack overflow.
> But variably modified types, which are akin to VLAs but not the
> same, provide similar benefits without the associated risk of
> blowing the stack.

I know VLAs because I have been bitten by them in the past, but the
Variably Modified Types *without* the concept of VLAs is a new one to
me. Would you mind providing a short example where such VMT-without-VLA
is used meaningfully?

> Number 2: non-constant block-level initializers for structs and
> arrays. In C89/C90 initializers for structs and array must have
> constant values for each element, including variables that are
> function locals. In C99 this limitation is removed, avoiding the
> need to do element-by-element assignment for what is logically
> just an initialization.

Saves a memcpy() call (on paper, not in implementation)... ok, why not.

> Number 1: compound literals and designated initializers.

The idea here, as I understand it, is to pass structs or unions to
functions without having to declare them first. As such:

#include <stdio.h>

struct t { int x, y; };

static void poo(struct t p) {
printf("x = %d , y = %d\n", p.x, p.y);
}

int main(void) {
poo((struct t){0, 1});
return(0);
}

I wondered for a while what practical use I could have of it, and the
only place I was able to find quickly is for calling select() without
declaring a timeout struct first. But that would work only if I do not
need to read the struct back (and often I do, and yes I know it's a
despicable linuxism). You put it as #1 reason for C99, though, so I
imagine there must be some other hugely interesting uses that I don't
see right away. I will keep that in mind in my future code, looking for
occasions where it could be a win.

> One key difference is that when using standard C you know what
> you're getting. To say that another way, standard C can be
> counted on to be the same across different platforms. Conversely
> it is frustrating to take a program that compiles under gnu C
> on one platform but doesn't on a different platform.

I understand your point, really, but I consider this to be rather a
matter of aesthetics. Whether you have an ISO C program with
platform-dependent functions in a separate file or a gnu89 program, the
end result won't differ that much... In both cases you will have to
spend some time to port the thing if target platform changes.

To sum up - from the list you kindly provided there are some things that
are simply diagnostics, and these are available in compilers also in
C89. Other things are nice additions to C89 and that they are also
available in gnu89 (if one accepts using gnu89). One or two features
that are of disputable interest (because they depend on individual
needs probably), and then one or two that are potentially interesting. I
don't see any revolution that would change my coding life, but
nonetheless I will keep an eye on those one or two potentially
interesting things in the context of my daily work.

Mateusz

Re: How to avoid an overflow during multiplication?

<stlt2u$q62$1@dont-email.me>

  copy mid

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

  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: How to avoid an overflow during multiplication?
Date: Sat, 5 Feb 2022 14:11:57 +0100
Organization: A noiseless patient Spider
Lines: 50
Message-ID: <stlt2u$q62$1@dont-email.me>
References: <sqmkg9$157v$1@gioia.aioe.org> <8735m954yz.fsf@bsb.me.uk>
<sqn0aq$1e8b$1@gioia.aioe.org>
<unsigned-20211231144041@ram.dialup.fu-berlin.de>
<sqn2dj$14ld$2@gioia.aioe.org> <877dbk36qu.fsf@nosuchdomain.example.com>
<sqnmsm$1ff8$1@gioia.aioe.org> <sqo47m$n9p$1@dont-email.me>
<sqq0lb$1g77$1@gioia.aioe.org> <86v8ydp57h.fsf@linuxsc.com>
<ssehod$hfk$1@gioia.aioe.org> <86pmojo7th.fsf@linuxsc.com>
<sslpp3$61s$2@gioia.aioe.org> <86sfszlsfd.fsf@linuxsc.com>
<sthe7t$1cph$1@gioia.aioe.org> <sthkum$5nu$2@dont-email.me>
<sthm4i$d2b$1@gioia.aioe.org> <sti5s0$dtj$1@dont-email.me>
<stip0g$1aii$1@gioia.aioe.org> <stitqj$23u$1@dont-email.me>
<46fd23b2-d691-4742-b387-f66d071463a7n@googlegroups.com>
<stjd4t$722$1@dont-email.me> <stjele$1hsd$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 5 Feb 2022 13:11:58 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="2a1bd8dad6d2a8157f4e34697e9f4abb";
logging-data="26818"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ZvexLHuAHokqzoypIj3osBZ8s681xQ8E="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:YI54Xe0NJtm2WHXG1cU4WzyNx5U=
In-Reply-To: <stjele$1hsd$1@gioia.aioe.org>
Content-Language: en-GB
 by: David Brown - Sat, 5 Feb 2022 13:11 UTC

On 04/02/2022 15:53, Mateusz Viste wrote:
> 2022-02-04 at 15:27 +0100, David Brown wrote:
>> That is a completely separate issue, and nothing to do with the
>> question, which was "what advantages are there to using _Bool over an
>> plain integer type?".
>
> I think that in a vicious way, it is not separate.
>
>> Yes, it's hard to see what
>> SetSpnEdit(height_spn, height, true, false, false);
>>
>> is doing. But you don't make anything better by writing
>> SetSpnEdit(height_spn, height, 1, 0, 0);
>
> If I would be a user of _Bool, I'd be very tempted to use the first form
> (and end up with a hardly readable line).

I think you need to work on your temptation issues. When a language
supports a feature, you are not /required/ to use it just because it is
there. There are perhaps a dozen different ways to make the call to
SetSpnEdit clearer and safer - none of them are in any way broken by the
existence of "_Bool".

> I guess it's a vicious side effect of the human mind - "if I have a
> hammer, then everything looks like nails". :-)
>

You seem to be limiting yourself severely as a programmer. How you code
is of course up to you, but please do not assume all programmers are
going to see things that way.

And your analogy is all wrong. You are claiming that because you only
have a hammer (int), you are better equipped to invent a spanner when
you need it than people who also have access to a screwdriver (_Bool).

There is an argument to be made that the power of a programming language
comes from what it stops you from doing just as much as from what it
allows you to do, and that more features and possibilities allows for
more misuse as well as more good usage. I really don't think C99 comes
anywhere close to having too many features as a language. Historically,
many of the new features of C99 were taken from gcc (and other
compilers) extensions to C90 (such as "inline", C++ comments, and mixing
statements and declarations), or as standardisations of existing common
practices (such as size-specific integer types and boolean types). To a
fair extent, C99 was a standardisation of the language people were
already using for C programming.

Re: How to avoid an overflow during multiplication?

<stltoc$u8l$1@dont-email.me>

  copy mid

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

  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: How to avoid an overflow during multiplication?
Date: Sat, 5 Feb 2022 14:23:23 +0100
Organization: A noiseless patient Spider
Lines: 47
Message-ID: <stltoc$u8l$1@dont-email.me>
References: <sqmkg9$157v$1@gioia.aioe.org> <8735m954yz.fsf@bsb.me.uk>
<sqn0aq$1e8b$1@gioia.aioe.org>
<unsigned-20211231144041@ram.dialup.fu-berlin.de>
<sqn2dj$14ld$2@gioia.aioe.org> <877dbk36qu.fsf@nosuchdomain.example.com>
<sqnmsm$1ff8$1@gioia.aioe.org> <sqo47m$n9p$1@dont-email.me>
<sqq0lb$1g77$1@gioia.aioe.org> <86v8ydp57h.fsf@linuxsc.com>
<ssehod$hfk$1@gioia.aioe.org> <86pmojo7th.fsf@linuxsc.com>
<sslpp3$61s$2@gioia.aioe.org> <86sfszlsfd.fsf@linuxsc.com>
<sthe7t$1cph$1@gioia.aioe.org> <sthkum$5nu$2@dont-email.me>
<sthm4i$d2b$1@gioia.aioe.org> <sti5s0$dtj$1@dont-email.me>
<stip0g$1aii$1@gioia.aioe.org> <stitqj$23u$1@dont-email.me>
<stjvbo$dmc$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 5 Feb 2022 13:23:24 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="2a1bd8dad6d2a8157f4e34697e9f4abb";
logging-data="30997"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/NTXV0fcGYhHfk0p9uoUOM85VKu0fmNWI="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:LFgB5ijbprKnx2pil4AIvMol3yw=
In-Reply-To: <stjvbo$dmc$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Sat, 5 Feb 2022 13:23 UTC

On 04/02/2022 20:38, Bart wrote:
> On 04/02/2022 10:06, David Brown wrote:

>> Secondly, there are technical differences.  The conversion of an integer
>> "x" to a _Bool "b" is effectively "b = !!x;".  You are guaranteed that
>> "b" is either 0 or 1 - and the compiler can use that knowledge for
>> optimisation (as can the programmer).  This can mean some operations
>> (such as conversion from an integer) may be less efficient, but other
>> operations (such as logical operations) are more efficient.  Overall, it
>> is usually a win.
>
> You're never really quite sure what's happening with _Bool.
>
> There's little control over its size. And in A program like this:
>
>     union {
>         _Bool a;
>         unsigned char b;
>     } x;
>
>     x.b=0x55;
>     printf("A= %X\n", x.a);
>     printf("B= %X\n", x.b);
>

<snip>

> You can probably claim this is due to UB or whatever, but the fact
> remains that using regular int types, the results will be consistent and
> predictable across all compilers. It /is/ possible for the byte value
> that usually represents a bool type to be set something that is neither
> true (1) or false (0).
>

It /is/ undefined behaviour. (6.2.6.1p5 covers it, though I doubt if
you care.) It is also common sense. A _Bool contains either 0 or 1 -
any other value is invalid and can't be achieved without extraordinary
effort designed purely to make bad code.

Yes, you know /exactly/ what is happening with _Bool - write sane code,
and you get the results you expect. Write code with clear and
intentional errors with the sole intention of getting inconsistent
results, and you get exactly what you expect.

Pages:12345678
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor