"modulas" operator in C

Dave Jones djones at megatest.UUCP
Sat Feb 17 12:49:07 AEST 1990


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.



More information about the Comp.lang.c mailing list