C integral types [was Re: Semi-OT: Why Kids Can't use Computers] (fwd)

D. Hugh Redelmeier hugh-pmF8o41NoarQT0dZR+AlfA at public.gmane.org
Thu Aug 15 20:25:25 UTC 2013


| From: David Collier-Brown <davec-b-bJEeYj9oJeDQT0dZR+AlfA at public.gmane.org>

| Previous to C, and in languages from the same broad family,
| you had "words" in B, and specific numbers of digits and bits in PL/1.
| 
| The C authors reacted strongly to "fixed binary 71" and introduced
| types that were less strongly tied to machine-word sizes.

And yet that is where we've gotten to in C with inttypes.h: you can
specify how many bits you need (for three different variants of need:
at least, fast, and exactly).

What seems better about C (kind of) is that arithmentic is done in crudely
quantized widths rather than finely tailored widths.  But the problem
is pretty much the same: surprising errors due to overflow.

I kind of think that integral arithmetic should be evaluated by the
compiler in sufficient width that no overflow in intermediate
calculations occurs except possibly in the case where the final result is too
big for its destination.  So in:
	"longv = intv1 + intv2;"
the + would be evaluated in long, not int.  But in:
	"intv = intv1 + intv2;"
the + could be evaluated in int.

Currently C evaluates intv1 + intv2 in int.

The idea of my proposal is that there is no overflow unless the programmer 
forced it.

Where PL/I went badly off the rails was the rule for division.  They gave 
away all the bits to fraction, leaving no room for a carry in subsequent 
calculations.
	8 + 7 / 3
would overflow if I remember correctly.

| I liked "fixed decimal 6", but hated "fixed binary 71", so C was a step
| up for me.  It's too bad their idea of fixed decimal wasn't Sue's!

Why?

Guess: you think when you care about the base, you care about
number-of-digit specs from the "real world" (forms and the like); with
binary, you really mean "an integer" and width is a distraction.
--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list