int divided by unsigned.
Larry Jones
scjones at sdrc.UUCP
Sat Jun 17 07:31:59 AEST 1989
In article <13940 at lanl.gov>, jlg at lanl.gov (Jim Giles) writes:
>
> [ example of -5 / 1000U -- Cray gets zero instead of the large
> number required by ANSI ]
>
> This
> is an example of a case where deviating from the C definition produces
> desireable results. I hope Cray doesn't "fix" their C compiler.
>
> (Note: this behaviour occurs because C requires arguments to be "promoted"
> to unsigned if either is already unsigned. The preferable rule would be
> that if one argument is an int and the other is an unsigned, _both_ should
> be promoted to long before the operator is applied. Unsigned is _not_ a
> promotion from int - it is a break-even semantics change.)
Well, there're two good reasons why you DON'T want to promote to
long: long might well be the same size as int and, if it's not,
it may well take a lot longer to compute the answer. In the
first case, you get an answer which is no more useful than the
unsigned version, in the second case you violate the Spirit of C
by doing non-obvious things behind the programmer's back. If you
want long division, put in a cast! (Then when it doesn't work
because long and int are the same size, people can blame you
instead of the ANSI committee :-).
----
Larry Jones UUCP: uunet!sdrc!scjones
SDRC scjones at SDRC.UU.NET
2000 Eastman Dr. BIX: ltl
Milford, OH 45150-2789 AT&T: (513) 576-2070
"You can't get a body like mine in a bottle --
unless you push REAL HARD." - Judy Tenuta / Dr. Pepper
More information about the Comp.lang.c
mailing list