Integer division

jimc at ucla-cs.UUCP jimc at ucla-cs.UUCP
Fri Feb 14 10:55:59 AEST 1986


In article <561 at jplgodo.UUCP> steve at jplgodo.UUCP (Steve Schlaifer x3171 156/224) writes:
>If you think of % as returning the *mathematical* remainder of a/b then
>it should return a value >=0.  On the other hand, to be consistent with this
>view, the quotient operator (/) will also have to be modified to preserve
>the formulae
>
>       b=qa+r (0<=r<a)
>       q=b/a
>
>i.e. (-3)/2 must be -2 if (-3)%2 is 1. But this then means that (|a|)/b is not
>the same as |a/b| for a<0. Maybe *An Angry Number Theorist* wants this, but...
>
Right on!  Invariably when I integer-divide negative numbers I have to do
fancy coding to cause (-3)/2 to come out -2 rather than -1.  I would very
much like to see the quotient of *signed* integers come out this way auto-
matically.  However, most hardware doesn't cooperate, necessitating extra
compiled code.  I hope the compiler would have a separate, more efficient
code macro for the unsigned case, so that users concerned with efficiency
and knowing about their hardware could avoid useless overhead by declaring
unsigned ints.
   This kind of efficient compilation would be helped if all int constants
were automatically unsigned.  In other words, the compiler would interpret
-5 as (unary negate operator)(cast to signed)(unsigned int ={5}).

James F. Carter            (213) 206-1306
UCLA-SEASnet; 2567 Boelter Hall; 405 Hilgard Ave.; Los Angeles, CA 90024
UUCP:...!{ihnp4,ucbvax,{hao!cepu}}!ucla-cs!jimc  ARPA:jimc at locus.UCLA.EDU



More information about the Comp.lang.c mailing list