[net.lang] Summary of modulus function

brian@digi-g.UUCP (Brian Westley) (09/06/84)

Here is a summary of the responses I received about the 'C' & Pascal &
Modula, etc. modulus functions.

I asked if (A mod B) should always fall within 0..B-1 even with negative A.
It seems that the usual mathematical definition of modulus says yes, while
most programming languages say no.

Most programming languages really use remainder, such that:
(A div B) * B + (A mod B) = A    or:
(A mod B) = A - (A div B) * B
(This still depends on whether div truncates towards zero, or always down)
Anyway, most languages return -2 for (-2 mod 3), while math modulus would
say (-2 mod 3) is 1.

Also, in math modulus, if B is negative, the answer is in B+1..0
Some langs don't allow B (or even A) to be negative.

Wouldn't it be nice to have a modulus function, a remainder function,
and two integer division functions, one truncating towards zero, the other
always truncating to largest integer <= A/B ?
I'm tired of having to do ((A+B) mod B) to get modulus to work the way I
want it to....

joe@petsd.UUCP (Joe Orost) (09/07/84)

<>
The designers of Ada* saw some light in the mod/rem area.  They put in both
"rem", which does what Fortran/C's MOD/% does, and "mod", which does what
all the math majors want.

Now all we need is a decent Ada* compiler.

[*Ada is a registered trademark of the U.S government, Ada Joint Program Office]

					regards,
					joe

--
Full-Name:  Joseph M. Orost
UUCP:       ..!{decvax,ucbvax,ihnp4}!vax135!petsd!joe
US Mail:    MS 313; Perkin-Elmer; 106 Apple St; Tinton Falls, NJ 07724
Phone:      (201) 870-5844