Integer division

Steve Schlaifer x3171 156/224 steve at jplgodo.UUCP
Sat Feb 1 06:25:31 AEST 1986


In article <11610 at ucbvax.BERKELEY.EDU>, gsmith at brahms.BERKELEY.EDU (Gene Ward Smith) writes:
> In article <11603 at ucbvax.BERKELEY.EDU> weemba at brahms.UUCP (Matthew P. Wiener) writes:
> >In article <332 at ism780c.UUCP> tim at ism780c.UUCP (Tim Smith) writes:
> >>[Which is preferred: (-a)%b == - (a%b) or (-a)%b >= 0 always?]
> >>Are there any good mathematical grounds for choosing one alternative over
> >>the other here?  Note that I am not asking from a hardware point of view
> >>which is better.  I want to know mathematically if one is better than
> >>the other.
> >
> >I have NEVER seen an instance where the first one is preferable.  Not
> >only is it not preferable, it is just incorrect.  Why such a routine
> >has been allowed to be 50% inaccurate in every existing language all
> >these years is beyond me.
> >
> >[Whether CS people should even be *allowed* to make such mathematical
> >decisions is another question.  In C on UNIX, for example, one has
> 
> >ucbvax!brahms!weemba	Matthew P Wiener/UCB Math Dept/Berkeley CA 94720
> 
>   Matthew has just about said it all, but since this has been my absolute
> *pet* gripe for some time now, I can't resist adding another $0.02 to the
> bill. When mathematicians define functions in a certain way, it is almost
> always for good reasons. I can think of only a few cases where doing 
> .......
> Then why mess around with quotient and remainder
> functions when you don't have a clue on God's green Earth what you are
> doing?
> 
> 
>      Signed
> 
>      An Angry Number Theorist

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 it
seems to me to be a trap just waiting for the unwary to fall into.

As for why the restriction of 0<=r<a was decided on, my only guess is that
it then always produces a unique (q,r) for any given (a,b); this is a useful
property when you are proving theorems or doing theoretical investigations.
-- 

...smeagol\			Steve Schlaifer
......wlbr->!jplgodo!steve	Advance Projects Group, Jet Propulsion Labs
....group3/			4800 Oak Grove Drive, M/S 156/204
				Pasadena, California, 91109
					+1 818 354 3171



More information about the Comp.lang.c mailing list