Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Unsinged types |
| Date | 2012-07-20 17:41 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <jucjal$b8e$1@dont-email.me> (permalink) |
| References | <ju9p6k$rg0$1@speranza.aioe.org> <87obnbfjvm.fsf@blp.benpfaff.org> <juchir$ut$1@usenet.stanford.edu> |
On 7/20/2012 5:11 PM, Seungbeom Kim wrote:
> On 2012-07-19 15:47, Ben Pfaff wrote:
>>
>> Many quantities are naturally unsigned; for example, counts and
>> sizes. These quantities are most naturally modeled with unsigned
>> types.
>
> Things that you assume should be non-negative often turn out to be
> not always so. For example, though no one thinks of a negative day
> of the month, the tm_* members of struct tm are signed, because you
> sometimes need to be able to represent out-of-range values.
It's also a way to help with some calculations. For example,
you can start from a given date, subtract fourteen from tm_mday,
re-normalize, and easily find out "What date was a fortnight
before January 5?"
> In addition, "unsigned" in C does not only mean non-negativity,
> but also implies modulo arithmetic; you can't even get the difference
> between two unsigned values in the most natural way (a - b).
You can't do that with signed integers, either. And in the
cases where naive subtraction doesn't work, you don't just get a
possibly surprising but predictable outcome: You get undefined
behavior. (Ever seen someone write a qsort() comparator that just
subtracts two `int' keys and returns the difference? Ever seen
the chaos that can result?)
> And the
> unsignedness is contagious, so (unsigned_value > -1) may not be true
> and ((unsigned)negative_value > 100) may be true to your surprise.
Oh, come on! You might just as well complain about double-ness
being "contagious."
Besides, why blame the surprises on the unsigned operand? They
don't arise from either one of the operands in isolation, but from
the combination of the two -- so the signed operand is every bit as
much to blame as the unsigned. If you blame one, you should blame
the other equally.[*]
[*] Okay, that doesn't always happen in real life: Doheny was
acquitted of offering the bribe that Fall was convicted of taking.
But Roaring Twenties jurisprudence is a poor model for programming!
> For these reasons, I had the feeling that unsigned was dangerous and
> should be limited to values which bitwise operations were intended for
> (e.g. bitmasks) or values on nominal/ordinal scales (e.g. character
> codes, TCP port numbers).
>
> The reality is, however, that everyone thinks differently, similar
> debates are endless, and that unsigned is widely used for other things
> (e.g. counts and sizes) even in the standard library, so you have to
> live with unsigned being everywhere, and learn to be careful when
> mixing signed and unsigned.
... or when mixing signed integer with long double complex, or
when mixing unsigned long with pointer-to-pointer-to-T, or ... In
fact, your recommendation to "be careful when..." can be improved
by deleting "when" and everything after it. Just be careful, okay?
--
Eric Sosman
esosman@ieee-dot-org.invalid
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Unsinged types Jase Schick <nospam@nospam.com> - 2012-07-19 20:03 +0000
Re: Unsinged types Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-19 21:29 +0100
Re: Unsinged types jacob navia <jacob@spamsink.net> - 2012-07-19 23:15 +0200
Re: Unsinged types Robert Wessel <robertwessel2@yahoo.com> - 2012-07-19 16:46 -0500
Re: Unsinged types Ben Pfaff <blp@cs.stanford.edu> - 2012-07-19 15:47 -0700
Re: Unsinged types Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-07-19 21:06 -0400
Re: Unsinged types Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-20 02:32 +0100
Re: Unsinged types Stephen Sprunk <stephen@sprunk.org> - 2012-07-19 21:08 -0500
Re: Unsinged types Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-20 04:26 +0100
Re: Unsinged types Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-07-19 22:56 -0400
Re: Unsinged types Kenneth Brody <kenbrody@spamcop.net> - 2012-07-20 14:57 -0400
Re: Unsinged types Keith Thompson <kst-u@mib.org> - 2012-07-20 12:36 -0700
Re: Unsinged types Kenneth Brody <kenbrody@spamcop.net> - 2012-07-20 14:56 -0400
Re: Unsinged types Seungbeom Kim <musiphil@bawi.org> - 2012-07-20 14:11 -0700
Re: Unsinged types Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-07-20 17:41 -0400
Re: Unsinged types Seungbeom Kim <musiphil@bawi.org> - 2012-07-21 14:07 -0700
Re: Unsinged types "BartC" <bc@freeuk.com> - 2012-07-21 22:26 +0100
Re: Unsinged types Tim Rentsch <txr@alumni.caltech.edu> - 2012-07-22 00:01 -0700
Re: Unsinged types "BartC" <bc@freeuk.com> - 2012-07-22 10:37 +0100
Re: Unsinged types Tim Rentsch <txr@alumni.caltech.edu> - 2012-09-07 11:34 -0700
Re: Unsinged types gwowen <gwowen@gmail.com> - 2012-07-23 00:06 -0700
Re: Unsinged types Tim Rentsch <txr@alumni.caltech.edu> - 2012-09-07 11:30 -0700
Re: Unsinged types Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-07-22 14:29 +0300
Re: Unsinged types "BartC" <bc@freeuk.com> - 2012-07-22 14:43 +0100
Re: Unsinged types Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-07-22 19:12 +0300
Re: Unsinged types Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-07-22 12:14 -0700
Re: Unsinged types Tim Rentsch <txr@alumni.caltech.edu> - 2012-07-21 14:57 -0700
Re: Unsinged types Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-07-20 00:11 -0700
Re: Unsinged types Andrew Cooper <amc96@cam.ac.uk> - 2012-07-20 10:00 +0100
Re: Unsinged types Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-07-20 23:48 -0700
Re: Unsinged types Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-21 15:24 +0100
Re: Unsinged types Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-07-21 08:53 -0700
Re: Unsinged types Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-21 17:48 +0100
Re: Unsinged types Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-07-21 11:22 -0700
Re: Unsinged types Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-21 22:35 +0100
Re: Unsinged types Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-07-21 14:55 -0700
Re: Unsinged types "BartC" <bc@freeuk.com> - 2012-07-21 18:27 +0100
Re: Unsinged types Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-07-21 11:37 -0700
Re: Unsinged types Kenneth Brody <kenbrody@spamcop.net> - 2012-07-23 14:02 -0400
Re: Unsinged types Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-07-23 12:21 -0700
Re: Unsinged types pete <pfiland@mindspring.com> - 2012-07-23 23:57 -0400
Re: Unsinged types "Bill Leary" <Bill_Leary@msn.com> - 2012-07-20 20:21 -0400
Re: Unsinged types "io_x" <a@b.c.invalid> - 2012-07-22 10:36 +0200
csiph-web