Re: What is your opinion about unsigned int u = -2 ?

Liste des GroupesRevenir à cl c  
Sujet : Re: What is your opinion about unsigned int u = -2 ?
De : david.brown (at) *nospam* hesbynett.no (David Brown)
Groupes : comp.lang.c
Date : 03. Aug 2024, 18:17:19
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v8lon1$3hv78$1@dont-email.me>
References : 1 2 3 4 5 6
User-Agent : Mozilla Thunderbird
On 02/08/2024 16:48, Bart wrote:
On 02/08/2024 15:33, Kenny McCormack wrote:
In article <v8inds$2qpqh$1@dont-email.me>,
Thiago Adams  <thiago.adams@gmail.com> wrote:
...
So it seams that anything is ok for unsigned but not for signed.
Maybe because all computer gave same answer for unsigned but this is not
true for signed?
>
I think it is because it wants to (still) support representations other
than 2s complement.  I think POSIX requires 2s complement, and I expect the
C standard to (eventually) follow suit.
>
 C23 assumes 2s complement. However overflow on signed integers will still be considered UB: too many compilers depend on it.
 But even if well-defined (eg. that UB was removed so that overflow just wraps as it does with unsigned), some here, whose initials may or may not be DB, consider such overflow Wrong and a bug in a program.
 However they don't consider overflow of unsigned values wrong at all, simply because C allows that behaviour.
 But I don't get it. If my calculation gives the wrong results because I've chosen a u32 type instead of u64, that's just as much a bug as using i32 instead of i64.
 
You don't get it because you never pay attention to what I write - you'd rather jump to conclusions without reading.
In almost all cases, wrapping signed integer overflow would give the incorrect (in terms of what makes sense for the code) result even if it is fully defined by the compiler.
In almost all cases, wrapping unsigned integer overflow would give the incorrect (in terms of what makes sense for the code) result regardless of the fact that C gives a definition for the behaviour.
There are, of course, exceptions - situations where you really do want modulo arithmetic.  But in most cases you want integer types that model real mathematical integers to the extent possible with efficient practical implementations.  Using 16-bit int because the numbers are easier, if you have 65535 apples in a pile and you add an apple, you do not expect to have 0 apples.  That would be almost as silly as having 32767 apples, adding one more, and having -32768 apples.
Outside of the occasional rare case, code that relies on overflow behaviour of integers - signed or unsigned, defined by the language/implementation or not - is logically incorrect code.
That is in addition to some cases (signed integer overflow for C) being undefined in the language.
It's helpful that the language provides a way to get modulo arithmetic for the cases that need it.  But just because C defines the behaviour of unsigned integer overflow, does not make it makes sense in code. Defined incorrect results are just as wrong as undefined incorrect results.

Date Sujet#  Auteur
31 Jul 24 * What is your opinion about unsigned int u = -2 ?56Thiago Adams
31 Jul 24 +* Re: What is your opinion about unsigned int u = -2 ?7Ben Bacarisse
11 Aug 24 i`* Re: What is your opinion about unsigned int u = -2 ?6Tim Rentsch
11 Aug 24 i `* Re: What is your opinion about unsigned int u = -2 ?5Vir Campestris
11 Aug 24 i  +- Re: What is your opinion about unsigned int u = -2 ?1Richard Damon
11 Aug 24 i  +* Re: What is your opinion about unsigned int u = -2 ?2James Kuyper
12 Aug 24 i  i`- Re: What is your opinion about unsigned int u = -2 ?1Vir Campestris
12 Aug 24 i  `- Re: What is your opinion about unsigned int u = -2 ?1Tim Rentsch
31 Jul 24 +- Re: What is your opinion about unsigned int u = -2 ?1James Kuyper
1 Aug 24 `* Re: What is your opinion about unsigned int u = -2 ?47Blue-Maned_Hawk
1 Aug 24  +* Re: What is your opinion about unsigned int u = -2 ?45Ben Bacarisse
2 Aug 24  i`* Re: What is your opinion about unsigned int u = -2 ?44Thiago Adams
2 Aug 24  i `* Re: What is your opinion about unsigned int u = -2 ?43Kenny McCormack
2 Aug 24  i  +* Re: What is your opinion about unsigned int u = -2 ?34Bart
2 Aug 24  i  i+- Re: What is your opinion about unsigned int u = -2 ?1Ben Bacarisse
2 Aug 24  i  i+* Re: What is your opinion about unsigned int u = -2 ?2Thiago Adams
2 Aug 24  i  ii`- Re: What is your opinion about unsigned int u = -2 ?1Thiago Adams
2 Aug 24  i  i+* Re: What is your opinion about unsigned int u = -2 ?29Keith Thompson
2 Aug 24  i  ii+* Re: What is your opinion about unsigned int u = -2 ?27James Kuyper
3 Aug 24  i  iii+* Re: What is your opinion about unsigned int u = -2 ?19Thiago Adams
3 Aug 24  i  iiii+* Re: What is your opinion about unsigned int u = -2 ?8Thiago Adams
3 Aug 24  i  iiiii`* Re: What is your opinion about unsigned int u = -2 ?7Keith Thompson
3 Aug 24  i  iiiii `* Re: What is your opinion about unsigned int u = -2 ?6Thiago Adams
3 Aug 24  i  iiiii  `* Re: What is your opinion about unsigned int u = -2 ?5Keith Thompson
3 Aug 24  i  iiiii   `* Re: What is your opinion about unsigned int u = -2 ?4Thiago Adams
4 Aug 24  i  iiiii    `* Re: What is your opinion about unsigned int u = -2 ?3Keith Thompson
4 Aug 24  i  iiiii     `* Re: What is your opinion about unsigned int u = -2 ?2Thiago Adams
4 Aug 24  i  iiiii      `- Re: What is your opinion about unsigned int u = -2 ?1Thiago Adams
3 Aug 24  i  iiii`* Re: What is your opinion about unsigned int u = -2 ?10Keith Thompson
3 Aug 24  i  iiii `* Re: What is your opinion about unsigned int u = -2 ?9Thiago Adams
3 Aug 24  i  iiii  `* Re: What is your opinion about unsigned int u = -2 ?8Keith Thompson
3 Aug 24  i  iiii   `* Re: What is your opinion about unsigned int u = -2 ?7Thiago Adams
3 Aug 24  i  iiii    `* Re: What is your opinion about unsigned int u = -2 ?6Keith Thompson
3 Aug 24  i  iiii     +* Re: What is your opinion about unsigned int u = -2 ?3David Brown
3 Aug 24  i  iiii     i`* Re: What is your opinion about unsigned int u = -2 ?2Thiago Adams
4 Aug 24  i  iiii     i `- Re: What is your opinion about unsigned int u = -2 ?1Keith Thompson
25 Aug 24  i  iiii     `* Re: What is your opinion about unsigned int u = -2 ?2dave thompson 2
25 Aug 24  i  iiii      `- Re: What is your opinion about unsigned int u = -2 ?1Keith Thompson
3 Aug 24  i  iii`* Re: What is your opinion about unsigned int u = -2 ?7David Brown
3 Aug 24  i  iii `* Re: What is your opinion about unsigned int u = -2 ?6Thiago Adams
3 Aug 24  i  iii  +* Re: What is your opinion about unsigned int u = -2 ?4Thiago Adams
4 Aug 24  i  iii  i`* Re: What is your opinion about unsigned int u = -2 ?3Keith Thompson
11 Aug 24  i  iii  i `* Re: What is your opinion about unsigned int u = -2 ?2Tim Rentsch
11 Aug 24  i  iii  i  `- Re: What is your opinion about unsigned int u = -2 ?1Keith Thompson
4 Aug 24  i  iii  `- Re: What is your opinion about unsigned int u = -2 ?1Keith Thompson
9 Aug 24  i  ii`- Re: What is your opinion about unsigned int u = -2 ?1Bonita Montero
3 Aug 24  i  i`- Re: What is your opinion about unsigned int u = -2 ?1David Brown
4 Aug 24  i  `* Re: What is your opinion about unsigned int u = -2 ?8Bonita Montero
8 Aug 24  i   `* Re: What is your opinion about unsigned int u = -2 ?7Lawrence D'Oliveiro
8 Aug 24  i    `* Re: What is your opinion about unsigned int u = -2 ?6David Brown
9 Aug 24  i     `* Re: What is your opinion about unsigned int u = -2 ?5Bonita Montero
9 Aug 24  i      `* Re: What is your opinion about unsigned int u = -2 ?4David Brown
9 Aug 24  i       +* Re: What is your opinion about unsigned int u = -2 ?2Bonita Montero
9 Aug 24  i       i`- Re: What is your opinion about unsigned int u = -2 ?1David Brown
10 Aug 24  i       `- Re: What is your opinion about unsigned int u = -2 ?1Richard Damon
4 Aug 24  `- Re: What is your opinion about unsigned int u = -2 ?1Bonita Montero

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal