[comp.lang.c] "modulas" operator in C

alanf@bruce.OZ (Alan Grant Finlay) (02/16/90)

After 3 hours of tracking down a bug I found my C compiler didn't implement
the modulas operator "%" properly.  At least that's what I thought until on
further investigation I found this in Kernighan and Ritchie's manual:

	"The binary % operator yields the remainder from division of the
	 first expression by the second."

In short it turns out that the meaning of remainder is machine dependent and
normally this means (-1)%2 results in the value -1.

This is a gripe that probably keeps recurring but specifically I have the
following request.  Would the C community stop referring to this operator as
a modulas operator and call it something else like a "remainder" operator.

djones@megatest.UUCP (Dave Jones) (02/17/90)

He asked that we quit saying 'modulas'. Okay. It's 'modulus'.

C does not specify the results of '/' and '%' completely. See KR II, page
205. :-(

In some versions, divides round down. In some they round toward zero.
The round-down version works well with one kind of 'mod', the
round-toward-zero works, er.. sort of okay with the other kind.

Back when the Pascal committee was fighting over the proposed
standard, I threw a big snit and convinced some of the members that
Pascal should specify the mathematical definition, which has the
result of the operation X mod n always in the range 0 .. (n-1).
That's what they did.

Now, I'm not too sure I should have snitted. There is much to be
said for not changing (breaking) things. But if I were defining a new
language, I certainly would specify the mathematical definition.

The problem is that on some processors, the machine instructions for
divide and remainder behave the goofy way. So defining the functions
the goofy way makes the operations faster.

To complicate matters, some people say the right way is the goofy
way and the goofy way is the right way. Others say one is better
for some kinds of functions and the other is better for others.
We went round and round about it once before and, IMOO, the round-down
camp was victorious in every way except in obtaining an explicit
abject surrender from the enemy, something which has never happened
in the history of the net.