/, >>, %, and other ugliness...

der Mouse mouse at mcgill-vision.UUCP
Tue Nov 15 19:21:27 AEST 1988


In article <1081 at dukeac.UUCP>, sbigham at dukeac.UUCP (Scott Bigham) writes:
> In article <11529 at bellcore.bellcore.com> sjs at ctt.bellcore.com (Stan Switzer) writes:
>> Which is to say that you want the MODULUS operation rather than the
>> REMAINDER operation.
> Errr... The modulus operation IS the remainder operation.

The range of i MOD n is [0,n); the range of i REM n depends on the
division operation in use, since REM is the operation such that
(i REM n) + n * (i DIV n) == i
This usually means the range of i REM n is (-n,n), not [0,n), since DIV
usually truncates towards zero rather than either infinity.  C defines
/ and % (for integer operands) to be DIV and REM in my terminology
above.  Having all three flavors of DIV/REM behavior available would be
nice, though it could rapidly get confusing.

> Consider:
> double r;
> int i=5;
> ...
>  r=i/4;

> What I want is what i/4 -should- do; ie. return r=1.25.  Yes, I know
> I can say r=(double)(i/4).

And get 1.0, same as for r=i/4.  Perhaps you meant r=i/4.0 or
r=(double)i/4 instead?

In what sense "should" i/4 produce 1.25 instead of 1?  It may be what
*you* want, but it isn't C.

> The irony here is that C can't make this simple conversion,

Sure it can.  All you have to do is ask it to.  You seem to expect it
to read your mind and somehow realize that you want it to, without your
having to tell it so.

					der Mouse

			old: mcgill-vision!mouse
			new: mouse at larry.mcrcim.mcgill.edu



More information about the Comp.lang.c mailing list