Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Signed mod unsigned |
| Date | 2012-06-07 17:08 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <4FD11858.3040507@verizon.net> (permalink) |
| References | <jqnnfr$96n$1@news.albasani.net> <jqokpp$oui$1@speranza.aioe.org> <4FCFD9B2.9090507@verizon.net> <jqr3rr$vr$1@speranza.aioe.org> |
On 06/07/2012 04:44 PM, Edward Rutherford wrote:
> James Kuyper wrote:
>
>> On 06/06/2012 06:15 PM, Edward Rutherford wrote:
>>> Johannes Bauer wrote:
>>>
>>>> Hi group,
>>>>
>>>> today I ran into something I honestly did not expect. Consider this
>>>> snippet:
>>>>
>>>> #include <stdio.h>
>>>>
>>>> int main() {
>>>> int i1, i2;
>>>> unsigned int u2;
>>>>
>>>> i1 = -1;
>>>>
>>>> i2 = 30;
>>>> u2 = 30;
>>>>
>>>> printf("%d\n", i1 % i2);
>>>> printf("%d\n", i1 % u2);
>>>> return 0;
>>>> }
>>>>
>>>> I expected to find the output "-1 / -1". Instead the result I got was
>>>> "-1 / 15". Apparently when getting the remainder of a signed by a
>>>> unsigned int, the signed is silently promoted to unsigned in twos
>>>> complement and then the operation is performed, i.e.
>>>>
>>>> 0xffffffff % 30 == 15
>>>>
>>>> I would have expected at least a warning since I find this highly
>>>> unintuitive -- my expectation would be that the unsigned becomes a
>>>> (truncated) signed and a warning. Instead gcc does not emit a warning
>>>> (-Wall -Wextra) and silently performs above operation.
>>>>
>>>> Is this really in accordance with the C standard or is gcc doing
>>>> something weird here?
>>>
>>> This looks like a compiler bug. I would report it to a Gcc mailing
>>> list.
>>
>> I explained in my response why I believe that this behavior is not
>> merely allowed, but mandated, by the C standard. Do you disagree? If so,
>> on what grounds?
>
> This outcome just seems counterintuitive to me. Even if it is permitted
> by the C standard, this option is kinda retarded and I'd say the compiler
> has poor quality of implementation.
It's not merely permitted; it's not an option; it's required. The phrase
"Quality of implementation" is normally considered to apply only to
issues where the standard gives a conforming implementation freedom to
choose how something is done. The only freedom that the implementation
has that's relevant to this program is in number of value bits in an
unsigned int. You're not suggesting, I hope, that choosing 32 for that
number gives the implementation a low QoI? It's a pretty popular choice.
Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread
Signed mod unsigned Johannes Bauer <dfnsonfsduifb@gmx.de> - 2012-06-06 15:55 +0200
Re: Signed mod unsigned James Kuyper <jameskuyper@verizon.net> - 2012-06-06 10:41 -0400
Re: Signed mod unsigned Johannes Bauer <dfnsonfsduifb@gmx.de> - 2012-06-07 12:12 +0200
Re: Signed mod unsigned James Kuyper <jameskuyper@verizon.net> - 2012-06-07 06:44 -0400
Re: Signed mod unsigned Johannes Bauer <dfnsonfsduifb@gmx.de> - 2012-06-07 13:31 +0200
Re: Signed mod unsigned gwowen <gwowen@gmail.com> - 2012-06-06 07:53 -0700
Re: Signed mod unsigned Johannes Bauer <dfnsonfsduifb@gmx.de> - 2012-06-07 12:15 +0200
Re: Signed mod unsigned "BartC" <bc@freeuk.com> - 2012-06-07 12:29 +0100
Re: Signed mod unsigned Johannes Bauer <dfnsonfsduifb@gmx.de> - 2012-06-07 13:43 +0200
Re: Signed mod unsigned Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-07 07:46 -0400
Re: Signed mod unsigned Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-07 20:58 +0100
Re: Signed mod unsigned Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-07 19:58 -0400
Re: Signed mod unsigned Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-08 02:20 -0700
Re: Signed mod unsigned Johannes Bauer <dfnsonfsduifb@gmx.de> - 2012-06-08 11:33 +0200
Re: Signed mod unsigned Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-08 12:30 +0100
Re: Signed mod unsigned Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-08 11:12 -0700
Re: Signed mod unsigned Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-08 20:37 +0100
Re: Signed mod unsigned Keith Thompson <kst-u@mib.org> - 2012-06-08 13:44 -0700
Re: Signed mod unsigned Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-08 22:22 +0100
Re: Signed mod unsigned Johannes Bauer <dfnsonfsduifb@gmx.de> - 2012-06-10 10:56 +0200
Re: Signed mod unsigned Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-10 14:40 +0100
Re: Signed mod unsigned Keith Thompson <kst-u@mib.org> - 2012-06-11 15:49 -0700
Re: Signed mod unsigned Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-24 09:32 -0700
Re: Signed mod unsigned Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-08 11:05 -0700
Re: Signed mod unsigned Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-08 12:06 +0100
Re: Signed mod unsigned Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-08 11:32 -0700
Re: Signed mod unsigned Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> - 2012-06-06 22:15 +0000
Re: Signed mod unsigned James Kuyper <jameskuyper@verizon.net> - 2012-06-06 18:29 -0400
Re: Signed mod unsigned Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> - 2012-06-07 20:44 +0000
Re: Signed mod unsigned James Kuyper <jameskuyper@verizon.net> - 2012-06-07 17:08 -0400
csiph-web